diff --git a/re.py b/re.py index e60077a..b7ce6cd 100644 --- a/re.py +++ b/re.py @@ -3,10 +3,12 @@ Reccomendation Engine Example ''' import json +import os +import random import ox -with open('pandora.json') as f: +with open('json/pandora.json') as f: # {"url": "...", "username": "...", "password": "..."} PANDORA = json.loads(f.read()) @@ -44,7 +46,7 @@ class Pandora: 'keys': keys })['data'] -def update(): +def get_playlists(): # Get all storylines with tags storylines = [{ 'name': entity['name'], @@ -66,14 +68,29 @@ def update(): }, ['id', 'in', 'out', 'value']) if clip['value'] in names] # Get list of ids for videos with clips ids = list(set([clip['id'].split('/')[0] for clip in clips])) - # Get order for each video - order = {id: pandora.get(id, ['order'])['order'] for id in ids} + # Get (and cache) order (and code + name) for each video + filename = 'json/videos.json' + if os.path.exists(filename): + with open(filename) as f: + videos_ = json.loads(f.read()) + ids_ = [video['id'] for video in videos_] + else: + videos_, ids_ = [], [] + videos = sorted(videos_ + [ + 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: + f.write(json.dumps(videos, indent=4, sort_keys=True)) + order = {video['id']: video['order'] for video in videos} + print(order) + # Sort clips clips = sorted( clips, key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in'] ) - # Get playlists - playlists = [playlist for playlist in [{ + # Return playlists + return [playlist for playlist in [{ 'name': storyline['name'], 'tags': storyline['tags'], 'clips': [ @@ -82,17 +99,21 @@ def update(): ) for clip in clips if clip['value'] == storyline['name'] ] } for storyline in storylines] if playlist['clips']] - return { - 'playlists': playlists - } -def getVideos(user): - pass +def get_videos(user): + products = [] + for event in user['events']: + if 'product' in event['data']: + products.append(event['data']['product']) + +def shift_clips(clips): + index = random.randrange(len(clips)) + return clips[index:] + clips[:index - 1] if __name__ == '__main__': pandora = Pandora() - data = update() - with open('data.json', 'w') as f: - f.write(json.dumps(data, indent=4, sort_keys=True)) - print(len(data['playlists']), 'playlists') + playlists = get_playlists() + with open('playlists.json', 'w') as f: + f.write(json.dumps(playlists, indent=4, sort_keys=True)) + print(len(playlists), 'playlists')