-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
335 lines (300 loc) · 9.02 KB
/
main.py
File metadata and controls
335 lines (300 loc) · 9.02 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
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
# -*- coding:utf-8 -*-
import cgi, os, mimetypes, urllib, datetime, time
import video
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app, login_required
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from appengine_utilities.sessions import Session
class VideoCollection(db.Model):
owner = db.UserProperty(auto_current_user=True)
keyword = db.StringProperty()
yt_id = db.StringProperty()
yt_name = db.StringProperty()
yt_pswd = db.StringProperty()
pswd = db.StringProperty()
fetch_count = db.IntegerProperty()
home_url = db.LinkProperty()
@staticmethod
def get_collection(user):
q = VideoCollection.gql(u'WHERE owner = :1', user)
if q.count() == 1:
return q.get()
for col in q:
col.delete()
return None
@staticmethod
def get_by_keyword(keywd, pswd):
q = VideoCollection.gql(u'WHERE keyword = :1 AND pswd = :2', keywd, pswd)
if q.count() == 1:
return q.get()
return None
def gamelists(self):
return GameList.gql(u'WHERE coll = :1 ORDER BY description DESC', self.key())
class GameList(db.Model):
coll = db.ReferenceProperty(VideoCollection)
description = db.StringProperty()
def games(self):
return Game.gql(u'WHERE games = :1 ORDER BY title', self.key())
@staticmethod
def get_by_description(descr, vc):
q = GameList.gql(u'WHERE description = :1', descr)
if q.count() == 1:
return q.get()
else:
ret = GameList()
ret.description = descr
ret.coll = vc
ret.put()
return ret
class Game(db.Model):
games = db.ReferenceProperty(GameList)
title = db.StringProperty()
thumbnail = db.LinkProperty()
width = db.IntegerProperty()
height = db.IntegerProperty()
def has_next(self):
return self.next() is not None
def has_prev(self):
return self.prev() is not None
def next(self):
found = False
for g in Game.gql(u'WHERE games = :1 ORDER BY title', self.games.key()):
if found: return g
if g.title == self.title:
found = True
return None
def prev(self):
found = None
for g in Game.gql(u'WHERE games = :1 ORDER BY title', self.games.key()):
if g.title == self.title:
return found
found = g
return None
def entries(self):
return VideoEntry.gql(u'WHERE game = :1 ORDER BY number', self.key())
def thumbs(self):
ret = []
for e in self.entries():
for t in e.thumbs():
ret.append(t)
return ret
@staticmethod
def get_by_title(games, title, entry):
num = None
if title.endswith(u'Q'):
x, y = title.rsplit(u' ', 1)
num = int(y[:-1])
title = x
q = Game.gql(u'WHERE title = :1 AND games = :2', title, games)
if q.count() == 1:
return q.get(), num
else:
ret = Game()
ret.games = games
ret.title = title
ret.thumbnail = entry[u'thumbnail']
ret.width = int(entry[u'width'])
ret.height = int(entry[u'height'])
ret.put()
return ret, num
class VideoEntry(db.Model):
game = db.ReferenceProperty(Game)
videoid = db.StringProperty()
number = db.IntegerProperty()
url = db.LinkProperty()
thumbnail = db.LinkProperty()
thumbnail1 = db.LinkProperty()
thumbnail2 = db.LinkProperty()
thumbnail3 = db.LinkProperty()
width = db.IntegerProperty()
height = db.IntegerProperty()
hqthumbnail = db.LinkProperty()
hqwidth = db.IntegerProperty()
hqheight = db.IntegerProperty()
@staticmethod
def has_entry(videoid):
q = VideoEntry.gql(u'WHERE videoid = :1', videoid)
return q.count() == 1
def embed(self):
return u'http://www.youtube.com/embed/' + self.videoid
def thumbs(self):
return [self.thumbnail1, self.thumbnail2, self.thumbnail3]
class VideoList(webapp.RequestHandler):
def __show_list(self):
if self.sess.has_key(u'vc'):
vc = VideoCollection.get(self.sess[u'vc'])
pos = int(self.request.get(u'pos', u'0'))
q = vc.gamelists()
if pos < vc.fetch_count:
prev = None
has_prev = False
else:
has_prev = True
prev = pos - vc.fetch_count
if pos + vc.fetch_count < q.count():
next = pos + vc.fetch_count
else:
next = None
template_values = {
u'title' : vc.keyword + u'試合内容',
u'pageid' : unicode(vc.key())+unicode(pos),
u'has_prev' : has_prev,
u'prev' : prev,
u'next' : next,
u'home' : vc.keyword,
u'home_url' : vc.home_url,
u'gamelists': q.fetch(offset=pos, limit=vc.fetch_count)
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
else:
path = os.path.join(os.path.dirname(__file__), 'login.html')
self.response.out.write(template.render(path, {
u'title' : u'ログイン',
u'home' : u'トップ画面へ',
u'home_url' : u'/',
u'pageid' : u'loginscreen'
}))
def get(self):
self.sess = Session()
self.__show_list()
def post(self):
self.sess = Session()
vc = VideoCollection.get_by_keyword(
self.request.get(u'keyword'), self.request.get(u'pswd'))
if vc:
self.sess[u'vc'] = vc.key()
time.sleep(1)
self.__show_list()
class GameView(webapp.RequestHandler):
def get(self):
game = Game.get(self.request.get(u'game'))
self.sess = Session()
if self.sess.has_key(u'vc') and self.sess[u'vc'] == game.games.coll.key():
path = os.path.join(os.path.dirname(__file__), 'game.html')
self.response.out.write(template.render(path, {
u'title' : game.title,
u'home' : game.games.coll.keyword,
u'home_url' : game.games.coll.home_url,
u'pageid' : game.key(),
u'game': game
}))
else:
del self.sess[u'vc']
self.redirect(u'/')
class Config(webapp.RequestHandler):
def __show_config(self, vc):
path = os.path.join(os.path.dirname(__file__), u'config.html')
if vc is None:
gl = []
else:
gl = vc.gamelists()
self.response.out.write(template.render(path, {
u'title' : u'Youtube収集設定',
u'home' : u'トップ画面へ',
u'home_url' : u'/',
u'pageid' : u'ytcollectpage',
u'gamelists': gl,
u'vc' : vc or VideoCollection()
}))
@login_required
def get(self):
self.__show_config(VideoCollection.get_collection(users.get_current_user()))
def post(self):
vc = VideoCollection.get_collection(users.get_current_user()) or VideoCollection()
vc.keyword = self.request.get(u'keyword')
vc.yt_id = self.request.get(u'yt_id')
vc.yt_name = self.request.get(u'yt_name')
vc.yt_pswd = self.request.get(u'yt_pswd')
vc.pswd = self.request.get(u'pswd')
vc.fetch_count = int(self.request.get(u'count'))
vc.home_url = self.request.get(u'home_url')
vc.put()
self.__show_config(vc)
class Api(webapp.RequestHandler):
@login_required
def get(self):
vc = VideoCollection.get_collection(users.get_current_user())
if vc is None:
self.response.set_status(500)
m = self.request.get(u'method')
if m == u'clear':
for gl in vc.gamelists():
for g in gl.games():
for ve in g.entries():
ve.delete()
g.delete()
gl.delete()
self.response.set_status(200)
if m == u'create':
gl = GameList()
gl.coll = vc;
gl.description = self.request.get(u'name').strip(u'"')
gl.put()
self.response.set_status(200)
elif m == u'scan':
yt = video.CollectYouTube(vc.yt_id, vc.yt_pswd)
for entry in yt.collect(vc.yt_name, vc.keyword):
if VideoEntry.has_entry(entry[u'videoid']):
# 既にアップロード済
continue
games = GameList.get_by_description(entry[u'description'], vc)
game, num = Game.get_by_title(games, entry[u'title'], entry)
ve = VideoEntry()
ve.game = game
ve.videoid = entry[u'videoid']
ve.number = num
ve.url = entry[u'url']
ve.thumbnail = entry[u'thumbnail']
ve.thumbnail1 = entry[u'thumbnail1']
ve.thumbnail2 = entry[u'thumbnail2']
ve.thumbnail3 = entry[u'thumbnail3']
ve.width = int(entry[u'width'])
ve.height = int(entry[u'height'])
ve.hqthumbnail = entry[u'hqthumbnail']
ve.hqwidth = int(entry[u'hqwidth'])
ve.hqheight = int(entry[u'hqheight'])
ve.put()
self.response.set_status(200)
def post(self):
gm = None
for i in range(1, 10):
ve = VideoEntry()
ve.videoid = self.request.get(u'game' + unicode(i))
if len(ve.videoid)==0: continue
ve.number = i
ve.url = u'https://www.youtube.com/watch?v='+ve.videoid+'&feature=youtube_gdata'
ve.height = 90
ve.width = 120
ve.hqheight = 360
ve.hqwidth = 480
ve.thumbnail = u'http://i.ytimg.com/vi/'+ve.videoid+'/default.jpg'
ve.thumbnail1 = u'http://i.ytimg.com/vi/'+ve.videoid+'/1.jpg'
ve.thumbnail2 = u'http://i.ytimg.com/vi/'+ve.videoid+'/2.jpg'
ve.thumbnail3 = u'http://i.ytimg.com/vi/'+ve.videoid+'/3.jpg'
ve.hqthumbnail = u'http://i.ytimg.com/vi/'+ve.videoid+'/hqdefault.jpg'
if gm is None:
gm = Game()
gm.games = GameList.get(self.request.get(u'gamelist'))
gm.title = self.request.get(u'gamename')
gm.thumbnail = ve.thumbnail
gm.width = ve.width
gm.height = ve.height
gm.put()
ve.game = gm
ve.put()
self.redirect(u'/config')
application = webapp.WSGIApplication(
[
('/', VideoList),
('/list', VideoList),
('/game', GameView),
('/config', Config),
('/api', Api),
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()