This commit is contained in:
rlx 2017-11-01 16:02:29 +00:00
parent ce8539472f
commit 1ad3740677

51
re.py
View file

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