this patch adds the fetching of database totals to the script.
these totals are the same as found in the recentlyadded script.

i hope this gets accepted.

cheers,
ronie
--- script.randomitems/RandomItems.py	2010-10-18 11:41:30.000000000 +0200
+++ RandomItems.py	2010-11-03 15:35:55.476548000 +0100
@@ -6,12 +6,13 @@
 import os
 import random
 
-
 class Main:
     # grab the home window
     WINDOW = Window( 10000 )
 
     def _clear_properties( self ):
+        # reset Totals property for visible condition
+        self.WINDOW.clearProperty( "Database.Totals" )
         # we enumerate thru and clear individual properties in case other scripts set window properties
         for count in range( self.LIMIT ):
             # we clear title for visible condition
@@ -43,6 +44,7 @@
         self.LIMIT = int( params.get( "limit", "5" ) )
         self.ALBUMS = params.get( "albums", "" ) == "True"
         self.UNPLAYED = params.get( "unplayed", "" ) == "True"
+        self.TOTALS = params.get( "totals", "" ) == "True"
         self.PLAY_TRAILER = params.get( "trailer", "" ) == "True"
         self.RANDOM_ORDER = "True"
 
@@ -56,9 +58,86 @@
         xbmc.executehttpapi( "SetResponseFormat(OpenRecord,%s)" % ( "<record>", ) )
         xbmc.executehttpapi( "SetResponseFormat(CloseRecord,%s)" % ( "</record>", ) )
         # fetch media info
+        self._fetch_totals()
         self._fetch_movie_info()
         self._fetch_tvshow_info()
         self._fetch_music_info()
+
+    def _fetch_totals( self ):
+        # only run if user/skinner preference
+        if ( not self.TOTALS ): return
+        import datetime
+        # get our regions format
+        date_format = xbmc.getRegion( "dateshort" ).replace( "MM", "%m" ).replace( "DD", "%d" ).replace( "YYYYY", "%Y" ).replace( "YYYY", "%Y" ).strip()
+        # only need to make Totals not empty
+        self.WINDOW.setProperty( "Database.Totals", "true" )
+        # sql statement for movie totals
+        sql_totals = "select count(1), count(playCount), movieview.* from movieview group by lastPlayed"
+        totals_xml = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % quote_plus( sql_totals ), )
+        records = re.findall( "<record>(.+?)</record>", totals_xml, re.DOTALL )
+        # initialize our list
+        movies_totals = [ 0 ] * 7
+        # enumerate thru and total our numbers
+        for record in records:
+            fields = re.findall( "<field>(.*?)</field>", record, re.DOTALL )
+            movies_totals[ 0 ] += int( fields[ 0 ] )
+            movies_totals[ 1 ] += int( fields[ 1 ] )
+            if ( fields[ 29 ] ):
+                movies_totals[ 2 ] = fields[ 3 ] # title
+                movies_totals[ 3 ] = fields[ 10 ] # year
+                movies_totals[ 4 ] = fields[ 14 ] # runningtime
+                movies_totals[ 5 ] = fields[ 17 ] # genre
+                movies_totals[ 6 ] = "" # last watched
+                date = fields[ 29 ].split( " " )[ 0 ].split( "-" )
+                movies_totals[ 6 ] = datetime.date( int( date[ 0 ] ), int( date[ 1 ] ), int( date[ 2 ] ) ).strftime( date_format ) # last played
+        # sql statement for music videos totals
+        sql_totals = "select count(1), count(playCount) from musicvideoview"
+        totals_xml = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % quote_plus( sql_totals ), )
+        mvideos_totals = re.findall( "<field>(.+?)</field>", totals_xml, re.DOTALL )
+        # sql statement for tv shows/episodes totals
+        sql_totals = "SELECT tvshow.*, path.strPath AS strPath, counts.totalcount AS totalCount, counts.watchedcount AS watchedCount, counts.totalcount=counts.watchedcount AS watched FROM tvshow JOIN tvshowlinkpath ON tvshow.idShow=tvshowlinkpath.idShow JOIN path ON path.idpath=tvshowlinkpath.idPath LEFT OUTER join (SELECT tvshow.idShow AS idShow, count(1) AS totalCount, count(files.playCount) AS watchedCount FROM tvshow JOIN tvshowlinkepisode ON tvshow.idShow=tvshowlinkepisode.idShow JOIN episode ON episode.idEpisode=tvshowlinkepisode.idEpisode JOIN files ON files.idFile=episode.idFile GROUP BY tvshow.idShow) counts ON tvshow.idShow=counts.idShow"
+        totals_xml = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % quote_plus( sql_totals ), )
+        # initialize our list
+        tvshows_totals = [ 0 ] * 4
+        records = re.findall( "<record>(.+?)</record>", totals_xml, re.DOTALL )
+        # enumerate thru and total our numbers
+        for record in records:
+            fields = re.findall( "<field>(.*?)</field>", record, re.DOTALL )
+            if ( fields[ 25 ] ):
+                tvshows_totals[ 0 ] += 1
+                tvshows_totals[ 1 ] += int( fields[ 24 ] ) # number of episodes
+                tvshows_totals[ 2 ] += int( fields[ 26 ] ) # watched?
+                tvshows_totals[ 3 ] += int( fields[ 25 ] ) # number of episodes watched
+         # sql statement for tv albums/songs totals
+
+        sql_totals = "select count(1), count(distinct strAlbum), count(distinct strArtist) from songview"
+        totals_xml = xbmc.executehttpapi( "QueryMusicDatabase(%s)" % quote_plus( sql_totals ), )
+        music_totals = re.findall( "<field>(.+?)</field>", totals_xml, re.DOTALL )
+        
+        # set properties
+        self.WINDOW.setProperty( "Movies.Count" , str( movies_totals[ 0 ] ) or "" )
+        self.WINDOW.setProperty( "Movies.Watched" , str( movies_totals[ 1 ] ) or "" )
+        self.WINDOW.setProperty( "Movies.UnWatched" , str( movies_totals[ 0 ] - movies_totals[ 1 ] ) or "" )
+        self.WINDOW.setProperty( "Movies.LastWatchedTitle" , movies_totals[ 2 ] or "" )
+        self.WINDOW.setProperty( "Movies.LastWatchedYear" , movies_totals[ 3 ] or "" )
+        self.WINDOW.setProperty( "Movies.LastWatchedRuntime" , movies_totals[ 4 ] or "" )
+        self.WINDOW.setProperty( "Movies.LastWatchedGenre" , movies_totals[ 5 ] or "" )
+        self.WINDOW.setProperty( "Movies.LastWatchedDate" , movies_totals[ 6 ] or "" )
+        
+        self.WINDOW.setProperty( "MusicVideos.Count" , mvideos_totals[ 0 ] or "" )
+        self.WINDOW.setProperty( "MusicVideos.Watched" , mvideos_totals[ 1 ] or "" )
+        self.WINDOW.setProperty( "MusicVideos.UnWatched" , str( int( mvideos_totals[ 0 ] ) - int( mvideos_totals[ 1 ] ) ) or "" )
+        
+        self.WINDOW.setProperty( "TVShows.Count" , str( tvshows_totals[ 0 ] ) or "" )
+        self.WINDOW.setProperty( "TVShows.Watched" , str( tvshows_totals[ 2 ] ) or "" )
+        self.WINDOW.setProperty( "TVShows.UnWatched" , str( tvshows_totals[ 0 ] - tvshows_totals[ 2 ] ) or "" )
+        self.WINDOW.setProperty( "Episodes.Count" , str( tvshows_totals[ 1 ] ) or "" )
+        self.WINDOW.setProperty( "Episodes.Watched" , str( tvshows_totals[ 3 ] ) or "" )
+        self.WINDOW.setProperty( "Episodes.UnWatched" , str( tvshows_totals[ 1 ] - tvshows_totals[ 3 ] ) or "" )
+        
+        self.WINDOW.setProperty( "Music.SongsCount" , music_totals[ 0 ] or "" )
+        self.WINDOW.setProperty( "Music.AlbumsCount" , music_totals[ 1 ] or "" )
+        self.WINDOW.setProperty( "Music.ArtistsCount" , music_totals[ 2 ] or "" )
     
     def _fetch_movie_info( self ):
         # set our unplayed query
------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Xbmc-addons mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/xbmc-addons

Reply via email to