-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
388 lines (292 loc) · 10.2 KB
/
models.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
import requests
import time
import os
from hashlib import md5
from bs4 import BeautifulSoup
from collections import OrderedDict
from utils import remove_special, word_count, identity
from natsort import natsorted
from operator import attrgetter
import pickle
try:
import simplejson as json
except ImportError:
import json
class JSONSerializable:
serializable = True
hidden = []
def to_json(self):
if self.serializable:
o = self.__dict__
for k in self.hidden:
o.pop(k, None)
return o
else:
return None
class IterableWrapper:
iterable = None
def __iter__(self):
return getattr(self, self.iterable).__iter__()
def __getitem__(self, item):
return getattr(self, self.iterable).__getitem__(item)
def __len__(self):
return getattr(self, self.iterable).__len__()
def __contains__(self, item):
return getattr(self, self.iterable).__contains__(item)
def __bool__(self):
return True
class LineSet(JSONSerializable, IterableWrapper):
iterable = 'lines'
def __init__(self, *lines):
self.lines = []
for l in lines:
self.lines.extend(l)
def to_json(self):
return self.lines
def speakers(self) -> set:
speakers = set()
for line in self.lines:
speakers |= line.speaker
return speakers
def filter(self, function, transform=identity):
return LineSet(filter(function, map(transform, self.lines)))
def map(self, function):
return LineSet(map(function, self.lines))
def by(self, char):
if isinstance(char, str):
char = {char}
else:
char = set(char)
return self.filter(lambda l: char & l.speaker)
def contain(self, search, transform=identity):
if not callable(search):
search = lambda l: search in l
return self.filter(search, transform)
@property
def wc(self):
return sum(map(attrgetter('wc'), self.lines))
def __repr__(self):
return self.lines.__repr__()
class PageParser:
cache = 'cache'
ttl = 360
@classmethod
def get_file(cls, url):
path = cls._cache_path(url)
try:
last_modified = os.path.getmtime(path)
if (time.time() - last_modified) // 60 < cls.ttl:
with open(path, 'r', encoding='utf-8') as f:
return f.read()
else:
raise FileNotFoundError
except FileNotFoundError:
r = requests.get(url)
with open(path, 'w', encoding='utf-8') as f:
f.write(r.text)
return r.text
@classmethod
def _cache_path(cls, url):
return os.path.join(cls.cache, md5(url.encode('utf-8')).hexdigest())
@classmethod
def get_page(cls, url):
# TODO: use pickle for caching
return BeautifulSoup(cls.get_file(url), "lxml")
@classmethod
def clear_cache(cls, url):
path = cls._cache_path(url)
try:
os.unlink(path)
except FileNotFoundError:
pass
@classmethod
def clear_all_cache(cls):
for f in os.scandir(cls.cache):
os.unlink(f.path)
@staticmethod
def get_text(tag):
if tag.string:
return tag.string.strip()
return ''.join(tag.find_all(string=True))
class ModelEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, JSONSerializable):
return o.to_json()
if isinstance(o, (map, filter)):
return list(o)
if isinstance(o, set):
return list(sorted(o))
return super().default(o)
class Episode(PageParser, JSONSerializable, IterableWrapper):
hidden = ['show', 'season']
iterable = 'lines'
def __init__(self, season=None, number=None, url=None, hydrate=None, show=None):
self.lines = LineSet()
self.season = season
self.show = show or season.show
if hydrate:
self.hydrate(hydrate)
return
if url:
self.load(url)
else:
self.title = None
if number is None:
number = len(season.episodes) + 1
self.number = number
def load(self, url):
ret = self._parse(self.get_page(url))
# TODO: There might be a better way to do this
try: # Try three value return format first
self.title, lines, kwargs = ret
for k, v in kwargs.items():
setattr(self, k, v)
except ValueError: # If not, then assume only two values returned
self.title, lines = ret
self._add_lines(lines)
return self
def hydrate(self, data):
for k, v in data.items():
if k != 'lines':
setattr(self, k, v)
else:
self._add_lines(v)
return self
def serialize(self):
with open(self._filepath(), encoding='utf-8', mode='w') as f:
json.dump(self, f, indent=4, cls=ModelEncoder)
def _filepath(self):
return os.path.join(self.season.storage_dir(), self._filename())
def _filename(self, ext='json'):
return str(self.number) + ' - ' + remove_special(self.title) + '.' + ext
def _add_lines(self, lines):
for line in lines:
self.add_line(**line)
def add_line(self, *args, **kwargs):
kwargs['episode'] = self
line = self.show.create_line(*args, **kwargs)
self.lines.lines.append(line)
return line
# TODO: Consider splitting this into individual functions
def _parse(self, ep):
raise NotImplementedError
def __repr__(self):
return self.title
class Season(IterableWrapper):
iterable = 'episodes'
def __init__(self, order=None, name=None, show=None, urls=None, hydrate=False):
self.show = show
if order is None:
order = len(show.seasons) + 1
if name is None:
name = order
self.order = order
self.name = name
self.episodes = []
if urls:
self.load(urls)
elif hydrate:
self.hydrate()
def load(self, episodes):
for url in episodes:
episode = self.show.create_episode(season=self, url=url)
self.add_episode(episode)
def serialize(self):
if not os.path.exists(self.storage_dir()):
os.mkdir(self.storage_dir())
for episode in self.episodes:
episode.serialize()
def hydrate(self):
files = os.scandir(self.storage_dir())
for file in natsorted(files, key=attrgetter('path')):
with open(file.path, encoding='utf-8') as f:
data = json.load(f)
self.create_episode(hydrate=data, season=self)
return self
def storage_dir(self):
return os.path.join(self.show.storage_dir(), self.name)
def create_episode(self, *args, **kwargs):
episode = self.show.create_episode(*args, **kwargs)
self.add_episode(episode)
def add_episode(self, episode):
self.episodes.append(episode)
return episode
@property
def lines(self):
return LineSet(*map(attrgetter('lines'), self.episodes))
def episode(self, title):
for episode in self.episodes:
if episode.title == title:
return episode
def __repr__(self):
return self.name
class Line(JSONSerializable, IterableWrapper):
hidden = ['episode']
iterable = 'text'
def __init__(self, speaker, text, episode: Episode, number=None):
self.episode = episode
if number is None:
number = len(episode.lines) + 1
self.number = number
self.text = self._parse_text(text, episode, number)
if isinstance(speaker, str):
self.speaker = self._parse_speaker(speaker, episode, number)
else:
self.speaker = set(speaker)
def _parse_speaker(self, speaker, episode, number):
raise NotImplementedError
def _parse_text(self, text, episode, number) -> str:
raise NotImplementedError
@property
def wc(self):
return word_count(self.text)
def __repr__(self):
return ', '.join(self.speaker) + ': ' + self.text
class Show(PageParser, IterableWrapper):
ttl = 60 * 24
iterable = 'seasons'
def __init__(self, url=None, hydrate=False, season=Season, episode=Episode, line=Line):
self.seasons = []
self.episode_class = episode
self.season_class = season
self.line_class = line
if url:
self.load(url)
elif hydrate:
self.hydrate()
def create_episode(self, *args, **kwargs) -> Episode:
return self.episode_class(*args, **kwargs)
def create_season(self, *args, **kwargs) -> Season:
return self.season_class(*args, **kwargs)
def create_line(self, *args, **kwargs) -> Line:
return self.line_class(*args, **kwargs)
def add_season(self, *args, **kwargs) -> Season:
season = self.create_season(*args, **kwargs)
self.seasons.append(season)
return season
def load(self, url: str):
seasons_page = self.get_page(url)
seasons = self._parse(seasons_page, url)
for name, episodes in seasons.items():
self.add_season(name=name, show=self, urls=episodes)
def storage_dir(self):
return type(self).__name__.lower()
def seasons_file(self):
return os.path.join(self.storage_dir(), 'seasons.json')
def _parse(self, page: BeautifulSoup, url: str):
raise NotImplementedError
def hydrate(self):
with open(self.seasons_file(), encoding='utf-8') as f:
seasons = json.load(f)
for season in seasons:
self.add_season(name=season, show=self, hydrate=True)
def serialize(self):
if not os.path.exists(self.storage_dir()):
os.mkdir(self.storage_dir())
with open(self.seasons_file(), encoding='utf-8', mode='w') as f:
json.dump([s.name for s in self.seasons], f, indent=4)
for season in self.seasons:
season.serialize()
@property
def lines(self):
return LineSet(*map(attrgetter('lines'), self.seasons))