def Sleep_If_Function_Active(function, args=[], kill_time=30, show_busy=True): """ This will allow you to pause code while a specific function is running in the background. CODE: Sleep_If_Function_Active(function, args, kill_time, show_busy) AVAILABLE PARAMS: function - This is the function you want to run. This does not require brackets, you only need the function name. args - These are the arguments you want to send through to the function, these need to be sent through as a list. kill_time - By default this is set to 30. This is the maximum time in seconds you want to wait for a response. If the max. time is reached before the function completes you will get a response of False. show_busy - By default this is set to True so you'll get a busy working dialog appear while the function is running. Set to false if you'd rather not have this. EXAMPLE CODE: def Open_Test_URL(url): koding.Open_URL(url) dialog.ok('SLEEP IF FUNCTION ACTIVE','We will now attempt to read a 20MB zip and then give up after 10 seconds.','Press OK to continue.') koding.Sleep_If_Function_Active(function=Open_Test_URL, args=['http://download.thinkbroadband.com/20MB.zip'], kill_time=10, show_busy=True) dialog.ok('FUNCTION COMPLETE','Of course we cannot read that file in just 10 seconds so we\'ve given up!') ~""" from guitools import Show_Busy import threading if show_busy: Show_Busy(True) my_thread = threading.Thread(target=function, args=args) my_thread.start() thread_alive = True counter = 0 while thread_alive and counter <= kill_time: xbmc.sleep(1000) thread_alive = my_thread.isAlive() xbmc.log('%s thread alive for %s seconds' % (function, counter)) counter += 1 if show_busy: Show_Busy(False) return thread_alive
def Check_Repo(repo,show_busy=True,timeout=10): """ This will check the status of repo and return True if the repo is online or False if it contains paths that are no longer accessible online. IMPORTANT: If you're running an old version of Kodi which uses the old Python 2.6 (OSX and Android lower than Kodi 17 or a linux install with old Python installed on system) you will get a return of False on https links regardless of their real status. This is due to the fact Python 2.6 cannot access secure links. Any still using standard http links will return the correct results. CODE: Check_Repo(repo, [show_busy, timeout]) AVAILABLE PARAMS: (*) repo - This is the name of the folder the repository resides in. You can either use the full path or just the folder name which in 99.99% of cases is the add-on id. If only using the folder name DOUBLE check first as there are a handful which have used a different folder name to the actual add-on id! show_busy - By default this is set to True and a busy dialog will show during the check timeout - By default this is set to 10 (seconds) - this is the maximum each request to the repo url will take before timing out and returning False. EXAMPLE CODE: repo_status = Check_Repo('repository.xxxecho',show_busy=False,timeout=10) if repo_status: dialog.ok('REPO STATUS','The repository modules4all is: [COLOR=lime]ONLINE[/COLOR]') else: dialog.ok('REPO STATUS','The repository modules4all is: [COLOR=red]OFFLINE[/COLOR]') ~""" import re from __init__ import dolog from filetools import Text_File from guitools import Show_Busy from web import Validate_Link dolog('### CHECKING %s'%repo) status = True if show_busy: Show_Busy() if repo.startswith('special://'): repo_path = xbmc.translatePath(repo) elif not ADDONS in repo and not XBMC_PATH in repo: repo_path = os.path.join(ADDONS,repo) else: repo_path = repo repo_path = os.path.join(repo_path,'addon.xml') dolog(repo_path) if os.path.exists(repo_path): content = Text_File(repo_path,'r') md5_urls = re.findall(r'<checksum>(.+?)</checksum>', content, re.DOTALL) for item in md5_urls: link_status = Validate_Link(item,timeout) dolog(item) dolog('STATUS: %s'%link_status) if link_status < 200 or link_status >= 400: status = False break if show_busy: Show_Busy(False) return status else: if show_busy: Show_Busy(False) return False
def Play_Video(video, showbusy=True, content='video', ignore_dp=False, timeout=10, item=None, player=xbmc.Player()): """ This will attempt to play a video and return True or False on whether or not playback was successful. This function is similar to Check_Playback but this actually tries a number of methods to play the video whereas Check_Playback does not actually try to play a video - it will just return True/False on whether or not a video is currently playing. If you have m3u or m3u8 playlist links please use the M3U_Selector function to get the final resolved url. CODE: Play_Video(video, [showbusy, content, ignore_dp, timeout, item]) AVAILABLE PARAMS: (*) video - This is the path to the video, this can be a local path, online path or a channel number from the PVR. showbusy - By default this is set to True which means while the function is attempting to playback the video the user will see the busy dialog. Set to False if you prefer this not to appear but do bare in mind a user may navigate to another section and try playing something else if they think this isn't doing anything. content - By default this is set to 'video', however if you're passing through audio you may want to set this to 'music' so the system can correctly set the tags for artist, song etc. ignore_dp - By default this is set to True but if set to False this will ignore the DialogProgress window. If you use a DP while waiting for the stream to start then you'll want to set this True. Please bare in mind the reason this check is in place and enabled by default is because some streams do bring up a DialogProgress when initiated (such as f4m proxy links) and disabling this check in those circumstances can cause false positives. timeout - This is the amount of time you want to allow for playback to start before sending back a response of False. Please note if ignore_dp is set to True then it will also add a potential 10s extra to this amount if a DialogProgress window is open. The default setting for this is 10s. item - By default this is set to None and in this case the metadata will be auto-populated from the previous Add_Dir so you'll just get the basics like title, thumb and description. If you want to send through your own metadata in the form of a dictionary you can do so and it will override the auto-generation. If anything else sent through no metadata will be set, you would use this option if you've already set metadata in a previous function. EXAMPLE CODE: isplaying = koding.Play_Video('http://totalrevolution.tv/videos/python_koding/Browse_To_Folder.mov') if isplaying: dialog.ok('PLAYBACK SUCCESSFUL','Congratulations, playback was successful') xbmc.Player().stop() else: dialog.ok('PLAYBACK FAILED','Sorry, playback failed :(') ~""" dolog('### ORIGINAL VIDEO: %s' % video) import urlresolver try: import simplejson as json except: import json if not item: meta = {} for i in [ 'title', 'originaltitle', 'tvshowtitle', 'year', 'season', 'episode', 'genre', 'rating', 'votes', 'director', 'writer', 'plot', 'tagline' ]: try: meta[i] = xbmc.getInfoLabel('listitem.%s' % i) except: pass meta = dict((k, v) for k, v in meta.iteritems() if not v == '') if 'title' not in meta: meta['title'] = xbmc.getInfoLabel('listitem.label') icon = xbmc.getInfoLabel('listitem.icon') item = xbmcgui.ListItem(path=video, iconImage=icon, thumbnailImage=icon) if content == "music": try: meta['artist'] = xbmc.getInfoLabel('listitem.artist') item.setInfo(type='Music', infoLabels={ 'title': meta['title'], 'artist': meta['artist'] }) except: item.setInfo(type='Video', infoLabels=meta) else: item.setInfo(type='Video', infoLabels=meta) elif type(item).__name__ == 'dict': item.setInfo(type='Video', infoLabels=meta) else: pass playback = False if showbusy: Show_Busy() # if a plugin path is sent we try activate window if video.startswith('plugin://'): try: dolog('Attempting to play via xbmc.Player().play() method') player.play(video) playback = Check_Playback(ignore_dp, timeout) except: dolog(Last_Error()) # If an XBMC action has been sent through we do an executebuiltin command elif video.startswith('ActivateWindow') or video.startswith( 'RunAddon') or video.startswith('RunScript') or video.startswith( 'PlayMedia'): try: dolog('Attempting to play via xbmc.executebuiltin method') xbmc.executebuiltin('%s' % video) playback = Check_Playback(ignore_dp, timeout) except: dolog(Last_Error()) elif ',' in video: # Standard xbmc.player method (a comma in url seems to throw urlresolver off) try: dolog('Attempting to play via xbmc.Player.play() method') player.play('%s' % video, item) playback = Check_Playback(ignore_dp, timeout) # Attempt to resolve via urlresolver except: try: dolog('Attempting to resolve via urlresolver module') dolog('video = %s' % video) hmf = urlresolver.HostedMediaFile(url=video, include_disabled=False, include_universal=True) if hmf.valid_url() == True: video = hmf.resolve() dolog('### VALID URL, RESOLVED: %s' % video) player.play('%s' % video, item) playback = Check_Playback(ignore_dp, timeout) except: dolog(Last_Error()) # Play from a db entry - untested elif video.isdigit(): dolog('### Video is digit, presuming it\'s a db item') command = ( '{"jsonrpc": "2.0", "id":"1", "method": "Player.Open","params":{"item":{"channelid":%s}}}' % url) xbmc.executeJSONRPC(command) playback = Check_Playback(ignore_dp, timeout) else: # Attempt to resolve via urlresolver try: dolog('Attempting to resolve via urlresolver module') dolog('video = %s' % video) hmf = urlresolver.HostedMediaFile(url=video, include_disabled=False, include_universal=True) if hmf.valid_url() == True: video = hmf.resolve() dolog('### VALID URL, RESOLVED: %s' % video) player.play('%s' % video, item) playback = Check_Playback(ignore_dp, timeout) # Standard xbmc.player method except: try: dolog('Attempting to play via xbmc.Player.play() method') player.play('%s' % video, item) playback = Check_Playback(ignore_dp, timeout) except: dolog(Last_Error()) dolog('Playback status: %s' % playback) Show_Busy(False) counter = 1 dialogprogress = xbmc.getCondVisibility('Window.IsActive(progressdialog)') if not ignore_dp: while dialogprogress: dp.create('Playback Good', 'Closing dialog...') dolog('Attempting to close dp #%s' % counter) dp.close() xbmc.sleep(1000) counter += 1 dialogprogress = xbmc.getCondVisibility( 'Window.IsActive(progressdialog)') return playback