anime-downloader/anime_downloader/watch.py

149 lines
4.6 KiB
Python
Raw Normal View History

from anime_downloader import config
from anime_downloader.sites import get_anime_class
2018-05-31 04:04:47 -07:00
import os
import sys
import json
2018-05-31 04:04:47 -07:00
import logging
import click
2018-06-03 05:43:19 -07:00
import warnings
2018-06-03 10:30:26 -07:00
from time import time
2018-06-03 05:43:19 -07:00
logger = logging.getLogger(__name__)
2018-06-03 05:43:19 -07:00
# Don't warn if not using fuzzywuzzy[speedup]
with warnings.catch_warnings():
warnings.simplefilter('ignore')
from fuzzywuzzy import process
2018-05-31 04:04:47 -07:00
class Watcher:
WATCH_FILE = os.path.join(config.APP_DIR, 'watch.json')
def __init__(self):
pass
def new(self, url):
AnimeInfo = self._get_anime_info_class(url)
2018-06-03 10:30:26 -07:00
anime = AnimeInfo(url, timestamp=time())
2018-05-31 04:04:47 -07:00
self._append_to_watch_file(anime)
logger.info('Added {:.50} to watch list.'.format(anime.title))
return anime
2018-05-31 04:04:47 -07:00
def list(self):
animes = self._read_from_watch_file()
2018-06-02 12:11:43 -07:00
click.echo('{:>5} | {:^35} | {:^8} | {:^10}'.format(
'SlNo', 'Name', 'Eps', 'Type'
))
click.echo('-'*65)
2018-06-01 05:43:04 -07:00
fmt_str = '{:5} | {:35.35} | {:3}/{:<3} | {meta:10.10}'
2018-05-31 04:04:47 -07:00
for idx, anime in enumerate(animes):
meta = anime.meta
2018-05-31 04:04:47 -07:00
click.echo(fmt_str.format(idx+1, anime.title,
2018-06-01 05:43:04 -07:00
*anime.progress(),
meta=meta.get('Type', '')))
2018-05-31 04:04:47 -07:00
def anime_list(self):
return self._read_from_watch_file()
2018-05-31 04:04:47 -07:00
def get(self, anime_name):
animes = self._read_from_watch_file()
2018-06-03 05:43:19 -07:00
if isinstance(anime_name, int):
return animes[anime_name]
2018-06-02 13:04:39 -07:00
match = process.extractOne(anime_name, animes, score_cutoff=40)
if match:
2018-06-03 10:30:26 -07:00
anime = match[0]
logger.debug('Anime: {!r}, episodes_done: {}'.format(
2018-07-06 11:13:10 -07:00
anime, anime.episodes_done))
2018-06-03 10:30:26 -07:00
if (time() - anime._timestamp) > 4*24*60*60:
2018-06-05 14:11:43 -07:00
anime = self.update_anime(anime)
2018-06-03 10:30:26 -07:00
return anime
2018-05-31 04:04:47 -07:00
2018-06-05 14:11:43 -07:00
def update_anime(self, anime):
if not hasattr(anime, 'meta') or not anime.meta.get('Status') or \
anime.meta['Status'].lower() == 'airing':
logger.info('Updating anime {}'.format(anime.title))
AnimeInfo = self._get_anime_info_class(anime.url)
newanime = AnimeInfo(anime.url, episodes_done=anime.episodes_done,
2018-06-18 12:07:31 -07:00
timestamp=time())
newanime.title = anime.title
self.update(newanime)
return newanime
2018-07-05 08:40:40 -07:00
return anime
2018-06-05 14:11:43 -07:00
2018-06-03 05:43:19 -07:00
def add(self, anime):
self._append_to_watch_file(anime)
2018-06-05 14:11:43 -07:00
def remove(self, anime):
anime_name = anime.title
2018-06-02 12:11:43 -07:00
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)
2018-05-31 04:04:47 -07:00
def _append_to_watch_file(self, anime):
if not os.path.exists(self.WATCH_FILE):
2018-06-02 12:11:43 -07:00
self._write_to_watch_file([anime])
2018-05-31 04:04:47 -07:00
return
data = self._read_from_watch_file()
2018-06-02 12:11:43 -07:00
data = [anime] + data
2018-06-02 12:11:43 -07:00
self._write_to_watch_file(data)
2018-05-31 04:04:47 -07:00
2018-06-02 12:11:43 -07:00
def _write_to_watch_file(self, animes):
animes = [anime.__dict__ for anime in animes]
with open(self.WATCH_FILE, 'w') as watch_file:
json.dump(animes, watch_file)
2018-05-31 04:04:47 -07:00
def _read_from_watch_file(self):
if not os.path.exists(self.WATCH_FILE):
logger.error('Add something to watch list first.')
2018-06-03 10:30:26 -07:00
sys.exit(1)
2018-05-31 04:04:47 -07:00
with open(self.WATCH_FILE, 'r') as watch_file:
data = json.load(watch_file)
ret = []
for anime_dict in data:
# For backwards compatibility
if '_episodeIds' in anime_dict:
anime_dict['_episode_urls'] = anime_dict['_episodeIds']
AnimeInfo = self._get_anime_info_class(anime_dict['url'])
anime = AnimeInfo(_skip_online_data=True)
anime.__dict__ = anime_dict
ret.append(anime)
return ret
2018-05-31 04:04:47 -07:00
def _get_anime_info_class(self, url):
cls = get_anime_class(url)
2018-05-31 04:04:47 -07:00
# TODO: Maybe this is better off as a mixin
class AnimeInfo(cls, sitename=cls.sitename):
def __init__(self, *args, **kwargs):
self.episodes_done = kwargs.pop('episodes_done', 0)
self._timestamp = kwargs.pop('timestamp', 0)
2018-05-31 04:04:47 -07:00
super(cls, self).__init__(*args, **kwargs)
2018-05-31 04:04:47 -07:00
def progress(self):
return (self.episodes_done, len(self))
2018-05-31 04:04:47 -07:00
return AnimeInfo