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

@ -27,13 +27,28 @@ class Engine:
username=kwargs.get('username', 'dd.re'),
password=kwargs.get('password', 'dd.re')
)
filename = os.path.join(self.path, 'playlists.json')
filename = os.path.join(self.path, 'playlists.json')
if os.path.exists(filename):
with open(filename) as f:
self.playlists = json.loads(f.read())
self.playlists = json.load(f)
else:
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
def pandora(self):
while not self._pandora:
@ -101,6 +116,20 @@ class Engine:
'name': video['name']
} 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):
# Get all storylines with tags
storylines = [{
@ -160,6 +189,21 @@ class Engine:
} 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))
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
def update_async(self):

View File

@ -28,6 +28,10 @@ def api_task(request, engine, callback):
try:
if request['method'] == 'getVideos':
result = engine.get_videos(request['params'])
elif request['method'] == 'getRecommendations':
result = engine.state
elif request['method'] == 'setRecommendations':
result = engine.update_state(request['params'])
else:
result = {}
response = {
@ -53,7 +57,7 @@ class RPCHandler(tornado.web.RequestHandler):
request = None
try:
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')
except:
error = {'error': {'code': -32700, 'message': 'Parse error'}}