From a088662559ec25b57ceb7fdde37493be9911d440 Mon Sep 17 00:00:00 2001 From: rlx Date: Wed, 1 Nov 2017 16:20:58 +0000 Subject: [PATCH] refactor --- re.py | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/re.py b/re.py index 071dbe1..1dff752 100644 --- a/re.py +++ b/re.py @@ -8,18 +8,9 @@ import random import ox -with open('json/pandora.json') as f: - # {"url": "...", "username": "...", "password": "..."} - PANDORA = json.loads(f.read()) - class Pandora: - def __init__( - self, - url=PANDORA['url'], - username=PANDORA['username'], - password=PANDORA['password'] - ): + def __init__(self, url, username, password): self.api = ox.API(url) self.api.signin(username=username, password=password) @@ -48,8 +39,13 @@ class Pandora: class Engine: - def __init__(self): - pass + def __init__(self, path): + self.path = path + self.pandora = Pandora( + url='http://pandora.dmp/api/', + username='dd.re', + password='dd.re' + ) def _shift_clips(self, clips): index = random.randrange(len(clips)) @@ -66,7 +62,7 @@ class Engine: storylines = [{ 'name': entity['name'], 'tags': entity['tags'] - } for entity in pandora.find_entities({ + } for entity in self.pandora.find_entities({ 'conditions': [ {'key': 'type', 'operator': '==', 'value': 'storylines'}, ], @@ -75,7 +71,7 @@ class Engine: # Get list of storyline names names = list(set([storyline['name'] for storyline in storylines])) # Get all clips annotated with storyline references - clips = [clip for clip in pandora.find_annotations({ + clips = [clip for clip in self.pandora.find_annotations({ 'conditions': [ {'key': 'layer', 'operator': '==', 'value': 'storylines'} ], @@ -84,7 +80,7 @@ class Engine: # Get list of ids for videos with clips ids = list(set([clip['id'].split('/')[0] for clip in clips])) # Get (and cache) order (and code + name) for each video - filename = 'json/videos.json' + filename = os.path.join(self.path, 'videos.json') if os.path.exists(filename): with open(filename) as f: videos_ = json.loads(f.read()) @@ -92,7 +88,7 @@ class Engine: else: videos_, ids_ = [], [] videos = sorted(videos_ + [ - pandora.get(id, ['code', 'id', 'order', 'title']) + self.pandora.get(id, ['code', 'id', 'order', 'title']) for id in ids if not id in ids_ ], key=lambda video: video['order']) with open(filename, 'w') as f: @@ -103,8 +99,8 @@ class Engine: clips, key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in'] ) - # Return playlists - return [playlist for playlist in [{ + # Get playlists + playlists = [playlist for playlist in [{ 'name': storyline['name'], 'tags': storyline['tags'], 'clips': [ @@ -113,11 +109,10 @@ class Engine: ) for clip in clips if clip['value'] == storyline['name'] ] } for storyline in storylines] if playlist['clips']] + with open(os.path.join(self.path, 'playlists.json'), 'w') as f: + f.write(json.dumps(playlists, indent=4, sort_keys=True)) if __name__ == '__main__': - pandora = Pandora() - engine = Engine() - playlists = engine.update() - with open('json/playlists.json', 'w') as f: - f.write(json.dumps(playlists, indent=4, sort_keys=True)) + engine = Engine('json') + engine.update()