only connect to pandora as needed

This commit is contained in:
j 2018-02-05 11:47:38 +01:00
parent 43c9849b16
commit 71d6e0d0e6
2 changed files with 17 additions and 2 deletions

View file

@ -7,7 +7,6 @@ Restart=always
User=dd User=dd
Group=dd Group=dd
WorkingDirectory=/srv/dd/re WorkingDirectory=/srv/dd/re
ExecStartPre=/usr/bin/wait-for-it -h pandora.dmp -p 80
ExecStart=/srv/dd/re/server.py ExecStart=/srv/dd/re/server.py
[Install] [Install]

View file

@ -5,18 +5,24 @@ Recommendation Engine Example
from collections import defaultdict from collections import defaultdict
import json import json
import logging
import os import os
import random import random
import time
import ox import ox
from utils import run_async from utils import run_async
logger = logging.getLogger(__name__)
class Engine: class Engine:
_pandora = None
def __init__(self, path, **kwargs): def __init__(self, path, **kwargs):
self.path = path self.path = path
self.pandora = Pandora( self.pandora_args = dict(
url=kwargs.get('pandora', 'http://pandora.dmp/api/'), url=kwargs.get('pandora', 'http://pandora.dmp/api/'),
username=kwargs.get('username', 'dd.re'), username=kwargs.get('username', 'dd.re'),
password=kwargs.get('password', 'dd.re') password=kwargs.get('password', 'dd.re')
@ -28,6 +34,16 @@ class Engine:
else: else:
self.playlists = [] self.playlists = []
@property
def pandora(self):
while not self._pandora:
try:
self._pandora = Pandora(**self.pandora_args)
except:
logger.error('failed to connect to pandora, retry in 10 seconds')
time.sleep(10)
return self._pandora
def _patch_clips(self, clips): def _patch_clips(self, clips):
inpoints = {} inpoints = {}
for index, clip in enumerate(clips): for index, clip in enumerate(clips):