dd-re/re.py

99 lines
2.8 KiB
Python
Raw Normal View History

2017-11-01 15:09:17 +00:00
'''
Reccomendation Engine Example
'''
import json
import ox
with open('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']
):
self.api = ox.API(url)
self.api.signin(username=username, password=password)
def find_annotations(self, query, keys):
# print('FIND ANNOTATIONS', query, keys)
return self.api.findAnnotations({
'keys': keys,
'query': query,
'range': [0, 1000000]
})['data']['items']
def find_entities(self, query, keys):
# print('FIND ENTITIES', query, keys)
return self.api.findEntities({
'keys': keys,
'query': query,
'range': [0, 1000000]
})['data']['items']
def get(self, id, keys):
# print('GET', id, keys)
return self.api.get({
'id': id,
'keys': keys
})['data']
def update():
# Get all storylines with tags
storylines = [{
'name': entity['name'],
'tags': entity['tags']
} for entity in pandora.find_entities({
'conditions': [
{'key': 'type', 'operator': '==', 'value': 'storylines'},
],
'operator': '&'
}, ['id', 'name', 'tags']) if entity.get('tags', [])]
# 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({
'conditions': [
{'key': 'layer', 'operator': '==', 'value': 'storylines'}
],
'operator': '&'
}, ['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}
clips = sorted(
clips,
key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in']
)
# Get playlists
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']
]
} for storyline in storylines] if playlist['clips']]
return {
'playlists': playlists
}
def getVideos(user):
pass
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')