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
|
# -*- coding: iso-8859-1 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api.urlfetch import fetch
from api import AnyClipAPI
import re
from time import sleep
from random import randint
class NewQuestion(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
titles = Title.all()
title = titles[randint(0, titles.count() - 1)]
if len(title.actors) == 0:
return self.get()
url = "http://img.anyclip.com/thumbnails/%s/tmb_%s_480.jpg" % (title.code, "910") #frame hard-coded for now
cast = ""
for i in range(len(title.actors)):
if i != 0:
cast += ","
cast += "[\"%s\",\"%s\"]" % (title.actors[i], title.characters[i])
self.response.out.write("{title:\"%s\",frame:\"%s\",cast:[%s]}" % (title.title, url, cast))
class Title(db.Model):
title = db.StringProperty(required=True)
code = db.StringProperty(required=True)
actors = db.StringListProperty()
characters = db.StringListProperty()
class LoadNewTitles(webapp.RequestHandler):
def get(self):
api = AnyClipAPI("CAD58B9E-F045-492F-81B9-22CFE6B00604")
index = 0
imdbPattern = re.compile("<td class=\"nm\"><a href=\"/name/nm[0-9]{7}/\" onclick=\"\\(new Image\\(\\)\\)\\.src='/rg/castlist/position-[0-9]{1,2}/images/b\\.gif\\?link=/name/nm[0-9]{7}/';\">([^><]+)</a></td><td class=\"ddd\"> \\.\\.\\. </td><td class=\"char\">(?:<a href=\"/character/ch[0-9]{7}/\">([^><]+)</a>[^><]*|([^><]*))</td>")
while True:
response = api.request("titles", ("recent", index, 20))
index += 20
for item in response["Items"]:
if Title.all().filter('code = ', item["Code"]).count() == 0:
self.response.out.write("Adding %s<br>" % item["Name"])
newTitle = Title(title=item["Name"], code=item["Code"])
imdbList = fetch("http://www.imdb.com/title/tt%s/fullcredits" % item["ImdbID"])
i = 0
while imdbList.status_code != 200:
i += 1
if i == 5:
break
sleep(3)
imdbList = fetch("http://www.imdb.com/title/tt%s/fullcredits" % item["ImdbID"])
if imdbList.status_code != 200:
self.response.out.write("Could not add cast of %s<br>" % item["Name"])
continue
i = 0
for match in imdbPattern.finditer(imdbList.content):
newTitle.actors.append(match.group(1))
character = match.group(2)
if character == None:
character = match.group(3)
newTitle.characters.append(character)
i += 1
if i == 10:
break
newTitle.put()
else:
index = response["TotalItemCount"]
break
if index >= response["TotalItemCount"]:
break
application = webapp.WSGIApplication(
[('/newquestion', NewQuestion),
('/loadnewtitles', LoadNewTitles)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
|