-
Notifications
You must be signed in to change notification settings - Fork 0
/
albumFinder.py
executable file
·139 lines (113 loc) · 4.38 KB
/
albumFinder.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
"""
AlbumFinder v0.2.3
AlbumFinder is a bare-bones web app to create YouTube playlists for albums
Album info in fetched from Wikipedia
TODO: make track name parsing from wikipedia more robust.
beter yet, get track names from amazon instead
"""
version = '0.2.3'
# api imports
import amazonproduct
import gdata.youtube
import gdata.service
# local imports
import amazon as amzn
import youtube as yt
import logger
# python imports
import urllib, urlparse, re, web
from datetime import datetime
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('albumFinder.cfg')
youtube = yt.Youtube(config.get('youtube', 'developer_key'))
amazon = amzn.Amazon(config.get('amazon', 'aws_key'), config.get('amazon', 'secret_key'))
urls = ['/', 'Index',
'/create_playlist', 'CreatePlaylist',
'/show_playlist', 'ShowPlaylist',
'/favicon.ico', 'Icon'
]
error_messages = [] # global array to hold error messages
app = web.application(urls, globals())
render = web.template.render('templates/')
class CreatePlaylist():
def GET(self):
"""get one-time token from youtube, exchange for session token"""
params = urlparse.parse_qs(web.ctx.query[1:])
if 'token' not in params.keys():
return 'No youtube session token given'
one_time_token = params['token'][0]
youtube.upgrade_token(one_time_token)
if 'artist' not in params.keys():
return "please enter an artist"
if 'album' not in params.keys():
return "please enter an album"
artist = params['artist'][0]
album = params['album'][0]
logger.log_request("%s, %s, %s, %s" % (datetime.now(), web.ctx.ip, artist, album))
title = "%s %s" % (artist, album)
summary = "%s by %s. Playlist generated by AlbumFinder" % (album, artist)
try:
album_videos = list(get_album_videos(artist, album))
except amazonproduct.api.NoExactMatchesFound as e:
logger.log_error("not found: %s, %s" % (artist, album))
error_messages.append('Sorry, that album could not be found.')
return web.seeother('/')
try:
playlist_id = youtube.add_playlist(title, summary)
for video_id in album_videos:
youtube.add_video_to_playlist(video_id, playlist_id)
web.seeother("/show_playlist?playlist_id=%s" % playlist_id)
except gdata.service.RequestError as e:
if re.search('Playlist already exists', str(e)):
logger.log_error("already exists: %s" % title)
error_messages.append('Sorry, that playlist already exists.')
# get playlist id and show it
else:
logger.log_error(str(e))
raise e
web.seeother('/')
POST = GET
class ShowPlaylist:
def GET(self):
params = urlparse.parse_qs(web.ctx.query[1:])
if 'playlist_id' not in params.keys():
return 'No playlist id given'
playlist_id = params['playlist_id'][0]
return render.playlist(playlist_id, error_messages)
album_artist_form = web.form.Form(
web.form.Textbox('Artist', web.form.notnull),
web.form.Textbox('Album', web.form.notnull)
)
class Index():
def GET(self):
global error_messages
temp = error_messages
error_messages = []
form = album_artist_form()
return render.index(form, temp)
def POST(self):
global error_messages
temp = error_messages
error_messages = []
form = album_artist_form()
if not form.validates():
return render.index(form, temp)
else:
playlist_params = urllib.urlencode({'album' : form.Album.value, 'artist' : form.Artist.value})
authsub_url = youtube.get_authsub_url('%s/create_playlist?%s' % (web.ctx.homedomain, playlist_params))
web.seeother(authsub_url)
class Icon():
def GET(self):
web.header('Content-Type', 'image/x-icon')
return open('favicon.ico', 'rb').read()
def get_album_videos(artist, album):
"""helper function to get track names from amazon and corresponding videos from youtube"""
tracks = amazon.get_tracks(album, artist)
for track in tracks:
video_id = youtube.query("%s %s" % (artist, track))
if video_id:
yield video_id
if __name__ == '__main__':
app.run()