-
Notifications
You must be signed in to change notification settings - Fork 0
/
pithospandora.py
58 lines (47 loc) · 1.78 KB
/
pithospandora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging, os
import pithos.pithosconfig
import pithos.pandora
from pithos.PreferencesPithosDialog import PreferencesPithosDialog
from twisted.internet.threads import deferToThread
from pithos.pandora import PandoraAuthTokenInvalid
class GetPref(PreferencesPithosDialog):
def just_get_prefs(self):
getattr(self, '_PreferencesPithosDialog__load_preferences')()
return self.get_preferences()
def setup_fields(self):
pass
def newPandora():
"""
this is blocking
"""
gp = GetPref()
pref = gp.just_get_prefs()
pandora = pithos.pandora.make_pandora()
pandora.set_proxy(pref['proxy'])
pandora.set_audio_format(pref['audio_format'])
def reconnect():
logging.warn("reconnecting")
return pandora.connect(pref['username'], pref['password'])
pandora.mpdpandorafeeder_reconnect = reconnect
pandora.mpdpandorafeeder_reconnect()
logging.info("Pandora connected. Pithos version %s" %
pithos.pithosconfig.VERSION)
return pandora
def deferredCallWithReconnects(pandora, c, *args):
"""
run a blocking pandora call in a thread, and also reconnect the
pandora session if it has timed out
"""
def eb(f):
logging.warn("eb %r", f)
f.trap(PandoraAuthTokenInvalid)
logging.warn("PandoraAuthTokenInvalid")
if c.__name__ == 'mpdpandorafeeder_reconnect':
logging.error("reconnect is failing")
os.abort()
return (
deferredCallWithReconnects(pandora,
pandora.mpdpandorafeeder_reconnect)
.addCallback(deferredCallWithReconnects, pandora, c, *args))
print "deferToThread(%r,%r) pandora=%r" % (c, args, pandora)
return deferToThread(c, *args).addErrback(eb)