1
import xbmcgui
2
dlg = xbmcgui.DialogProgress()
3
dlg.create( "PANDORA", "Loading Script..." )
4
dlg.update( 0 )
5
import xbmc, os
6
import xbmcaddon
7
8
try:
9
	from libpandora.pandora import Pandora
10
except AttributeError, e:
11
	xbmcgui.Dialog().ok( "PANDORA", \
12
						 "ERROR: Something is wrong with your encryption keys." )
13
	raise e
14
15
from pandagui import PandaGUI
16
from pandaplayer import PandaPlayer
17
18
__title__ = "Pandora"
19
__settings__ = xbmcaddon.Addon(id='script.xbmc.pandora')
20
21
scriptPath = os.getcwd().replace(';','')
22
23
def GetGuiSetting( type, name ):
24
	resp = xbmc.executehttpapi( "GetGuiSetting( %d, %s )" %( type, name ) )
25
	resp = resp.replace( "<li>", "" )
26
27
	if type == 0:
28
		resp = int( resp )
29
	elif type == 1:
30
		resp = ( resp == "True" )
31
	elif type == 2:
32
		resp = float( resp )
33
34
	return resp
35
36
class PandaException( Exception ):
37
	pass
38
39
class Panda:
40
41
	def __init__( self ):
42
		self.gui = None
43
		self.pandora = None
44
		self.playlist = []
45
		self.curStation = ""
46
		self.playing = False
47
		self.skip = False
48
		self.die = False
49
		self.settings = __settings__
50
		
51
		fmt = self.settings.getSetting( "format" )
52
		self.pandora = Pandora( fmt )
53
54
		#Proxy settings
55
		if self.settings.getSetting( "proxy_enable" ):
56
			proxy_info = {
57
				"host" : self.settings.getSetting( "proxy_server" ),
58
				"port" : self.settings.getSetting( "proxy_port" ),
59
				"user" : self.settings.getSetting( "proxy_user" ),
60
				"pass" : self.settings.getsetting( "proxy_pass" )
61
			}
62
			self.pandora.setProxy( proxy_info )
63
64
		
65
		self.pandora.sync()
66
		
67
		while not self.auth():
68
			resp = xbmcgui.Dialog().yesno( "Pandora", \
69
					"Failed to authenticate listener.", \
70
					"Check username/password and try again.", \
71
					"Show Settings?" )
72
			if resp:
73
				self.settings.openSettings()
74
			else:
75
				self.quit()
76
				return
77
78
		self.player = PandaPlayer( panda = self )
79
		scriptSkinPath = os.path.join(scriptPath,"resources")
80
		self.gui = PandaGUI( "script-pandora.xml", scriptPath, \
81
							 "Default", "NTSC", panda = self )
82
83
	def auth( self ):
84
		user = self.settings.getSetting( "username" )
85
		pwd = self.settings.getSetting( "password" )
86
		if user == "" or pwd == "":
87
			return False
88
		dlg = xbmcgui.DialogProgress()
89
		dlg.create( "PANDORA", "Logging In..." )
90
		dlg.update( 0 )
91
		ret = self.pandora.authListener( user, pwd )
92
		dlg.close()
93
		return ret
94
95
	def playStation( self, stationId ):
96
		self.curStation = stationId
97
		self.playlist = []
98
		self.getMoreSongs()
99
		self.playing = True
100
		self.playNextSong()
101
102
	def getStations( self ):
103
		return self.pandora.getStations()
104
	
105
	def getMoreSongs( self ):
106
		if self.curStation == "":
107
			raise PandaException()
108
		items = []
109
		fragment = self.pandora.getFragment( self.curStation )
110
		for s in fragment:
111
			item = xbmcgui.ListItem( s["songTitle"] )
112
			item.setIconImage( s["artRadio"] )
113
			item.setThumbnailImage( s["artRadio"] )
114
			item.setProperty( "Cover", s["artRadio"] )
115
			info = { "title"	:	s["songTitle"], \
116
					 "artist"	:	s["artistSummary"], \
117
					 "album"	:	s["albumTitle"], \
118
					 "genre"	:	"".join(s["genre"]), \
119
					}
120
			item.setInfo( "music", info )
121
			items.append( ( s["audioURL"], item ) )
122
		self.playlist.extend( items )
123
124
	def playNextSong( self ):
125
		if not self.playing:
126
			raise PandaException()
127
		try:
128
			next = self.playlist.pop( 0 )
129
			self.player.playSong( next )
130
			art = next[1].getProperty( "Cover" )
131
			self.gui.setProperty( "AlbumArt", art )
132
		except IndexError:
133
			self.getMoreSongs()
134
		if len( self.playlist ) == 0:
135
			#Out of songs, grab some more while playing
136
			self.getMoreSongs()
137
138
	def skipSong( self ):
139
		self.skip = True
140
		self.player.stop()
141
142
	def main( self ):
143
		if self.die:
144
			return
145
		self.gui.doModal()
146
		self.cleanup()
147
		xbmc.sleep( 500 ) #Wait to make sure everything finishes
148
149
	def stop( self ):
150
		self.playing = False
151
152
	def cleanup( self ):
153
		self.skip = False
154
		if self.playing:
155
			self.playing = False
156
			self.player.stop()
157
		del self.gui
158
		del self.player
159
160
	def quit( self ):
161
		if self.gui != None:
162
			self.gui.close()
163
164
if __name__ == '__main__':
165
	if not ( os.path.exists( os.path.join( scriptPath, "crypt_key_input.h" ) ) \
166
			and os.path.exists( os.path.join( scriptPath, "crypt_key_output.h" ) ) ):
167
		xbmcgui.Dialog().ok( "Pandora", "Missing encription key files." )
168
		dlg.close()
169
	else:
170
		panda = Panda()
171
		panda.main()
172
		dlg.close()