-
Notifications
You must be signed in to change notification settings - Fork 222
/
watch.py
120 lines (89 loc) · 3.55 KB
/
watch.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
from anime_downloader import config
from anime_downloader.sites.nineanime import NineAnime
import os
import sys
import pickle
import logging
import click
import warnings
from time import time
# Don't warn if not using fuzzywuzzy[speedup]
with warnings.catch_warnings():
warnings.simplefilter('ignore')
from fuzzywuzzy import process
class Watcher:
WATCH_FILE = os.path.join(config.APP_DIR, 'watch.json')
def __init__(self):
pass
def new(self, url):
anime = AnimeInfo(url, timestamp=time())
self._append_to_watch_file(anime)
logging.info('Added {:.50} to watch list.'.format(anime.title))
def list(self):
animes = self._read_from_watch_file()
click.echo('{:>5} | {:^35} | {:^8} | {:^10}'.format(
'SlNo', 'Name', 'Eps', 'Type'
))
click.echo('-'*65)
fmt_str = '{:5} | {:35.35} | {:3}/{:<3} | {meta:10.10}'
for idx, anime in enumerate(animes):
meta = anime.meta
click.echo(fmt_str.format(idx+1, anime.title,
*anime.progress(),
meta=meta.get('Type', '')))
def get(self, anime_name):
animes = self._read_from_watch_file()
if isinstance(anime_name, int):
return animes[anime_name]
match = process.extractOne(anime_name, animes, score_cutoff=40)
if match:
anime = match[0]
logging.debug('Anime: {!r}, episodes_done: {}'.format(anime, anime.episodes_done))
if (time() - anime._timestamp) > 4*24*60*60:
anime = self.update_anime(anime)
return anime
def update_anime(self, anime):
logging.info('Updating anime {}'.format(anime.title))
newanime = AnimeInfo(anime.url, episodes_done=anime.episodes_done,
timestamp=time())
newanime.title = anime.title
self.update(newanime)
return newanime
def add(self, anime):
self._append_to_watch_file(anime)
def remove(self, anime):
anime_name = anime.title
animes = self._read_from_watch_file()
animes = [anime for anime in animes if anime.title != anime_name]
self._write_to_watch_file(animes)
def update(self, changed_anime):
animes = self._read_from_watch_file()
animes = [anime for anime in animes
if anime.title != changed_anime.title]
animes = [changed_anime] + animes
self._write_to_watch_file(animes)
def _append_to_watch_file(self, anime):
if not os.path.exists(self.WATCH_FILE):
self._write_to_watch_file([anime])
return
with open(self.WATCH_FILE, 'rb') as watch_file:
data = pickle.load(watch_file)
data = [anime] + data
self._write_to_watch_file(data)
def _write_to_watch_file(self, animes):
with open(self.WATCH_FILE, 'wb') as watch_file:
pickle.dump(animes, watch_file)
def _read_from_watch_file(self):
if not os.path.exists(self.WATCH_FILE):
logging.error('Add something to watch list first.')
sys.exit(1)
with open(self.WATCH_FILE, 'rb') as watch_file:
data = pickle.load(watch_file)
return data
class AnimeInfo(NineAnime):
def __init__(self, *args, **kwargs):
self.episodes_done = kwargs.pop('episodes_done', 0)
self._timestamp = kwargs.pop('timestamp')
super(NineAnime, self).__init__(*args, **kwargs)
def progress(self):
return (self.episodes_done, len(self))