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
|
# -*- 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 Title(db.Model):
title = db.StringProperty(required=True)
code = db.StringProperty(required=True)
actors = db.StringListProperty()
characters = db.StringListProperty()
lastThumb = db.IntegerProperty()
class Answer(db.Model):
title = db.ReferenceProperty(Title)
answer = db.ListProperty(int)
class NewQuestion(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
titles = Title.all()
title = titles[randint(0, titles.count() - 1)]
url = "http://img.anyclip.com/thumbnails/%s/tmb_%s_480.jpg" % (title.code, randint(1, title.lastThumb / 10) * 10) #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\",\"code\":\"%s\",\"frame\":\"%s\",\"cast\":[%s]}" % (title.title, title.code, url, cast))
class AnswerQuestion(webapp.RequestHandler):
def get(self):
code = self.request.get("code")
answer = self.request.get("inframe")
if code == "" or answer == "":
return
answers = answer.split(",")
for i in range(len(answers)):
try:
answers[i] = int(answers[i])
if (answers[i] > 9):
return
except:
return
titleQuery = Title.all().filter('code = ', code).fetch(1)
if len(titleQuery) != 1 or len(answers) == 0 or (answers[i] == -1 and len(answers) > 1):
return
answer = Answer()
answer.title = titleQuery[0]
answer.answer = answers;
answer.put()
self.response.out.write(str(answers) + "<br>" + code);
class LoadNewTitles(webapp.RequestHandler):
def __init__(self):
self.api = AnyClipAPI("CAD58B9E-F045-492F-81B9-22CFE6B00604")
def getCast(self, imdbCode):
imdbList = fetch("http://www.imdb.com/title/tt%s/fullcredits" % imdbCode)
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" % imdbCode)
if imdbList.status_code != 200:
return None
i = 0
actors = []
characters = []
index = 0
htmlRemove = re.compile("<.*?>")
while True:
index = imdbList.content.find("<td class=\"nm\">", index) + 15
end = imdbList.content.find("</td>", index)
if index < 15 or end < 0:
break
actor = htmlRemove.sub("", imdbList.content[index:end])
index = imdbList.content.find("<td class=\"ddd\"> ... </td><td class=\"char\">", end) + 43
end = imdbList.content.find("</td>", index)
if index < 43 or end < 0:
break
for character in htmlRemove.sub("", imdbList.content[index:end]).split(" / "):
actors.append(actor)
characters.append(character)
i += 1
if i == 10:
break
return (actors, characters)
def get(self):
thumbListing = fetch("http://anyclip.zx2c4.com/thumblisting.py")
if thumbListing.status_code != 200:
self.response.out.write("error")
return
movies = thumbListing.content.split("\n")
for movie in movies:
codeThumb = movie.split(" ")
if len(codeThumb) != 2:
continue
code = codeThumb[0]
lastThumb = int(codeThumb[1])
if Title.all().filter('code = ', code).count() == 0:
item = self.api.request("title", (code,))
if len(item) == 2 and item[0] == '1005':
self.response.out.write("Could not add invalid code %s<br>" % code)
continue
self.response.out.write("Adding %s<br>" % item["Name"])
newTitle = Title(title=item["Name"], code=item["Code"])
newTitle.lastThumb = lastThumb
cast = self.getCast(item["ImdbID"])
if cast == None:
self.response.out.write("Could not add cast of %s<br>" % item["Name"])
continue
newTitle.actors = cast[0]
newTitle.characters = cast[1]
newTitle.put()
application = webapp.WSGIApplication(
[('/newquestion', NewQuestion),
('/loadnewtitles', LoadNewTitles),
('/answerquestion', AnswerQuestion)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
|