recommendation state

This commit is contained in:
j 2018-02-05 14:15:39 +00:00 committed by Your Name
parent 71d6e0d0e6
commit c3962ac6eb
2 changed files with 51 additions and 3 deletions

View file

@ -30,10 +30,25 @@ class Engine:
filename = os.path.join(self.path, 'playlists.json') filename = os.path.join(self.path, 'playlists.json')
if os.path.exists(filename): if os.path.exists(filename):
with open(filename) as f: with open(filename) as f:
self.playlists = json.loads(f.read()) self.playlists = json.load(f)
else: else:
self.playlists = [] self.playlists = []
filename = os.path.join(self.path, 'state.json')
if os.path.exists(filename):
with open(filename) as f:
self.state = json.load(f)
else:
self.state = {
'channels': {
'keywords': {'locked': False, 'value': 7},
'screenings': {'locked': False, 'value': 7},
'random': {'locked': True, 'value': 2}
},
'keywords': {},
}
self.update_keywords()
@property @property
def pandora(self): def pandora(self):
while not self._pandora: while not self._pandora:
@ -101,6 +116,20 @@ class Engine:
'name': video['name'] 'name': video['name']
} for video in videos] } for video in videos]
def update_state(self, data):
for key in data:
if key in self.state:
self.state[key].update(data[key])
else:
self.state[key] = data[key]
self.save_state()
return self.state
def save_state(self):
filename = os.path.join(self.path, 'state.json')
with open(filename, 'w') as f:
json.dump(self.state, f, indent=4, ensure_ascii=False, sort_keys=True)
def update(self): def update(self):
# Get all storylines with tags # Get all storylines with tags
storylines = [{ storylines = [{
@ -160,6 +189,21 @@ class Engine:
} 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))
self.update_keywords()
def update_keywords(self):
changed = False
if 'keywords' not in self.state:
self.state['keywords'] = {}
changed = True
for playlist in self.playlists:
for tag in playlist.get('tags', []):
if tag.islower() and tag not in self.state['keywords']:
self.state['keywords'][tag] = {'value': 0}
changed = True
if changed:
self.save_state()
@run_async @run_async
def update_async(self): def update_async(self):

View file

@ -28,6 +28,10 @@ def api_task(request, engine, callback):
try: try:
if request['method'] == 'getVideos': if request['method'] == 'getVideos':
result = engine.get_videos(request['params']) result = engine.get_videos(request['params'])
elif request['method'] == 'getRecommendations':
result = engine.state
elif request['method'] == 'setRecommendations':
result = engine.update_state(request['params'])
else: else:
result = {} result = {}
response = { response = {
@ -53,7 +57,7 @@ class RPCHandler(tornado.web.RequestHandler):
request = None request = None
try: try:
request = json.loads(self.request.body.decode()) request = json.loads(self.request.body.decode())
if request['method'] not in ('getVideos', ): if request['method'] not in ('getVideos', 'getRecommendations', 'setRecommendations'):
raise Exception('unknown method') raise Exception('unknown method')
except: except:
error = {'error': {'code': -32700, 'message': 'Parse error'}} error = {'error': {'code': -32700, 'message': 'Parse error'}}