def set_image(request): """ Upload or change one image """ valid_request(request) file = request.files['file'] name = request.form.get('name') filename = get_secure_name(file.filename, name) save_file(file, filename) path = generate_path(filename) image = Images.query.filter_by(name=name).first() if image: Images.update(image, path, name) STATUS = 'UPDATED' else: image = Images.create(path, name) STATUS = 'CREATED' return {'image': image.serialize(), 'status': STATUS}, 201
def _parse_meta(data): from models.meta import Meta id = None title = None rating = None image_name = None episodes = [] description = None import json data_obj = json.loads(data) # print(data_obj) if "Title" in data_obj.keys() and data_obj["Title"] != 'N/A': title = data_obj["Title"] if "imdbRating" in data_obj.keys() and data_obj["imdbRating"] != 'N/A': rating = data_obj["imdbRating"] if "Poster" in data_obj.keys() and data_obj["Poster"] != 'N/A': url = data_obj["Poster"] image_name = f'{OMDB.IMAGE_PREFIX}{data_obj["imdbID"]}{url[-4:]}' try: from api.images import Images Images.download_poster(url, image_name) except Exception: image_name = None if "Plot" in data_obj.keys() and data_obj["Plot"] != 'N/A': description = data_obj["Plot"] return Meta(f"{OMDB.META_ID_PREFIX}:::{data_obj['imdbID']}", title, rating, image_name, episodes, description)
def _parse(data): import xml.etree.ElementTree from models.meta import Meta, Episode_Meta from api.images import Images root = xml.etree.ElementTree.fromstring(data) anidb_id = root.attrib['id'] title = root.findall('./titles/title[@type="main"]')[0].text episodes = [] _episodes = root.findall("./episodes/episode/epno[@type = '1']/..") namespace = "http://www.w3.org/XML/1998/namespace" for episode in _episodes: episode_data = [] episode_num = int(episode.find('./epno').text) episode_summary = "" summary = episode.find('./summary') if summary is not None: episode_summary = summary.text episode_title = episode.find( f'./title[@{{{namespace}}}lang="en"]').text episodes.append( Episode_Meta(episode_num, -1, episode_title, episode_summary)) temp_rating = float(root.find('./ratings/temporary').text) perm_rating = float(root.find('./ratings/permanent').text) rating = temp_rating if temp_rating > perm_rating else perm_rating _image_name = root.find("./picture").text image_url = f'http://cdn.anidb.net/images/main/{_image_name}' image_name = AniDB.IMAGE_PREFIX + _image_name Images.download_poster(image_url, image_name) description_node = root.find("./description") if description_node is not None: description = description_node.text.replace("\n", " ") else: description = "" return Meta(f"{AniDB.META_ID_PREFIX}:::{anidb_id}", title, rating, image_name, episodes, description)
def _identifiable_dict(identifiable): ret = {} ret['poster'] = Images.poster(identifiable) anidb_id = _anidb_id(identifiable) if anidb_id: ret['anidb_id'] = anidb_id omdb_id = _omdb_id(identifiable) if omdb_id: ret['omdb_id'] = omdb_id rating = _rating(identifiable) if rating: ret['rating'] = rating description = _description(identifiable) if description: ret['description'] = description ret['can_identify'] = _can_identify(identifiable) ret['is_identified'] = _is_identified(identifiable) ret['has_info'] = _has_info(identifiable) if identifiable.meta() and identifiable.meta().title(): ret['title'] = identifiable.meta().title() return ret
def dict(media): ret = { **DictItem.dict(media), 'played': media.played(), 'is_movie': isinstance(media, Movie) } has_poster = 'poster' in ret.keys() and ret['poster'] is not None ret['thumbnail'] = Images.thumbnail(media, create_ifmissing=not has_poster) if isinstance(media, Episode): ret['episode_number'] = media.episode_number() episode_meta = _episode_meta(media) if episode_meta: summary = episode_meta.summary() if summary: ret['summary'] = summary if not media.is_extra(): ret['title'] = '. '.join([ str(episode_meta.episode_number()), episode_meta.title() ]) return ret
def test_poster(self): from models.media import Movie from models.meta import Meta media = Movie("foo", "bar", played=False) self.assertIsNone(Images.poster(media)) media.set_meta(Meta( "id", "title", "rating", "image_name", "episodes", "description" )) self.assertEqual( Images.poster(media), f'{"/".join(Images.POSTER_FOLDER)}/{media.meta().image_name()}' )
def test_thumbnails(self): import sys import os from models.media import Episode from models.containers import MediaLibrary media = Episode( "foobar", episode_number=1, played=False) self.assertEqual( Images.thumbnail(media), f'{"/".join(Images.THUMBNAIL_FOLDER)}/{media.thumbnail()}.png' ) SYS_PATH_0 = sys.path[0] self.assertEqual(SYS_PATH_0, sys.path[0]) if __name__ != '__main__': sys.path[0] = os.sep.join([sys.path[0], 'test']) self.assertTrue(os.path.exists(sys.path[0])) parent = MediaLibrary(sys.path[0]) # long clip long_clip = 'sample-video-5min.mkv' self.assertTrue(os.path.exists(os.path.join( sys.path[0], long_clip ))) media = Episode( long_clip, episode_number=1, played=False, parent=parent) thumbnail_path = os.path.join( sys.path[0], os.sep.join(Images.THUMBNAIL_FOLDER), f"{media.thumbnail()}.png" ) self.assertFalse(os.path.exists(thumbnail_path)) thumbnail = Images.thumbnail(media, create_ifmissing=True) self.assertTrue(os.path.exists(thumbnail_path)) os.remove(thumbnail_path) self.assertFalse(os.path.exists(thumbnail_path)) os.removedirs(os.path.dirname(thumbnail_path)) self.assertFalse(os.path.exists(os.path.dirname(thumbnail_path))) # short clip short_clip = 'sample-video-12s.mkv' self.assertTrue(os.path.exists(os.path.join( sys.path[0], short_clip ))) media = Episode( short_clip, episode_number=2, played=False, parent=parent) thumbnail_path = os.path.join( sys.path[0], os.sep.join(Images.THUMBNAIL_FOLDER), f"{media.thumbnail()}.png" ) self.assertFalse(os.path.exists(thumbnail_path)) thumbnail = Images.thumbnail(media, create_ifmissing=True) self.assertTrue(os.path.exists(thumbnail_path)) os.remove(thumbnail_path) self.assertFalse(os.path.exists(thumbnail_path)) os.removedirs(os.path.dirname(thumbnail_path)) self.assertFalse(os.path.exists(os.path.dirname(thumbnail_path))) # parentless clip no_parent_clip = 'sample-video-12s.mkv' self.assertTrue(os.path.exists(os.path.join( sys.path[0], no_parent_clip ))) media = Episode( no_parent_clip, episode_number=3, played=False, parent=None) thumbnail_path = os.path.join( sys.path[0], os.sep.join(Images.THUMBNAIL_FOLDER), f"{media.thumbnail()}.png" ) self.assertFalse(os.path.exists(thumbnail_path)) thumbnail = Images.thumbnail(media, create_ifmissing=True) self.assertFalse(os.path.exists(thumbnail_path)) # missing clip missing_clip = 'foobar' self.assertFalse(os.path.exists(os.path.join( sys.path[0], missing_clip ))) media = Episode( missing_clip, episode_number=3, played=False, parent=None) thumbnail_path = os.path.join( sys.path[0], os.sep.join(Images.THUMBNAIL_FOLDER), f"{media.thumbnail()}.png" ) self.assertFalse(os.path.exists(thumbnail_path)) thumbnail = Images.thumbnail(media, create_ifmissing=True) self.assertFalse(os.path.exists(thumbnail_path)) if __name__ != '__main__': sys.path[0] = os.sep.join(sys.path[0].split(os.sep)[0:-1]) self.assertEqual(SYS_PATH_0, sys.path[0])