Python Forum
How to check if video has been deleted or removed in youtube using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if video has been deleted or removed in youtube using python
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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
Reply
#5
(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'}}
Reply
#6
(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())
Reply
#7
(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
Reply
#8
(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.
Reply
#9
(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/
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make a bot in pycharm for youtube for automatically adding python comments kodek2222 1 99 Jan-21-2021, 12:47 PM
Last Post: Aspire2Inspire
  How to preserve x-axis labels despite deleted subplot? Mark17 1 149 Dec-23-2020, 09:02 PM
Last Post: Mark17
  Openpyxl: Excel formula & condition formatting removed JaneTan 0 242 Sep-25-2020, 07:02 AM
Last Post: JaneTan
  Why is one duplicate not removed? Emekadavid 4 458 Jun-09-2020, 06:34 PM
Last Post: perfringo
  How to get Data-Taken For a Video file using Python ramprasad1211 1 458 Apr-28-2020, 08:48 AM
Last Post: ndc85430
  Get list of Video Device in python on Windows machine Michal 1 694 Apr-03-2020, 06:57 PM
Last Post: Mateusz
  Youtube video transcripts jehoshua 2 970 Feb-15-2020, 09:55 PM
Last Post: jehoshua
  Python Error with youtube-dl - Any Help? abrogard 4 2,498 Sep-30-2019, 10:01 PM
Last Post: snippsat
  How to get python to download YouTube videos in the background? Pythenx 11 3,274 Mar-25-2019, 04:57 AM
Last Post: samsonite
  Position video in X Y in python sigmahokies 0 736 Feb-05-2019, 01:18 PM
Last Post: sigmahokies

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020