-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube.py
More file actions
60 lines (50 loc) · 2.21 KB
/
Copy pathyoutube.py
File metadata and controls
60 lines (50 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
basic youtube functionalities to search for a video and add it to a playlist
Kyle Konrad 7/20/2011
"""
import urllib, urllib2, re
from BeautifulSoup import BeautifulStoneSoup
import gdata.youtube
import gdata.youtube.service
class Youtube():
api_playlist_base = 'http://gdata.youtube.com/feeds/api/playlists/'
playlist_base = 'http://youtube.com/playlist?list=PL'
authsubrequest_url = 'https://www.google.com/accounts/AuthSubRequest'
def __init__(self, developer_key):
self.yt_service = gdata.youtube.service.YouTubeService()
self.yt_service.ssl = True
self.yt_service.client_id = 'AlbumFinder'
self.yt_service.developer_key = developer_key
def query(self, search_term):
"""search youtube
returns id of first result"""
query = gdata.youtube.service.YouTubeVideoQuery()
query.vq = search_term
feed = self.yt_service.YouTubeQuery(query)
return feed.entry[0].id.text if len(feed.entry) > 0 else None
def add_playlist(self, title, summary):
"""create a playlist and return its ID"""
raw_xml = self.yt_service.AddPlaylist(title, summary)
match = re.search('<ns0:id>.*/([^/]+)</ns0:id>', str(raw_xml))
if match:
return match.group(1)
else:
raise Exception('could not parse playlist URL')
def upgrade_token(self, one_time_token):
"""upgrade a one-time token to a session token"""
self.yt_service.SetAuthSubToken(one_time_token)
try:
session_token = self.yt_service.UpgradeToSessionToken()
except Exception as e:
return 'failed to created session token: %s' % str(e)
def add_video_to_playlist(self, video_id, playlist_id):
playlist_url = Youtube.api_playlist_base + playlist_id
video_entry = self.yt_service.AddPlaylistVideoEntryToPlaylist(playlist_url, video_id)
#TODO: check success
def get_authsub_url(self, next):
"""generate URL to get youtube AuthSub token
returns a string"""
scope = 'http://gdata.youtube.com'
secure = False
session = True
return str(self.yt_service.GenerateAuthSubURL(next, scope, secure, session))