Created
March 21, 2014 18:32
-
-
Save LinnTroll/9692758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urlparse | |
import urllib | |
# достаем id видео из его url | |
def get_id(url): | |
qs = urlparse.urlparse(url).query | |
qd = dict(urlparse.parse_qsl(qs)) | |
return qd.get('v', None) | |
# проверяем существует ли видео средствами youtube api | |
def check_video(id): | |
api_url = 'https://gdata.youtube.com/feeds/api/videos/{id}?v=2'.format(id=id) | |
result = urllib.urlopen(api_url).read() | |
return not '<code>ResourceNotFoundException</code>' in result | |
id1 = get_id('https://www.youtube.com/watch?v=R80TklDqOS4') # существующее видео | |
print check_video(id1) # вернет True | |
id2 = get_id('https://www.youtube.com/watch?v=R80TklDqOS1') # несуществующее видео | |
print check_video(id2) # вернет False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment