i have a csv file where i have 1000 videos links . I want to check whether these videos still exists or they have been removed or deleted from YouTube. How can i do that in python?
Please guide on this
show your code, you don;'t even state whether you're using requests, urllib or whatever for access.
You should take a look at snippsats tutorials on web scraping
here:
part1
and
part2
using requests, if you get status_code != 200, the file doesn't exist.
(you can use exception clause of try, except to capture errors.
You should look
YouTube Data API v3.
Need a google account then get can get free API key and most also enable API.
Then can parse out video id out of csv and check if it's
publicStatsViewable
True or False.
Url is build like this.
https://www.googleapis.com/youtube/v3/videos?id={id_of_the_video}&key={your_api_key}&part=status
Then can write code.
import requests
from pprint import pprint
id_of_video = 'CUNq2_VjRn4'
your_api_key = 'xxxxxxxxxxxxxx'
url = f'https://www.googleapis.com/youtube/v3/videos?id={id_of_video}&key={your_api_key}&part=status'
url_get = requests.get(url)
pprint(url_get.json())
Output:
{'etag': '"RmznBCICv9YtgWaaa_nWDIH1_GM/XBA-sYAccdJQD803PbrNziArt-U"',
'items': [{'etag': '"RmznBCICv9YtgWaaa_nWDIH1_GM/i4VlXNZUnKWY8OIv_qh-vAbzstI"',
'id': 'CUNq2_VjRn4',
'kind': 'youtube#video',
'status': {'embeddable': True,
'license': 'youtube',
'privacyStatus': 'public',
'publicStatsViewable': True,
'uploadStatus': 'processed'}}],
'kind': 'youtube#videoListResponse',
'pageInfo': {'resultsPerPage': 1, 'totalResults': 1}}
The part needed.
>>> url_get.json()['items'][0]['status']['publicStatsViewable']
True
hi, Larz60+, I had no idea on how would i start on that so i asked for guidance.
HI, snippsat for your help. I had no idea on how to use API. so first i will study and practice all articles provided by Larz60+ on webscraping and then i will study your code.
Thank you so much again for giving me a kickstart
(Apr-10-2018, 05:18 PM)snippsat Wrote: [ -> ]Then can write code.
import requests
from pprint import pprint
id_of_video = 'CUNq2_VjRn4'
your_api_key = 'xxxxxxxxxxxxxx'
url = f'https://www.googleapis.com/youtube/v3/videos?id={id_of_video}&key={your_api_key}&part=status'
url_get = requests.get(url)
pprint(url_get.json())
I tried the above code (after entering in 'id_of_video' and 'your_api_key' and got an error ..
Quote:url = f'https://www.googleapis.com/youtube/v3/videos?id={id_of_video}&key={your_api_key}&part=status'
^
SyntaxError: invalid syntax
Then noticed the
f, removed it an whilst the script ran okay, this is what was displayed
Quote:{'error': {'code': 400,
'errors': [{'domain': 'usageLimits',
'message': 'Bad Request',
'reason': 'keyInvalid'}],
'message': 'Bad Request'}}
(Feb-16-2020, 05:34 AM)jehoshua Wrote: [ -> ]Then noticed the f
, removed it an whilst the script ran okay, this is what was displayed
I use
f-string
here,then need Python 3.6 or newer.
You should upgrade your Python version.
Here with older string formatting.
import requests
from pprint import pprint
id_of_video = 'CUNq2_VjRn4'
your_api_key = 'xxxxxxxxxxxxxxx'
url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
url_get = requests.get(url)
pprint(url_get.json())
(Feb-16-2020, 06:46 AM)snippsat Wrote: [ -> ]I use f-string
here,then need Python 3.6 or newer.
You should upgrade your Python version.
I'm only on 16.04.6 LTS of Kubuntu and can only go to version 3.5, but found a PPA
Quote:sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt upgrade
sudo apt install python3.7
It seems I now have 3 versions of python running
Quote:$ python -V
Python 2.7.12
$ python2 -V
Python 2.7.12
$ python3 -V
Python 3.5.2
$ python3.7 -V
Python 3.7.6
I assume there is a method to set the default version to 3.7.6 so that running any scripts I can simply do this ..
Quote:python script.py
(Feb-16-2020, 06:46 AM)snippsat Wrote: [ -> ]Here with older string formatting.
import requests
from pprint import pprint
id_of_video = 'CUNq2_VjRn4'
your_api_key = 'xxxxxxxxxxxxxxx'
url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
url_get = requests.get(url)
pprint(url_get.json())
Thanks, I will try that with vers 3.5 and then the
f -string
with version 3.6
(Feb-16-2020, 08:53 AM)jehoshua Wrote: [ -> ]I assume there is a method to set the default version to 3.7.6 so that running any scripts I can simply do this ..
Yes can set alias or mess with update-alternatives
link.
Or just use
python3.7 my_script.py
to run and install with eg
pip3.7 install requests
.
I use
pyenv Simple Python Version Management then this done automatically to chosen version.
(Feb-16-2020, 04:15 PM)snippsat Wrote: [ -> ]Yes can set alias or mess with update-alternatives link.
Or just use python3.7 my_script.py
to run and install with eg pip3.7 install requests
.
I use pyenv Simple Python Version Management then this done automatically to chosen version.
I tried everything as per the
pyenv Simple Python Version Management thread, but it still would not default as per expected. I would assume this is because I'm using a PPA to force to Python 3.7.6 , and for some reason, Kubuntu just isn't recognising it. It's on my 'to do' list to upgrade to the latest Kubuntu, so will try it again then. Thanks very much for your help.
Just a fyi, the output from bash is at
https://paste.ubuntu.com/p/9HYXYBS6jW/
jehoshua Wrote::~$ pyenv global 3.7.2
pyenv: version `3.7.2' not installed
You most first install a version and then set it as global.
Example if i want to use 3.8.1 on my Linux Mint 19.
# Look at available versions
tom@tom:~$ pyenv install --list
.....
3.7.5
3.7.5rc1
3.7.6
3.8.0
3.8-dev
3.8.1
3.9-dev
# Install
tom@tom:~$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/tom/.pyenv/versions/3.8.1
# Set as system wide version
tom@tom:~$ pyenv global 3.8.1
# Test
tom@tom:~$ python
Python 3.8.1 (default, Feb 11 2020, 09:18:58)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(walrus := True)
True
>>> exit()
tom@tom:~$ pip -V
pip 19.2.3 from /home/tom/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)