Skip to content

Instantly share code, notes, and snippets.

@panchicore
Last active July 20, 2016 21:12
Show Gist options
  • Save panchicore/8257f857b7173c361efa8f7ce5ebf366 to your computer and use it in GitHub Desktop.
Save panchicore/8257f857b7173c361efa8f7ce5ebf366 to your computer and use it in GitHub Desktop.
is youtube video blocked in youtube? python script, requires "pip install requests"

In [7]: youtube_video_is_blocked("tMhzINs6DjA") Out[7]: (False, None)

In [8]: youtube_video_is_blocked("2VfMafuY6o8") Out[8]: (True, [u'CO'])

import requests
def youtube_video_is_blocked(video_id):
"""
is the video bloked in youtube?
:param video_id: like "tMhzINs6DjA"
:return: tuple, if blocked: True, list of country codes where video is blocked, else: False, None.
"""
params = {
"part": "contentDetails",
"id": video_id,
"key": "YOUR_API_KEY"
}
url = "https://www.googleapis.com/youtube/v3/videos"
res = requests.get(url, params=params)
items = res.json().get("items")
for v in items:
if "regionRestriction" in v["contentDetails"]:
blocked_in = v["contentDetails"]["regionRestriction"]["blocked"]
if len(blocked_in) > 0:
return True, blocked_in
return False, None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment