-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·693 lines (635 loc) · 22.9 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/usr/bin/env python
import os
import cache
import urllib
import hashlib
import datetime
import time
import json
import logging
from models.dj import Permission, Dj
from models.tracks import Album, Song, ArtistName
from models.play import Play, Program
from models.base_models import NoSuchEntry
from models.blog import BlogPost
import amazon
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.ext import blobstore
from google.appengine.runtime import DeadlineExceededError
import webapp2
from webapp2_extras import sessions
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import blobstore_handlers
from passwd_crypto import hash_password
from handlers import BaseHandler, UserHandler
from dj import check_login, login_required
from configuration import webapp2conf
import configuration as conf
def get_path(filename):
return os.path.join(os.path.dirname(__file__), filename)
class MainPage(BaseHandler):
def get(self):
## Album list disabled until it is further optimized.
#album_list = []
album_list = Album.get_new(num=36)
start = datetime.date.today() - datetime.timedelta(days=6)
end = datetime.date.today() + datetime.timedelta(days=1)
song_num = 10
album_num = 10
top_songs, top_albums = Play.get_top(start, end, song_num, album_num)
posts = BlogPost.get_last(num=3)
events = [] #models.getEventsAfter(datetime.datetime.now() -
# datetime.timedelta(days=1), 3)
template_values = {
'news_selected': True,
'flash': self.flashes,
'session': self.session,
'album_list': album_list,
'top_songs': top_songs,
'top_albums': top_albums,
'posts': posts,
'events': events,
}
self.response.out.write(template.render(get_path("index.html"),
template_values))
class DjComplete(BaseHandler):
def get(self):
q = self.request.get("query")
djs = Dj.autocomplete(q)
self.response.out.write(json.dumps({
'query': q,
'suggestions': ["%s - %s"%(dj.fullname, dj.email) for dj in djs],
'data': [{'key': str(dj.key),
'name': dj.fullname,
'email': dj.email} for dj in djs],}))
class ArtistComplete(BaseHandler):
def get(self):
q = self.request.get("query")
artists = ArtistName.autocomplete(q)
self.response.out.write(json.dumps({
'query': q,
'suggestions': [ar.artist_name for ar in artists],
}))
class AlbumTable(BaseHandler):
def post(self):
pass
def dummy(self):
page = self.request.get('page')
try:
page = int(page)
except ValueError:
self.response.out.write(json.dumps({
'err': "Unable to parse requested page."
}))
return
album_table_html = memcache.get("album_table_html")
if album_table_html is None:
albums = models.getNewAlbums(20, page)
template_values = {
'session': self.session,
'album_list': albums,
}
album_table_html = template.render(
get_path("newalbums.html"), template_values)
memcache.set("album_table_html", album_table_html)
self.response.out.write(album_table_html)
class AlbumInfo(BaseHandler):
def get(self):
artist = self.request.get('artist')
album = self.request.get('album')
keywords = self.request.get('keywords')
keywords = (urllib.quote(keywords) + "%20" +
urllib.quote(artist) + "%20" + urllib.quote(album))
# AKIAJIXECWA77X5XX4DQ
# 6oYjAsiXTz8xZzpKZC8zkqXnkYV72CNuCRh9hUsQ
# datetime.datetime.now().strftime("%Y-%m-%dT%H:%M%:%SZ")
items = amazon.productSearch(keywords)
json_data = {'items': [{
'small_pic': (i.getElementsByTagName("SmallImage")[0].
getElementsByTagName("URL")[0].
firstChild.nodeValue),
'large_pic': (i.getElementsByTagName("LargeImage")[0].
getElementsByTagName("URL")[0].
firstChild.nodeValue),
'medium_pic': (i.getElementsByTagName("MediumImage")[0].
getElementsByTagName("URL")[0].
firstChild.nodeValue),
'artist': i.getElementsByTagName("Artist")[0].firstChild.nodeValue,
'title': i.getElementsByTagName("Title")[0].firstChild.nodeValue,
'asin': i.getElementsByTagName("ASIN")[0].firstChild.nodeValue,
'tracks': [t.firstChild.nodeValue
for t in i.getElementsByTagName("Track")],
} for i in items]}
updatehtml = template.render(get_path("addalbumupdate.html"), json_data)
json_data['updatehtml'] = updatehtml
self.response.headers['Content-Type'] = 'text/json'
self.response.out.write(json.dumps(json_data))
class Setup(BaseHandler):
def get(self):
labels = Permission.PERMISSIONS
try:
seth = Dj.get_by_email("[email protected]")
except NoSuchEntry:
seth = Dj.new(fullname='Seth Glickman',
email='[email protected]', username='seth',
password='testme')
seth.put()
program = Program.new(
title='Seth\'s Show', slug='seth',
desc='This is the show where Seth plays his favorite music.',
dj_list=[seth.key],
page_html='a <b>BOLD</b> show!')
program.put()
for l in labels:
try:
permission = Permission.get_by_title(l)
except NoSuchEntry:
permission = Permission.new(l, [])
permission.put()
finally:
if seth.key not in permission.dj_list:
permission.add_dj(seth.key)
permission.put()
if not BlogPost.get_last(num=3):
post1 = BlogPost.new(
title="Blog's first post!",
text="This is really just filler text on the first post.",
slug="first-post", post_date=datetime.datetime.now())
post1.put()
time.sleep(2)
post2 = BlogPost.new(
title="Blog's second post!",
text="More filler text, alas.",
slug="second-post", post_date=datetime.datetime.now())
post2.put()
contactspage = BlogPost.new(
title="Contacts Page",
text="This is a dummy stub for the contacts page. Lorem ipsum whatnot",
slug="contacts-page", post_date=datetime.datetime.now())
contactspage.put()
artists = [
"Bear In Heaven",
"Beck",
"Arcade Fire",
"Andrew Bird",
"The Antlers",
"Arcade Fire",
"The Beach Boys",
"Brian Wilson",
"The Beatles",
"Beethoven",
"Beirut",
"Belle & Sebastian",
"Benji Hughes",
"Club 8",
"Crayon Fields",
]
for a in artists:
if not (ArtistName._RAW.query()
.filter(ArtistName._RAW.artist_name == a)
.fetch(1, keys_only=True)):
ar = ArtistName.new(artist_name=a)
ar.put()
self.session.add_flash("Permissions set up, ArtistNames set up, "
"Blog posts set up, DJ Seth entered.")
self.redirect('/')
class BlogDisplay(BaseHandler):
def get(self, date_string, post_slug):
post_date = datetime.datetime.strptime(date_string, "%Y-%m-%d")
post = models.getPostBySlug(post_date, post_slug)
if not post:
self.session.add_flash(
"The post you requested could not be found. Please try again.")
self.redirect('/')
return
template_values = {
'session': self.session,
'post': post,
}
self.response.out.write(template.render("blog_post.html", template_values))
class ViewCoverHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, cover_key):
if not cover_key:
self.error(404)
cover_key = str(urllib.unquote(cover_key))
self.response.headers["Cache-Control"] = "public"
expires_date = datetime.datetime.utcnow() + datetime.timedelta(365)
expires_str = expires_date.strftime("%d %b %Y %H:%M:%S GMT")
self.response.headers.add_header("Expires", expires_str)
self.send_blob(cover_key)
"""class AlbumDisplay(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, key_id, size):
self.response.headers["Cache-Control"] = "max-age=604800"
album = cache.getAlbum(key_id)
if not album:
return
if size == "l":
self.send_blob(album.cover_large)
else:
self.send_blob(album.cover_small)"""
class UpdateInfo(webapp2.RequestHandler):
def get(self):
recent_songs = Play.get_last(num=3)
if recent_songs is not None and len(recent_songs) > 0:
last_play = recent_songs[0]
song, program = (last_play.song,
last_play.program)
logging.error(song.raw)
song_string = song.title
artist_string = song.artist
if program is not None:
program_title, program_desc, program_slug = (program.title,
program.desc,
program.slug)
else:
program_title, program_desc, program_slug = ("no show",
"No description",
"")
else:
song, program = None, None
song_string = "nothing"
artist_string = "nobody"
program_title, program_desc, program_slug = ("no show",
"No description",
"")
self.response.headers["Content-Type"] = "text/json"
self.response.out.write(json.dumps({
'song_string': song_string,
'artist_string': artist_string,
'program_title': program_title,
'program_desc': program_desc,
'program_slug': program_slug,
'top_played': ("Top artists: " + ", ".join(
[a[0] for a in program.top_artists[:3]]) if
program is not None else None),
'recent_songs_html': template.render(
get_path("recent_songs.html"), {'plays': recent_songs}),
}))
class CallVoice(webapp2.RequestHandler):
def get(self):
url = "https://clients4.google.com/voice/embed/webButtonConnect"
form_fields = {
'buttonId': self.request.get("button_id"),
'callerNumber': self.request.get("cid_number"),
'name': self.request.get("cid_name"),
'showCallerNumber': "1",
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(
url=url,
payload=form_data,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
self.response.out.write(result)
class SongList(BaseHandler):
def get(self):
self.response.headers["Content-Type"] = "text/json"
album_key = self.request.get("album_key")
songlist_html = memcache.get("songlist_html_" + album_key)
if songlist_html:
self.response.out.write(json.dumps({
'songListHtml': songlist_html,
'generated': 'memcache',
}))
return
album = Album.get(album_key, one_key=True)
self.response.headers["Content-Type"] = "text/json"
if not album:
self.response.out.write(json.dumps({
'err': ("An error occurred and the specified album could "
"not be found. Please try again.")
}))
return
songlist_html = template.render(get_path("ajax_songlist.html"), {
'songList': Song.get(album.tracklist),
})
memcache.set("songlist_html_" + album_key, songlist_html)
self.response.out.write(json.dumps({
'songListHtml': template.render(get_path("ajax_songlist.html"), {
'songList': Song.get(album.tracklist),
}),
'generated': 'generated',
}))
class EventPage(BaseHandler):
def get(self):
start_date = datetime.datetime.now() - datetime.timedelta(days=2)
events = models.getEventsAfter(start_date)
template_values = {
'events_selected': True,
'session': self.session,
'events': events,
}
self.response.out.write(template.render(get_path("events.html"),
template_values))
class SchedulePage(BaseHandler):
def get(self):
template_values = {
'schedule_selected': True,
'session': self.session,
}
self.response.out.write(template.render(get_path("schedule.html"),
template_values))
class PlaylistPage(BaseHandler):
def get(self):
shows = Program.get(num=1000) # TODO: NOPE
slug = self.request.get("show")
datestring = self.request.get("programdate")
selected_date = None
if datestring:
try:
selected_date = datetime.datetime.strptime(datestring, "%m/%d/%Y")
selected_date = selected_date + datetime.timedelta(hours=12)
except:
self.session.add_flash("The date provided could not be parsed.")
self.redirect("/")
return
if slug:
selected_program = Program.get(slug=slug)
if not selected_program:
self.session.add_flash("There is no program for slug %s." % slug)
self.redirect("/")
return
if selected_date:
plays = models.getPlaysBetween(program=selected_program,
after=(selected_date -
datetime.timedelta(hours=24)),
before=(selected_date +
datetime.timedelta(hours=24)))
else:
lastplay = Play.get_last(program=selected_program, num=1)
if lastplay:
lastplay = lastplay[0]
last_date = lastplay.play_date
plays = models.getPlaysBetween(program=selected_program,
after=(last_date -
datetime.timedelta(days=1)))
else:
plays = []
else:
if not selected_date:
lastplay = Play.get_last()
if lastplay:
selected_date = lastplay.play_date
if selected_date:
plays = models.getPlaysForDate(selected_date)
else:
plays = Play.get_last(num=60)
template_values = {
'playlists_selected': True,
'session': self.session,
'plays': plays,
'shows': shows,
}
self.response.out.write(template.render(get_path("playlist.html"),
template_values))
class PlaylistExport(BaseHandler):
@login_required
def get(self):
import csv
shows = models.getPrograms()
slug = self.request.get("show")
datestring = self.request.get("programdate")
selected_date = None
if datestring:
try:
selected_date = datetime.datetime.strptime(datestring, "%m/%d/%Y")
selected_date = selected_date + datetime.timedelta(hours=12)
print selected_date.isoformat(" ")
except:
self.session.add_flash("The date provided could not be parsed.")
self.redirect("/")
return
if slug:
selected_program = models.getProgramBySlug(slug)
if not selected_program:
self.session.add_flash("There is no program for slug %s." % slug)
self.redirect("/")
return
if selected_date:
plays = models.getPlaysBetween(program=selected_program,
after=(selected_date -
datetime.timedelta(hours=24)),
before=(selected_date +
datetime.timedelta(hours=24)))
else:
lastplay = Play.get_last(program=selected_program, num=1)
if lastplay:
lastplay = lastplay[0]
last_date = lastplay.play_date
plays = models.getPlaysBetween(program=selected_program,
after=(last_date -
datetime.timedelta(days=1)))
else:
plays = []
else:
if selected_date is None:
lastplay = Play.get_last()
if lastplay:
selected_date = lastplay.play_date
if selected_date:
print "doop"
print selected_date.isoformat(" ")
plays = models.getRetardedNumberOfPlaysForDate(selected_date)
else:
plays = Play.get_last(num=60)
csv_sep = "\t"
out_data = [("Date", "Time", "Title", "Artist")]
for p in plays:
s = cache.getSong(p.song_key)
out_data.append((p.play_date.isoformat(csv_sep), s.title, s.artist))
self.response.out.write("\n".join([csv_sep.join(row) for row in out_data]))
class FunPage(BaseHandler):
def get(self):
template_values = {
'session': self.session,
}
self.response.out.write(template.render(get_path("fun.html"),
template_values))
class ChartsPage(UserHandler):
@check_login
def get(self):
start = datetime.date.today() - datetime.timedelta(days=6)
end = datetime.date.today() + datetime.timedelta(days=1)
song_num = 30
album_num = 30
songs, albums = Play.get_top(start, end, song_num, album_num)
template_values = {
'charts_selected': True,
'session': self.session,
'flash': self.flashes,
'songs': songs,
'albums': albums,
'start': start,
'end': end,
'login': self.dj_login,
}
self.response.out.write(
template.render(get_path("charts.html"), template_values))
@check_login
def post(self):
song_num = 30
album_num = 30
# Parse dates
start_date_req = self.request.get("start_date")
if start_date_req:
try:
start = datetime.datetime.strptime(start_date_req, "%m/%d/%Y").date()
except ValueError:
self.session.add_flash(
"Unable to parse start date. Defaulting to a week ago, today.")
start = datetime.date.today() - datetime.timedelta(days=6)
else:
start = datetime.date.today() - datetime.timedelta(days=6)
end_date_req = self.request.get("end_date")
if end_date_req:
try:
end = datetime.datetime.strptime(end_date_req, "%m/%d/%Y").date()
except ValueError:
self.session.add_flash(
"Couldn't parse end date. Defaulting to a week of charts.")
end = start + datetime.timedelta(weeks=1)
else:
end = start + datetime.timedelta(weeks=1)
if self.request.get("song_num"):
song_num = int(self.request.get("song_num"))
if self.request.get("album_num"):
album_num = int(self.request.get("album_num"))
songs, albums = Play.get_top(start, end, song_num, album_num)
template_values = {
'charts_selected': True,
'session': self.session,
'flash': self.flashes,
'songs': songs,
'albums': albums,
'start': start,
'end': end,
'login': self.dj_login,
}
self.response.out.write(
template.render(get_path("charts.html"), template_values))
class HistoryPage(BaseHandler):
def get(self):
template_values = {
'history_selected': True,
'session': self.session,
}
self.response.out.write(
template.render(get_path("history.html"), template_values))
class ContactPage(BaseHandler):
def get(self):
# TODO: Please please fix this. Although realistically it will be fixed on
# models rework in future.
# So general MGMT can edit contacts page
contacts = memcache.get("contacts_page_html")
if contacts is None:
contacts = models.BlogPost.all().filter("slug =", "contacts-page").get()
cache.mcset_t(contacts, 3600, "contacts_page_html")
template_values = {
'contact_selected': True,
'session': self.session,
'contacts': contacts
}
self.response.out.write(
template.render(get_path("contact.html"), template_values))
class ProgramPage(BaseHandler):
def get(self, slug):
program = models.getProgramBySlug(slug)
posts = models.getLastPosts(1)
if not program:
self.session.add_flash("Invalid program slug specified.")
self.redirect("/")
return
template_values = {
'session': self.session,
'flash': self.flashes,
'program': program,
'djs' : (tuple(Dj.get(dj)
for dj in program.dj_list) if program.dj_list
else None),
'posts': posts,
}
self.response.out.write(
template.render(get_path("show.html"), template_values))
class TestModels(BaseHandler):
def get(self):
from models._raw_models import Play
self.response.out.write("hey there")
key = Play.query().get(keys_only=True)
self.response.out.write(key)
play = key.get()
self.response.out.write(play)
## There should never be a need to use the following handler in the future.
# However, it remains for educational purposes.
# Be aware that it's somewhat hackishly written
class SecretPortCoversPage(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
cursor = self.request.get('cursor', None)
query = models.Album.all()
if cursor:
logging.debug("cursor get!")
query.with_cursor(start_cursor=cursor)
album = query.get()
if album:
new_cursor = query.cursor()
new_url = '/secret12345qwerty?cursor=%s' % new_cursor
album = album
try:
models.moveCoverToBlobstore(album)
except:
pass
self.response.out.write("""
<html>
<head>
<meta http-equiv="refresh" content="0;url=%s"/>
</head>
<body>
<h3>Update Datastore</h3>
<ul>
<li>Cursor: %s</li>
<li>LastCursor: %s</li>
<li>Updated: %s</li>
</ul>
</body>
</html>
"""%(new_url, new_cursor, cursor, album.title))
return
self.response.out.write("Done")
def profile_main():
# This is the main function for profiling
# We've renamed our original main() above to real_main()
import cProfile, pstats, StringIO
prof = cProfile.Profile()
prof = prof.runctx("real_main()", globals(), locals())
stream = StringIO.StringIO()
stats = pstats.Stats(prof, stream=stream)
stats.sort_stats("time") # Or cumulative
stats.print_stats(80) # 80 = how many to print
# The rest is optional.
# stats.print_callees()
# stats.print_callers()
logging.info("Profile data:\n%s", stream.getvalue())
app = webapp2.WSGIApplication([
('/', MainPage),
('/updateinfo/?', UpdateInfo),
('/ajax/albumtable/?', AlbumTable),
('/ajax/albuminfo/?', AlbumInfo),
('/ajax/artistcomplete/?', ArtistComplete),
('/ajax/getSongList/?', SongList),
('/ajax/djcomplete/?', DjComplete),
('/setup/?', Setup),
('/blog/([^/]*)/([^/]*)/?', BlogDisplay),
('/programs?/([^/]*)/?', ProgramPage),
('/schedule/?', SchedulePage),
('/playlists/?', PlaylistPage),
('/playexport/?', PlaylistExport),
('/fun/?', FunPage),
('/charts/?', ChartsPage),
('/history/?', HistoryPage),
('/contact/?', ContactPage),
('/events/?', EventPage),
('/albums/([^/]*)/?', ViewCoverHandler),
('/callvoice/?', CallVoice),
('/testmodels/?', TestModels),
], debug=True, config=webapp2conf)