def checkSync(): # Check for Sync server svrfile = openNosyncDB() # Open server database curps = svrfile.execute('SELECT controlUrl FROM mServers WHERE mSync=?', ('Yes', )) srvrtuple = curps.fetchone() # Get server from database if srvrtuple: syncurl = srvrtuple[0] else: contenturl = settings('contenturl') # Check for content URL curpc = svrfile.execute('SELECT srvName, sIcon FROM mServers WHERE controlUrl=?', \ (contenturl,)) srvrtuple = curpc.fetchone() # Get server from database if srvrtuple: # Auto update if Mezzmo found syncurl = contenturl sname = srvrtuple[0] if updateSync(contenturl) == 0: # Update sync server flag iconimage = srvrtuple[1] msynclog = 'Mezzmo sync server updated automatically: ' + str( sname) xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) notify = xbmcgui.Dialog().notification(translate(30401), msynclog, addon_icon, 5000) else: # Sync srver not set yet syncurl = 'None' svrfile.close() return syncurl
def checkDailySync(): currhour = datetime.datetime.now().strftime('%H') syncflag = media.settings('dailysync') if syncflag != '': dailysync = int(syncflag) else: dailysync = 0 media.settings('dailysync', '0') # Set daily sync flag xbmc.log('Mezzmo initial daily sync flag is: ' + str(dailysync), xbmc.LOGDEBUG) if int(currhour) > 5 and dailysync <> 0: dailysync = 0 # Reset daily sync flag media.settings('dailysync', str(dailysync)) msynclog = 'Mezzmo daily sync process flag reset.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) elif int(currhour) >= 0 and int(currhour) <= 5 and dailysync == 0: dailysync = 1 # Set daily sync flag if not run yet msynclog = 'Mezzmo daily sync process flag set.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) elif int(currhour) >= 0 and int(currhour) <= 5 and dailysync == 1: dailysync = 0 xbmc.log('Mezzmo final daily sync flag is: ' + str(dailysync), xbmc.LOGDEBUG) return(dailysync)
def clearServers(): # Clear server data try: svrfile = openNosyncDB() # Open server database svrfile.execute('DELETE FROM mServers', ) svrfile.commit() svrfile.close() msynclog = 'Mezzmo sync servers cleared.' xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) except Exception as e: printexception() msynclog = 'Mezzmo sync server clearing error.' xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog)
def updateSync(controlurl): # Set sync for Mezzmo server try: svrfile = openNosyncDB() # Open server database svrfile.execute('UPDATE mServers SET mSync=?', ('No', )) xbmc.log('Mezzmo controlurl is: ' + controlurl, xbmc.LOGDEBUG) svrfile.execute('UPDATE mServers SET mSync=? WHERE controlUrl=?', ( 'Yes', controlurl, )) svrfile.commit() svrfile.close() return 0 except Exception as e: printexception() msynclog = 'Mezzmo sync server update error.' xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) return 1
def displayServers(): svrfile = openNosyncDB() # Open server database while True: servers = ["Refresh"] curps = svrfile.execute( 'SELECT srvName, mSync, sManuf, controlUrl FROM mServers \ WHERE sManuf LIKE ?', ('Conceiva%', )) srvresults = curps.fetchall() # Get servers from database for a in range(len(srvresults)): if srvresults[a][1] == 'Yes': # Is sync server ? syncserver = srvresults[a][ 0] + ' - [COLOR blue]Sync Server[/COLOR]' servers.append(syncserver) elif srvresults[a][1] == 'No': servers.append( srvresults[a][0]) # Convert rows to list for dialog box ddialog = xbmcgui.Dialog() ssync = ddialog.select(translate(30392), servers) if ssync < 0: # User cancel svrfile.close() break elif ssync == 0: # Refresh uPNP server list clearServers() mscount = getServers() if mscount == 0: msynclog = translate(30399) xbmcgui.Dialog().ok(translate(30398), msynclog) xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) else: msynclog = 'Mezzmo refresh sync servers found: ' + str(mscount) xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) elif ssync > 0: if updateSync(srvresults[ssync - 1] [3]) == 0: # Update sync server from selection msynclog ='Mezzmo sync server updated manually: ' + \ str(srvresults[ssync - 1][0]) xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) svrfile.close() return
media.mgenlogUpdate(mgenlog) mgenlog = 'A music file is playing at: ' + time.strftime( "%H:%M:%S", time.gmtime(pos)) media.mgenlogUpdate(mgenlog) elif contenturl == 'none': mgenlog = 'Mezzmo no servers selected yet. Cache update process skipped.' xbmc.log(mgenlog, xbmc.LOGNOTICE) media.mgenlogUpdate(mgenlog) else: sync.updateTexturesCache(contenturl) if count % 3600 == 0 or count == 11: # Mezzmo sync process if xbmc.Player().isPlayingVideo(): msynclog = 'Mezzmo sync skipped. A video is playing.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) else: syncpin = media.settings('content_pin') syncset = media.settings('kodisyncvar') syncurl = checkSync() # Get server control URL xbmc.log('Mezzmo contenturl is: ' + str(syncurl), xbmc.LOGDEBUG) if syncpin and syncset != 'Off' and syncurl != 'None': try: sync.syncMezzmo(syncurl, syncpin, count) except: msynclog = 'Mezzmo sync process failed with an exception error.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) pass elif syncset != 'Off' and syncurl == 'None': # Ensure Mezzmo server has been selected msynclog = 'Mezzmo no servers selected yet. Mezzmo sync skipped.'
def updateRealtime(mrecords, krecords, mlvcount, mnsyncount): # Disable real time updates when 90% sync achieved mezzmorecords = mrecords - mlvcount - mnsyncount # Calculate adjusted Mezzmo records if krecords >= mezzmorecords: # 100% maximum is Kodi > Mezzmo completepct = 100.0 elif krecords > 0 and mezzmorecords > 0: # Calculate % sync completepct = 1 / (float(mezzmorecords)/krecords) * 100.0 else: completepct = 0 if int(mezzmorecords * .9) > krecords: # Check if in sync media.settings('kodiactor', 'true') # Enable real time updates media.settings('kodichange', 'true') msynclog = 'Mezzmo sync process not yet in sync at {:.1f}'.format(completepct) + \ '%. Real time updates enabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) else: media.settings('kodiactor', 'false') # Disable real time updates media.settings('kodichange', 'false') msynclog = 'Mezzmo sync process in sync at {:.1f}'.format(completepct) + \ '%. Real time updates disabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) kodisync = media.settings('kodisync') # Check Kodi auto sync mksync = media.settings('kodisyncvar') # Get sync setting if kodisync == 'true': if int(completepct) > 90 and mksync != 'Off' \ and mksync != 'Newest': # Set to Newest > 90% media.settings('kodisyncvar', 'Newest') msynclog = 'Mezzmo autosync set to Newest.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) elif int(completepct) <= 90 and mksync != 'Off' \ and mksync != 'Normal': # Set to Normal <= 90% media.settings('kodisyncvar', 'Normal') msynclog = 'Mezzmo autosync set to Normal.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) else: msynclog = 'Mezzmo autosync is enabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) else: msynclog = 'Mezzmo autosync is disabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog)
def syncMezzmo(syncurl, syncpin, count): # Sync Mezzmo to Kodi global syncoffset, dupelog ksync = media.settings('kodisyncvar') # Get sync setting if 'ContentDirectory' not in syncurl: msynclog = 'Mezzmo sync process aborted. Invalid server selected.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) return if ksync != 'Off': # Check if enabled msynclog = 'Mezzmo sync beginning.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) starttime = time.time() rows = 0 newoffset = media.settings('sync_offset') # Get saved offset setting try: syncoffset = int(newoffset) except: syncoffset = 0 media.settings('sync_offset', str(syncoffset)) clean = checkDailySync() # Check sync flag if clean == 1 and count > 12: force = 1 media.kodiCleanDB(force) # Clear Kodi database daily if count < 12: content = browse.Browse(syncurl, 'recent', 'BrowseDirectChildren', 0, 400, syncpin) rows = syncContent(content, syncurl, 'recent', syncpin, 0, 400) recs = media.countKodiRecs(syncurl) # Get record count in Kodi DB recscount = media.countsyncCount() # Get nosync record count in nosync DB nsyncount = recscount[0] lvcount = recscount[1] # Get Live Channel record count in nosync DB msynclog = 'Mezzmo total Mezzmo record count: ' + str(mezzmorecs) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total Live Channels count: ' + str(lvcount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total nosync videos count: ' + str(nsyncount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) updateRealtime(mezzmorecs, recs, lvcount, nsyncount) elif clean == 0 and ksync != 'Daily': # Hourly sync set of records content = browse.Browse(syncurl, 'recent', 'BrowseDirectChildren', 0, 400, syncpin) rows = syncContent(content, syncurl, 'recent', syncpin, 0, 400) if rows == None: # Did sync get data from Mezzmo server ? rows = 0 msynclog = 'Mezzmo sync process could not contact the Mezzmo server' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) xbmc.log('Mezzmo sync offset = ' + str(syncoffset), xbmc.LOGDEBUG) if rows == 400 and syncoffset % 400 == 0 \ and ksync != 'Newest': # Mezzmo database > 400 records syncoffset = syncoffset + rows - 400 if mezzmorecs > 5000: # Faster hourly sync for large databases fetch = (mezzmorecs // 800) // 6 * 800 else: # Number of records to get from Mezzmo fetch = 800 # Slow hourly fetch itemsleft = (mezzmorecs - syncoffset) # Items remaining in Mezzmo if itemsleft < fetch: # Detect end of Mezzmo database fetch = itemsleft xbmc.log('Mezzmo fetch = ' + str(fetch), xbmc.LOGDEBUG) content = browse.Browse(syncurl, 'recent', 'BrowseDirectChildren', syncoffset, fetch, syncpin) rows1 = syncContent(content, syncurl, 'recent', syncpin, syncoffset, fetch) xbmc.log('Mezzmo sync rows1 = ' + str(rows1), xbmc.LOGDEBUG) if rows1 != None and rows1 > 0: syncoffset = syncoffset + rows1 rows = rows + rows1 else: syncoffset = 400 xbmc.log('Mezzmo sync rows test = ' + str(rows), xbmc.LOGDEBUG) if rows % 400 <> 0: # Start back through the Mezzmo database syncoffset = 400 recs = media.countKodiRecs(syncurl) # Get record count in Kodi DB recscount = media.countsyncCount() # Get nosync record count in nosync DB nsyncount = recscount[0] lvcount = recscount[1] # Get Live Channel record count in nosync DB msynclog = 'Mezzmo total Mezzmo record count: ' + str(mezzmorecs) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total Live Channels count: ' + str(lvcount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total nosync videos count: ' + str(nsyncount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) updateRealtime(mezzmorecs, recs, lvcount, nsyncount) elif clean == 1: # Sync all daily media.settings('kodiactor', 'false') # Disable real time updating ahead of full sync media.settings('kodichange', 'false') dupelog = media.settings('mdupelog') # Check if Mezzmo duplicate logging is enabled if dupelog == 'true': msynclog = 'Mezzmo duplicate logging is enabled. ' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) syncoffset = 0 lvcount = 0 # Reset live channel skip counter nsyncount = 0 # Reset nosync skip counter content = browse.Browse(syncurl, 'recent', 'BrowseDirectChildren', 0, 1000, syncpin) rows = syncContent(content, syncurl, 'recent', syncpin, 0, 1000) recs = media.countKodiRecs(syncurl) # Get record count in Kodi DB recscount = media.countsyncCount() # Get nosync record count in nosync DB nsyncount = recscount[0] lvcount = recscount[1] # Get Live Channel record count in nosync DB msynclog = 'Mezzmo total Mezzmo record count: ' + str(mezzmorecs) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total Live Channels count: ' + str(lvcount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) msynclog = 'Mezzmo total nosync videos count: ' + str(nsyncount) xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) media.optimizeDB() # Optimize DB after resync media.settings('dailysync', '1') # Set daily sync flag endtime = time.time() duration = endtime-starttime difference = str(int(duration // 60)) + 'm ' + str(int(duration % 60)) + 's checked.' media.settings('sync_offset', str(syncoffset)) dupelog = 'false' # Set Mezzmo duplicate logging to disable if ksync != 'Daily' or count < 30 or clean == 1: # Display summary stats if restart or not daily msynclog = 'Mezzmo sync completed. ' + str(rows) + ' videos in ' + difference elif ksync == 'Daily': msynclog = 'Mezzo sync Daily set. Hourly sync not enabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) if ksync != 'Daily': msynclog = 'Mezzmo sync setting is: ' + media.settings('kodisyncvar') xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog) if media.settings('perflog') == 'true': # Check if performance logging is enabled msynclog = 'Mezzmo performance logging is enabled.' xbmc.log(msynclog, xbmc.LOGNOTICE) media.mezlogUpdate(msynclog)
def getServers(): # Find uPNP servers try: timeoutval = float(settings('ssdp_timeout')) contenturl = '' msgdialogprogress = xbmcgui.DialogProgress() dialogmsg = translate(30402) dialoghead = translate(30401) msgdialogprogress.create(dialoghead, dialogmsg) servers = ssdp.discover("urn:schemas-upnp-org:device:MediaServer:1", timeout=timeoutval) srvcount = len(servers) addtlmsg = ' ' + str(srvcount) + ' uPNP servers discovered.' ddialogmsg = dialogmsg + addtlmsg msgdialogprogress.update(50, ddialogmsg) xbmc.sleep(1000) if srvcount > 0: msynclog = 'Mezzmo sync server search: ' + str( srvcount) + ' uPNP servers found.' xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) else: msynclog = translate(30403) xbmcgui.Dialog().notification(translate(30401), msynclog, addon_icon, 5000) xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) return 0 onlyShowMezzmo = False a = 0 mcount = 0 # Count of Mezzmo serves found for server in servers: url = server.location try: response = urllib2.urlopen(url) xmlstring = re.sub(' xmlns="[^"]+"', '', response.read(), count=1) e = xml.etree.ElementTree.fromstring(xmlstring) device = e.find('device') friendlyname = device.find('friendlyName').text manufacturer = device.find('manufacturer').text if manufacturer != None: manufacturer = manufacturer.text else: manufacturer = 'None' modelnumber = device.find('modelNumber') if modelnumber != None: modelnumber = modelnumber.text else: modelnumber = 'None' udn = device.find('UDN') if udn != None: udn = udn.text else: udn = 'None' description = device.find('modelDescription') if description != None: description = description.text else: description = 'None' serviceList = device.find('serviceList') iconList = device.find('iconList') iconurl = '' isMezzmo = False if manufacturer != None and manufacturer == 'Conceiva Pty. Ltd.': iconurl = addon_icon isMezzmo = True mcount += 1 elif iconList != None: bestWidth = 0 for icon in iconList.findall('icon'): mimetype = icon.find('mimetype').text width = icon.find('width').text height = icon.find('height').text width = int(width) if width > bestWidth: bestWidth = width iconurl = icon.find('url').text if iconurl.startswith('/'): end = url.find('/', 8) length = len(url) iconurl = url[:end - length] + iconurl else: end = url.rfind('/') length = len(url) iconurl = url[:end - length] + '/' + iconurl else: iconurl = addon_path + '/resources/media/otherserver.png' if isMezzmo or onlyShowMezzmo == False: contenturl = '' for service in serviceList.findall('service'): serviceId = service.find('serviceId') if serviceId.text == 'urn:upnp-org:serviceId:ContentDirectory': contenturl = service.find('controlURL').text if contenturl.startswith('/'): end = url.find('/', 8) length = len(url) contenturl = url[:end - length] + contenturl elif 'http' not in contenturl: end = url.rfind('/') length = len(url) contenturl = url[:end - length] + '/' + contenturl updateServers(url, friendlyname, contenturl, manufacturer, modelnumber, \ iconurl, description, udn) except urllib2.URLError, urllib2.HTTPError: # Detect Server Issues msynclog = 'Mezzmo uPNP server not responding: ' + url xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) dialog_text = translate(30405) + url xbmcgui.Dialog().ok(translate(30404), dialog_text) pass a += 1 percent = int(a / float(srvcount) * 50) + 50 dialogmsg = str(a) + ' / ' + str(srvcount) + ' server completed.' msgdialogprogress.update(percent, dialogmsg) xbmc.sleep(500) msgdialogprogress.close() return mcount
mezlogUpdate(msynclog) dialog_text = translate(30405) + url xbmcgui.Dialog().ok(translate(30404), dialog_text) pass a += 1 percent = int(a / float(srvcount) * 50) + 50 dialogmsg = str(a) + ' / ' + str(srvcount) + ' server completed.' msgdialogprogress.update(percent, dialogmsg) xbmc.sleep(500) msgdialogprogress.close() return mcount except Exception as e: printexception() msynclog = 'Mezzmo sync server discover error.' xbmc.log(msynclog, xbmc.LOGNOTICE) mezlogUpdate(msynclog) return 0 def getItemlUrl(contenturl, itemid): # Get itemurl for generic uPNP try: svrfile = openNosyncDB() # Open server database curps = svrfile.execute('SELECT sUdn FROM mServers WHERE controlUrl=?', (contenturl, )) srvrtuple = curps.fetchone() # Get server from database #xbmc.log('Mezzmo getItemUrl:' + str(srvrtuple[0]), xbmc.LOGNOTICE) if srvrtuple: itemurl = ('upnp://' + srvrtuple[0] + '/' + itemid).encode( 'utf-8', 'ignore') else: