refactor
This commit is contained in:
parent
d25cdf931e
commit
a088662559
1 changed files with 18 additions and 23 deletions
41
re.py
41
re.py
|
@ -8,18 +8,9 @@ import random
|
||||||
|
|
||||||
import ox
|
import ox
|
||||||
|
|
||||||
with open('json/pandora.json') as f:
|
|
||||||
# {"url": "...", "username": "...", "password": "..."}
|
|
||||||
PANDORA = json.loads(f.read())
|
|
||||||
|
|
||||||
class Pandora:
|
class Pandora:
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, url, username, password):
|
||||||
self,
|
|
||||||
url=PANDORA['url'],
|
|
||||||
username=PANDORA['username'],
|
|
||||||
password=PANDORA['password']
|
|
||||||
):
|
|
||||||
self.api = ox.API(url)
|
self.api = ox.API(url)
|
||||||
self.api.signin(username=username, password=password)
|
self.api.signin(username=username, password=password)
|
||||||
|
|
||||||
|
@ -48,8 +39,13 @@ class Pandora:
|
||||||
|
|
||||||
class Engine:
|
class Engine:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, path):
|
||||||
pass
|
self.path = path
|
||||||
|
self.pandora = Pandora(
|
||||||
|
url='http://pandora.dmp/api/',
|
||||||
|
username='dd.re',
|
||||||
|
password='dd.re'
|
||||||
|
)
|
||||||
|
|
||||||
def _shift_clips(self, clips):
|
def _shift_clips(self, clips):
|
||||||
index = random.randrange(len(clips))
|
index = random.randrange(len(clips))
|
||||||
|
@ -66,7 +62,7 @@ class Engine:
|
||||||
storylines = [{
|
storylines = [{
|
||||||
'name': entity['name'],
|
'name': entity['name'],
|
||||||
'tags': entity['tags']
|
'tags': entity['tags']
|
||||||
} for entity in pandora.find_entities({
|
} for entity in self.pandora.find_entities({
|
||||||
'conditions': [
|
'conditions': [
|
||||||
{'key': 'type', 'operator': '==', 'value': 'storylines'},
|
{'key': 'type', 'operator': '==', 'value': 'storylines'},
|
||||||
],
|
],
|
||||||
|
@ -75,7 +71,7 @@ class Engine:
|
||||||
# Get list of storyline names
|
# Get list of storyline names
|
||||||
names = list(set([storyline['name'] for storyline in storylines]))
|
names = list(set([storyline['name'] for storyline in storylines]))
|
||||||
# Get all clips annotated with storyline references
|
# Get all clips annotated with storyline references
|
||||||
clips = [clip for clip in pandora.find_annotations({
|
clips = [clip for clip in self.pandora.find_annotations({
|
||||||
'conditions': [
|
'conditions': [
|
||||||
{'key': 'layer', 'operator': '==', 'value': 'storylines'}
|
{'key': 'layer', 'operator': '==', 'value': 'storylines'}
|
||||||
],
|
],
|
||||||
|
@ -84,7 +80,7 @@ class Engine:
|
||||||
# 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) order (and code + name) for each video
|
||||||
filename = 'json/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:
|
||||||
videos_ = json.loads(f.read())
|
videos_ = json.loads(f.read())
|
||||||
|
@ -92,7 +88,7 @@ class Engine:
|
||||||
else:
|
else:
|
||||||
videos_, ids_ = [], []
|
videos_, ids_ = [], []
|
||||||
videos = sorted(videos_ + [
|
videos = sorted(videos_ + [
|
||||||
pandora.get(id, ['code', 'id', 'order', 'title'])
|
self.pandora.get(id, ['code', 'id', 'order', 'title'])
|
||||||
for id in ids if not id in ids_
|
for id in ids if not id in ids_
|
||||||
], key=lambda video: video['order'])
|
], key=lambda video: video['order'])
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
|
@ -103,8 +99,8 @@ class Engine:
|
||||||
clips,
|
clips,
|
||||||
key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in']
|
key=lambda clip: order[clip['id'].split('/')[0]] * 1000000 + clip['in']
|
||||||
)
|
)
|
||||||
# Return playlists
|
# Get playlists
|
||||||
return [playlist for playlist in [{
|
playlists = [playlist for playlist in [{
|
||||||
'name': storyline['name'],
|
'name': storyline['name'],
|
||||||
'tags': storyline['tags'],
|
'tags': storyline['tags'],
|
||||||
'clips': [
|
'clips': [
|
||||||
|
@ -113,11 +109,10 @@ class Engine:
|
||||||
) 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']]
|
||||||
|
with open(os.path.join(self.path, 'playlists.json'), 'w') as f:
|
||||||
|
f.write(json.dumps(playlists, indent=4, sort_keys=True))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pandora = Pandora()
|
engine = Engine('json')
|
||||||
engine = Engine()
|
engine.update()
|
||||||
playlists = engine.update()
|
|
||||||
with open('json/playlists.json', 'w') as f:
|
|
||||||
f.write(json.dumps(playlists, indent=4, sort_keys=True))
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue