Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.27 KB | None | 0 0
  1. # * Thanks to:
  2. # *
  3. # * Nuka for the original RecentlyAdded.py on which this is based
  4. # *
  5. # * ppic, Hitcher,ronie & phil65 for the updates
  6.  
  7. import xbmc, xbmcgui, xbmcaddon
  8. import re, sys, os, random
  9. from elementtree import ElementTree as xmltree
  10. if sys.version_info < (2, 7):
  11. import simplejson
  12. else:
  13. import json as simplejson
  14.  
  15. __addon__ = xbmcaddon.Addon()
  16. __addonid__ = __addon__.getAddonInfo('id')
  17. __addonversion__ = __addon__.getAddonInfo('version')
  18.  
  19. def log(txt):
  20. message = '%s: %s' % (__addonid__, txt)
  21. xbmc.log(msg=message, level=xbmc.LOGDEBUG)
  22.  
  23. class Main:
  24. # grab the home window
  25. WINDOW = xbmcgui.Window( 10000 )
  26.  
  27. def _clear_properties( self ):
  28. # reset totals property for visible condition
  29. self.WINDOW.clearProperty( "RandomMovie.Count" )
  30. self.WINDOW.clearProperty( "RandomEpisode.Count" )
  31. self.WINDOW.clearProperty( "RandomMusicVideo.Count" )
  32. self.WINDOW.clearProperty( "RandomSong.Count" )
  33. self.WINDOW.clearProperty( "RandomAlbum.Count" )
  34. self.WINDOW.clearProperty( "RandomAddon.Count" )
  35. # we clear title for visible condition
  36. for count in range( self.LIMIT ):
  37. self.WINDOW.clearProperty( "RandomMovie.%d.Title" % ( count + 1 ) )
  38. self.WINDOW.clearProperty( "RandomEpisode.%d.Title" % ( count + 1 ) )
  39. self.WINDOW.clearProperty( "RandomMusicVideo.%d.Title" % ( count + 1 ) )
  40. self.WINDOW.clearProperty( "RandomSong.%d.Title" % ( count + 1 ) )
  41. self.WINDOW.clearProperty( "RandomAlbum.%d.Title" % ( count + 1 ) )
  42. self.WINDOW.clearProperty( "RandomAddon.%d.Name" % ( count + 1 ) )
  43.  
  44. def _parse_argv( self ):
  45. try:
  46. # parse sys.argv for params
  47. params = dict( arg.split( "=" ) for arg in sys.argv[ 1 ].split( "&" ) )
  48. except:
  49. # no params passed
  50. params = {}
  51. # set our preferences
  52. self.LIMIT = int( params.get( "limit", "5" ) )
  53. self.UNPLAYED = params.get( "unplayed", "False" )
  54. self.PLAY_TRAILER = params.get( "trailer", "False" )
  55. self.ALARM = int( params.get( "alarm", "0" ) )
  56. self.ALBUMID = params.get( "albumid", "" )
  57.  
  58. def _set_alarm( self ):
  59. # only run if user/skinner preference
  60. if ( not self.ALARM ): return
  61. # set the alarms command
  62. command = "XBMC.RunScript(%s,limit=%d&unplayed=%s&trailer=%s&alarm=%d)" % ( __addonid__, self.LIMIT, str( self.UNPLAYED ), str( self.PLAY_TRAILER ), self.ALARM, )
  63. xbmc.executebuiltin( "AlarmClock(RandomItems,%s,%d,true)" % ( command, self.ALARM, ) )
  64.  
  65. def __init__( self ):
  66. # parse argv for any preferences
  67. self._parse_argv()
  68. # check if we were executed internally
  69. if self.ALBUMID:
  70. self._Play_Album( self.ALBUMID )
  71. else:
  72. # clear properties
  73. self._clear_properties()
  74. # set any alarm
  75. self._set_alarm()
  76. # fetch media info
  77. self._fetch_movie_info()
  78. self._fetch_episode_info()
  79. self._fetch_musicvideo_info()
  80. self._fetch_album_info()
  81. self._fetch_artist_info()
  82. self._fetch_song_info()
  83. self._fetch_addon_info()
  84.  
  85. def _fetch_movie_info( self ):
  86. # query the database
  87. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": {"properties": ["title", "playcount", "year", "plot", "runtime", "fanart", "thumbnail", "file", "trailer", "rating"] }, "id": 1}')
  88. json_query = unicode(json_query, 'utf-8', errors='ignore')
  89. # separate the records
  90. json_response = simplejson.loads(json_query)
  91. if json_response.has_key('result'):
  92. if (json_response['result'] != None) and (json_response['result'].has_key('movies')):
  93. json_response = json_response['result']['movies']
  94. # get total value
  95. total = str( len( json_response ) )
  96. # enumerate thru our records
  97. count = 0
  98. while count < self.LIMIT:
  99. count += 1
  100. # check if we don't run out of items before LIMIT is reached
  101. if len( json_response ) == 0:
  102. return
  103. # select a random item
  104. item = random.choice( json_response )
  105. # remove the item from our list
  106. json_response.remove( item )
  107. # find values
  108. if self.UNPLAYED == "True":
  109. playcount = item['playcount']
  110. if playcount > 0:
  111. count = count - 1
  112. continue
  113. title = item['title']
  114. rating = str(round(float(item['rating']),1))
  115. year = str(item['year'])
  116. plot = item['plot']
  117. runtime = item['runtime']
  118. path = item['file']
  119. thumb = item['thumbnail']
  120. trailer = item['trailer']
  121. fanart = item['fanart']
  122. # set our properties
  123. self.WINDOW.setProperty( "RandomMovie.%d.Title" % ( count ), title )
  124. self.WINDOW.setProperty( "RandomMovie.%d.Rating" % ( count ), rating )
  125. self.WINDOW.setProperty( "RandomMovie.%d.Year" % ( count ), year)
  126. self.WINDOW.setProperty( "RandomMovie.%d.Plot" % ( count ), plot )
  127. self.WINDOW.setProperty( "RandomMovie.%d.RunningTime" % ( count ), runtime )
  128. self.WINDOW.setProperty( "RandomMovie.%d.Path" % ( count ), path )
  129. self.WINDOW.setProperty( "RandomMovie.%d.Trailer" % ( count ), trailer )
  130. self.WINDOW.setProperty( "RandomMovie.%d.Fanart" % ( count ), fanart )
  131. self.WINDOW.setProperty( "RandomMovie.%d.Thumb" % ( count ), thumb )
  132. self.WINDOW.setProperty( "RandomMovie.Count" , total )
  133.  
  134. def _fetch_episode_info( self ):
  135. # query the database
  136. tvshowid = 2
  137. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "params": { "properties": ["title", "playcount", "season", "episode", "showtitle", "plot", "fanart", "thumbnail", "file", "rating"] }, "id": 1}')
  138. json_query = unicode(json_query, 'utf-8', errors='ignore')
  139. # separate the records
  140. json_response = simplejson.loads(json_query)
  141. if json_response.has_key('result'):
  142. if (json_response['result'] != None) and (json_response['result'].has_key('episodes')):
  143. json_response = json_response['result']['episodes']
  144. # get total value
  145. total = str( len( json_response ) )
  146. # enumerate thru our records
  147. count = 0
  148. while count < self.LIMIT:
  149. count += 1
  150. # check if we don't run out of items before LIMIT is reached
  151. if len( json_response ) == 0:
  152. return
  153. # select a random item
  154. item = random.choice( json_response )
  155. # remove the item from our list
  156. json_response.remove( item )
  157. # find values
  158. if self.UNPLAYED == "True":
  159. playcount = item['playcount']
  160. if playcount > 0:
  161. count = count - 1
  162. continue
  163. title = item['title']
  164. showtitle = item['showtitle']
  165. season = "%.2d" % float(item['season'])
  166. episode = "%.2d" % float(item['episode'])
  167. rating = str(round(float(item['rating']),1))
  168. plot = item['plot']
  169. path = item['file']
  170. thumb = item['thumbnail']
  171. fanart = item['fanart']
  172. episodeno = "s%se%s" % ( season, episode, )
  173. # set our properties
  174. self.WINDOW.setProperty( "RandomEpisode.%d.ShowTitle" % ( count ), showtitle )
  175. self.WINDOW.setProperty( "RandomEpisode.%d.EpisodeTitle" % ( count ), title )
  176. self.WINDOW.setProperty( "RandomEpisode.%d.EpisodeNo" % ( count ), episodeno )
  177. self.WINDOW.setProperty( "RandomEpisode.%d.EpisodeSeason" % ( count ), season )
  178. self.WINDOW.setProperty( "RandomEpisode.%d.EpisodeNumber" % ( count ), episode )
  179. self.WINDOW.setProperty( "RandomEpisode.%d.Rating" % ( count ), rating )
  180. self.WINDOW.setProperty( "RandomEpisode.%d.Plot" % ( count ), plot )
  181. self.WINDOW.setProperty( "RandomEpisode.%d.Path" % ( count ), path )
  182. self.WINDOW.setProperty( "RandomEpisode.%d.Fanart" % ( count ), fanart )
  183. self.WINDOW.setProperty( "RandomEpisode.%d.Thumb" % ( count ), thumb )
  184. self.WINDOW.setProperty( "RandomEpisode.Count" , total )
  185.  
  186. def _fetch_musicvideo_info( self ):
  187. # query the database
  188. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMusicVideos", "params": {"properties": ["title", "artist", "playcount", "year", "plot", "runtime", "fanart", "thumbnail", "file"] }, "id": 1}')
  189. json_query = unicode(json_query, 'utf-8', errors='ignore')
  190. # separate the records
  191. json_response = simplejson.loads(json_query)
  192. if json_response.has_key('result'):
  193. if (json_response['result'] != None) and (json_response['result'].has_key('musicvideos')):
  194. json_response = json_response['result']['musicvideos']
  195. # get total value
  196. total = str( len( json_response ) )
  197. # enumerate thru our records
  198. count = 0
  199. while count < self.LIMIT:
  200. count += 1
  201. # check if we don't run out of items before LIMIT is reached
  202. if len( json_response ) == 0:
  203. return
  204. # select a random item
  205. item = random.choice( json_response )
  206. # remove the item from our list
  207. json_response.remove( item )
  208. # find values
  209. if self.UNPLAYED == "True":
  210. playcount = item['playcount']
  211. if playcount > 0:
  212. count = count - 1
  213. continue
  214. title = item['title']
  215. year = str(item['year'])
  216. plot = item['plot']
  217. runtime = item['runtime']
  218. path = item['file']
  219. artist = item['artist']
  220. thumb = item['thumbnail']
  221. fanart = item['fanart']
  222. # set our properties
  223. self.WINDOW.setProperty( "RandomMusicVideo.%d.Title" % ( count ), title )
  224. self.WINDOW.setProperty( "RandomMusicVideo.%d.Year" % ( count ), year)
  225. self.WINDOW.setProperty( "RandomMusicVideo.%d.Plot" % ( count ), plot )
  226. self.WINDOW.setProperty( "RandomMusicVideo.%d.RunningTime" % ( count ), runtime )
  227. self.WINDOW.setProperty( "RandomMusicVideo.%d.Path" % ( count ), path )
  228. self.WINDOW.setProperty( "RandomMusicVideo.%d.Fanart" % ( count ), fanart )
  229. self.WINDOW.setProperty( "RandomMusicVideo.%d.Artist" % ( count ), artist )
  230. self.WINDOW.setProperty( "RandomMusicVideo.%d.Thumb" % ( count ), thumb )
  231. self.WINDOW.setProperty( "RandomMusicVideo.Count" , total )
  232.  
  233. def _fetch_album_info( self ):
  234. # query the database
  235. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", "params": {"properties": ["title", "description", "artist", "year", "thumbnail", "fanart", "rating"] }, "id": 1}')
  236. json_query = unicode(json_query, 'utf-8', errors='ignore')
  237. # separate the records
  238. json_response = simplejson.loads(json_query)
  239. if json_response.has_key('result'):
  240. if (json_response['result'] != None) and (json_response['result'].has_key('albums')):
  241. json_response = json_response['result']['albums']
  242. # get total value
  243. total = str( len( json_response ) )
  244. # enumerate thru our records
  245. count = 0
  246. while count < self.LIMIT:
  247. count += 1
  248. # check if we don't run out of items before LIMIT is reached
  249. if len( json_response ) == 0:
  250. return
  251. # select a random item
  252. item = random.choice( json_response )
  253. # remove the item from our list
  254. json_response.remove( item )
  255. # find values
  256. title = item['title']
  257. description = item['description']
  258. rating = str(item['rating'])
  259. if rating == '48':
  260. rating = ""
  261. year = str(item['year'])
  262. artist = item['artist']
  263. path = 'XBMC.RunScript(' + __addonid__ + ',albumid=' + str(item['albumid']) + ')'
  264. fanart = item['fanart']
  265. thumb = item['thumbnail']
  266. # set our properties
  267. self.WINDOW.setProperty( "RandomAlbum.%d.Title" % ( count ), title )
  268. self.WINDOW.setProperty( "RandomAlbum.%d.Rating" % ( count ), rating )
  269. self.WINDOW.setProperty( "RandomAlbum.%d.Year" % ( count ), year )
  270. self.WINDOW.setProperty( "RandomAlbum.%d.Artist" % ( count ), artist )
  271. self.WINDOW.setProperty( "RandomAlbum.%d.Path" % ( count ), path )
  272. self.WINDOW.setProperty( "RandomAlbum.%d.Fanart" % ( count ), fanart )
  273. self.WINDOW.setProperty( "RandomAlbum.%d.Thumb" % ( count ), thumb )
  274. self.WINDOW.setProperty( "RandomAlbum.%d.Album_Description" % ( count ), description )
  275. self.WINDOW.setProperty( "RandomAlbum.Count" , total )
  276.  
  277. def _fetch_artist_info( self ):
  278. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtists", "params": {"properties": ["genre", "description", "fanart", "thumbnail"], "sort": { "method": "label" } }, "id": 1}')
  279. json_query = unicode(json_query, 'utf-8', errors='ignore')
  280. json_response = simplejson.loads(json_query)
  281. if json_response.has_key('result'):
  282. if (json_response['result'] != None) and (json_response['result'].has_key('artists')):
  283. json_response = json_response['result']['artists']
  284. # get total value
  285. total = str( len( json_response ) )
  286. # enumerate thru our records
  287. count = 0
  288. while count < self.LIMIT:
  289. count += 1
  290. # check if we don't run out of items before LIMIT is reached
  291. if len( json_response ) == 0:
  292. return
  293. # select a random item
  294. item = random.choice( json_response )
  295. # remove the item from our list
  296. json_response.remove( item )
  297. # find values
  298. description = item['description']
  299. genre = str(item['genre'])
  300. artist = item['label']
  301. path = 'musicdb://2/' + str(item['artistid']) + '/'
  302. fanart = item['fanart']
  303. thumb = item['thumbnail']
  304. # set our properties
  305. self.WINDOW.setProperty( "RandomArtist.%d.Title" % ( count ), artist )
  306. self.WINDOW.setProperty( "RandomArtist.%d.Genre" % ( count ), genre )
  307. self.WINDOW.setProperty( "RandomArtist.%d.Path" % ( count ), path )
  308. self.WINDOW.setProperty( "RandomArtist.%d.Fanart" % ( count ), fanart )
  309. self.WINDOW.setProperty( "RandomArtist.%d.Thumb" % ( count ), thumb )
  310. self.WINDOW.setProperty( "RandomArtist.%d.Artist_Description" % ( count ), description )
  311. self.WINDOW.setProperty( "RandomArtist.Count" , total )
  312.  
  313. def _fetch_song_info( self ):
  314. # query the database
  315. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetSongs", "params": {"properties": ["title", "playcount", "artist", "album", "year", "file", "thumbnail", "fanart", "rating"] }, "id": 1}')
  316. json_query = unicode(json_query, 'utf-8', errors='ignore')
  317. # separate the records
  318. json_response = simplejson.loads(json_query)
  319. if json_response.has_key('result'):
  320. if (json_response['result'] != None) and (json_response['result'].has_key('songs')):
  321. json_response = json_response['result']['songs']
  322. # get total value
  323. total = str( len( json_response ) )
  324. # enumerate thru our records
  325. count = 0
  326. while count < self.LIMIT:
  327. count += 1
  328. # check if we don't run out of items before LIMIT is reached
  329. if len( json_response ) == 0:
  330. return
  331. # select a random item
  332. item = random.choice( json_response )
  333. # remove the item from our list
  334. json_response.remove( item )
  335. # find values
  336. if self.UNPLAYED == "True":
  337. playcount = item['playcount']
  338. if playcount > 0:
  339. count = count - 1
  340. continue
  341. title = item['title']
  342. rating = str(int(item['rating'])-48)
  343. year = str(item['year'])
  344. artist = item['artist']
  345. album = item['album']
  346. path = item['file']
  347. fanart = item['fanart']
  348. thumb = item['thumbnail']
  349. # set our properties
  350. self.WINDOW.setProperty( "RandomSong.%d.Title" % ( count ), title )
  351. self.WINDOW.setProperty( "RandomSong.%d.Rating" % ( count ), rating )
  352. self.WINDOW.setProperty( "RandomSong.%d.Year" % ( count ), year )
  353. self.WINDOW.setProperty( "RandomSong.%d.Artist" % ( count ), artist )
  354. self.WINDOW.setProperty( "RandomSong.%d.Album" % ( count ), album )
  355. self.WINDOW.setProperty( "RandomSong.%d.Path" % ( count ), path )
  356. self.WINDOW.setProperty( "RandomSong.%d.Fanart" % ( count ), fanart )
  357. self.WINDOW.setProperty( "RandomSong.%d.Thumb" % ( count ), thumb )
  358. self.WINDOW.setProperty( "RandomSong.Count" , total )
  359.  
  360. def _fetch_addon_info( self ):
  361. # initialize our list
  362. addonlist = []
  363. # list the contents of the addons folder
  364. addonpath = xbmc.translatePath( 'special://home/addons/' )
  365. addons = os.listdir(addonpath)
  366. # find directories in the addons folder
  367. for item in addons:
  368. if os.path.isdir(os.path.join(addonpath, item)):
  369. # find addon.xml in the addon folder
  370.  
  371. addonfile = os.path.join(addonpath, item, 'addon.xml')
  372. print '*********************************************************************'
  373. print addonfile
  374. if os.path.exists(addonfile):
  375. # find addon id
  376. addonfilecontents = xmltree.parse(addonfile).getroot()
  377. for element in addonfilecontents.getiterator():
  378. if element.tag == "addon":
  379. addonid = element.attrib.get('id')
  380. elif element.tag == "provides":
  381. addonprovides = element.text
  382. # find plugins and scripts
  383. try:
  384. addontype = xbmcaddon.Addon(id=addonid).getAddonInfo('type')
  385. if (addontype == 'xbmc.python.script') or (addontype == 'xbmc.python.pluginsource'):
  386. addonlist.append( (addonid, addonprovides) )
  387. except:
  388. pass
  389. # get total value
  390. total = str( len( addonlist ) )
  391. # count thru our addons
  392. count = 0
  393. while count < self.LIMIT:
  394. count += 1
  395. # check if we don't run out of items before LIMIT is reached
  396. if len(addonlist) == 0:
  397. return
  398. # select a random item
  399. addonid = random.choice(addonlist)
  400. # remove the item from our list
  401. addonlist.remove(addonid)
  402. # set properties
  403. self.WINDOW.setProperty( "RandomAddon.%d.Name" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('name') )
  404. self.WINDOW.setProperty( "RandomAddon.%d.Author" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('author') )
  405. self.WINDOW.setProperty( "RandomAddon.%d.Summary" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('summary') )
  406. self.WINDOW.setProperty( "RandomAddon.%d.Version" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('version') )
  407. self.WINDOW.setProperty( "RandomAddon.%d.Path" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('id') )
  408. self.WINDOW.setProperty( "RandomAddon.%d.Fanart" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('fanart') )
  409. self.WINDOW.setProperty( "RandomAddon.%d.Thumb" % ( count ), xbmcaddon.Addon(id=addonid[0]).getAddonInfo('icon') )
  410. self.WINDOW.setProperty( "RandomAddon.%d.Type" % ( count ), addonid[1] )
  411. self.WINDOW.setProperty( "RandomAddon.Count" , total )
  412.  
  413. def _Play_Album( self, ID ):
  414. # create a playlist
  415. playlist = xbmc.PlayList(0)
  416. # clear the playlist
  417. playlist.clear()
  418. # query the database
  419. json_query = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetSongs", "params": {"properties": ["file", "fanart"], "albumid":%s }, "id": 1}' % ID)
  420. json_query = unicode(json_query, 'utf-8', errors='ignore')
  421. # separate the records
  422. json_response = simplejson.loads(json_query)
  423. # enumerate thru our records
  424. if json_response.has_key('result'):
  425. if (json_response['result'] != None) and (json_response['result'].has_key('songs')):
  426. for item in json_response['result']['songs']:
  427. song = item['file']
  428. fanart = item['fanart']
  429. listitem = xbmcgui.ListItem()
  430. listitem.setProperty( "fanart_image", fanart )
  431. playlist.add( url=song, listitem=listitem )
  432. # play the playlist
  433. xbmc.Player().play( playlist )
  434.  
  435. if ( __name__ == "__main__" ):
  436. log('script version %s started' % __addonversion__)
  437. Main()
  438. log('script stopped')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement