implement get_videos

This commit is contained in:
rlx 2017-11-01 18:14:15 +00:00
parent 55ea206d87
commit 3bd83f0462

View file

@ -58,10 +58,29 @@ class Engine:
return clips[index:] + clips[:index - 1]
def get_videos(self, user):
products = []
# Count tags for the user
count = {}
for event in user['events']:
if 'product' in event['data']:
products.append(event['data']['product'])
count[event['data']['product']] = count.get(
event['data']['product'], 0
) + 1
score = {}
# For each tag in playlist, increment score by count
for playlist in self.playlists:
score[playlist['name']] = random.random()
for tag in playlist['tags']:
score[playlist['name']] += count.get(tag, 0)
playlists = sorted(
self.playlists,
key=lambda playlist: -score[playlist['name']]
)[:16]
# Shuffle playlists (randomize layout) and shift clips (randomize start)
random.shuffle(playlists)
return [{
'clips': self._shift_clips(playlist['clips']),
'name': playlist['name']
} for playlist in playlists]
def update(self):
# Get all storylines with tags
@ -85,7 +104,7 @@ class Engine:
}, ['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 (and cache) order (and code + name) for each video
# Get and cache video data
filename = os.path.join(self.path, 'videos.json')
if os.path.exists(filename):
with open(filename) as f:
@ -99,21 +118,27 @@ class Engine:
], key=lambda video: video['order'])
with open(filename, 'w') as f:
f.write(json.dumps(videos, indent=4, sort_keys=True))
# Get video order
order = {video['id']: video['order'] for video in videos}
# Sort clips
clips = sorted(
clips,
key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in']
)
# Get playlists
# Get and cache playlists
self.playlists = [playlist for playlist in [{
'name': storyline['name'],
'tags': storyline['tags'],
'clips': [
'{}_{:.3f}-{:.3f}'.format(
clip['id'].split('/')[0], clip['in'], clip['out']
) for clip in clips if clip['value'] == storyline['name']
]
'clips': [{
'id': clip['id'],
'in': clip['in'],
'out': clip['out']
} for clip in clips if clip['value'] == storyline['name']]
#'clips': [
# '{}_{:.3f}-{:.3f}'.format(
# clip['id'].split('/')[0], clip['in'], clip['out']
# ) 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(self.playlists, indent=4, sort_keys=True))
@ -121,4 +146,3 @@ class Engine:
if __name__ == '__main__':
engine = Engine('json')
engine.update()