implement get_videos
This commit is contained in:
parent
55ea206d87
commit
3bd83f0462
1 changed files with 34 additions and 10 deletions
|
@ -58,10 +58,29 @@ class Engine:
|
||||||
return clips[index:] + clips[:index - 1]
|
return clips[index:] + clips[:index - 1]
|
||||||
|
|
||||||
def get_videos(self, user):
|
def get_videos(self, user):
|
||||||
products = []
|
# Count tags for the user
|
||||||
|
count = {}
|
||||||
for event in user['events']:
|
for event in user['events']:
|
||||||
if 'product' in event['data']:
|
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):
|
def update(self):
|
||||||
# Get all storylines with tags
|
# Get all storylines with tags
|
||||||
|
@ -85,7 +104,7 @@ class Engine:
|
||||||
}, ['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 (and cache) order (and code + name) for each video
|
# Get and cache video data
|
||||||
filename = os.path.join(self.path, 'videos.json')
|
filename = os.path.join(self.path, 'videos.json')
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
|
@ -99,21 +118,27 @@ class Engine:
|
||||||
], key=lambda video: video['order'])
|
], key=lambda video: video['order'])
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(json.dumps(videos, indent=4, sort_keys=True))
|
f.write(json.dumps(videos, indent=4, sort_keys=True))
|
||||||
|
# Get video order
|
||||||
order = {video['id']: video['order'] for video in videos}
|
order = {video['id']: video['order'] for video in videos}
|
||||||
# Sort clips
|
# 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
|
# Get and cache playlists
|
||||||
self.playlists = [playlist for playlist in [{
|
self.playlists = [playlist for playlist in [{
|
||||||
'name': storyline['name'],
|
'name': storyline['name'],
|
||||||
'tags': storyline['tags'],
|
'tags': storyline['tags'],
|
||||||
'clips': [
|
'clips': [{
|
||||||
'{}_{:.3f}-{:.3f}'.format(
|
'id': clip['id'],
|
||||||
clip['id'].split('/')[0], clip['in'], clip['out']
|
'in': clip['in'],
|
||||||
) for clip in clips if clip['value'] == storyline['name']
|
'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']]
|
} for storyline in storylines] if playlist['clips']]
|
||||||
with open(os.path.join(self.path, 'playlists.json'), 'w') as f:
|
with open(os.path.join(self.path, 'playlists.json'), 'w') as f:
|
||||||
f.write(json.dumps(self.playlists, indent=4, sort_keys=True))
|
f.write(json.dumps(self.playlists, indent=4, sort_keys=True))
|
||||||
|
@ -121,4 +146,3 @@ class Engine:
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
engine = Engine('json')
|
engine = Engine('json')
|
||||||
engine.update()
|
engine.update()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue