def timeout(function, *args, **table): try: response = None f = repr(function) f = re.sub('.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f) a = hashlib.md5() for i in args: a.update(str(i)) a = str(a.hexdigest()) except: pass try: table = table['table'] except: table = 'rel_list' try: control.makeFile(control.dataPath) dbcon = database.connect(control.cacheFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) match = dbcur.fetchone() return int(match[3]) except: return
def add(url): try: data = json.loads(url) dbid = hashlib.md5() for i in data['bookmark']: dbid.update(str(i)) for i in data['action']: dbid.update(str(i)) dbid = str(dbid.hexdigest()) item = dict((k, v) for k, v in data.iteritems() if not k == 'bookmark') item = repr(item) control.makeFile(control.dataPath) dbcon = database.connect(control.bookmarksFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS bookmark (" "dbid TEXT, " "item TEXT, " "UNIQUE(dbid)" ");") dbcur.execute("DELETE FROM bookmark WHERE dbid = '%s'" % dbid) dbcur.execute("INSERT INTO bookmark Values (?, ?)", (dbid, item)) dbcon.commit() except: pass
def addView(content): try: skin = control.skin record = (skin, content, str(control.getCurrentViewId())) control.makeFile(control.dataPath) dbcon = database.connect(control.viewsFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS views" " (" "skin TEXT, " "view_type TEXT, " "view_id TEXT, " "UNIQUE(skin, view_type)" ");") dbcur.execute("DELETE FROM views WHERE skin = ? AND view_type = ?", ( record[0], record[1], )) dbcur.execute("INSERT INTO views Values (?, ?, ?)", record) dbcon.commit() viewName = control.infoLabel('Container.Viewmode') skinName = control.addon(skin).getAddonInfo('name') skinIcon = control.addon(skin).getAddonInfo('icon') control.infoDialog(viewName, heading=skinName, icon=skinIcon) except: return
def addView(content): try: skin = control.skin skinPath = control.skinPath xml = os.path.join(skinPath,'addon.xml') file = control.openFile(xml) read = file.read().replace('\n','') file.close() try: src = re.compile('defaultresolution="(.+?)"').findall(read)[0] except: src = re.compile('<res.+?folder="(.+?)"').findall(read)[0] src = os.path.join(skinPath, src) src = os.path.join(src, 'MyVideoNav.xml') file = control.openFile(src) read = file.read().replace('\n','') file.close() views = re.compile('<views>(.+?)</views>').findall(read)[0] views = [int(x) for x in views.split(',')] for view in views: label = control.infoLabel('Control.GetLabel(%s)' % (view)) if not (label == '' or label == None): break record = (skin, content, str(view)) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS views (""skin TEXT, ""view_type TEXT, ""view_id TEXT, ""UNIQUE(skin, view_type)"");") dbcur.execute("DELETE FROM views WHERE skin = '%s' AND view_type = '%s'" % (record[0], record[1])) dbcur.execute("INSERT INTO views Values (?, ?, ?)", record) dbcon.commit() viewName = control.infoLabel('Container.Viewmode') control.infoDialog('This View is now Default', heading=viewName) except: return
def delete(url): try: data = json.loads(url) dbid = hashlib.md5() for i in data['delbookmark']: dbid.update(str(i)) for i in data['action']: dbid.update(str(i)) dbid = str(dbid.hexdigest()) control.makeFile(control.dataPath) dbcon = database.connect(control.bookmarksFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS bookmark (" "dbid TEXT, " "item TEXT, " "UNIQUE(dbid)" ");") dbcur.execute("DELETE FROM bookmark WHERE dbid = '%s'" % dbid) dbcon.commit() control.refresh() except: pass
def timeout(definition, *args, **table): try: response = None f = repr(definition) f = re.sub('.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f) a = hashlib.md5() for i in args: a.update(str(i)) a = str(a.hexdigest()) except: pass try: table = table['table'] except: table = 'rel_list' try: control.makeFile(control.dataPath) dbcon = database.connect(control.cacheFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) match = dbcur.fetchone() return int(match[3]) except: return
def get(definition, time_out, *args, **table): try: response = None f = repr(definition) f = re.sub('.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f) a = hashlib.md5() for i in args: a.update(str(i)) a = str(a.hexdigest()) except: pass try: table = table['table'] except: table = 'rel_list' try: control.makeFile(control.dataPath) dbcon = database.connect(control.cacheFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) match = dbcur.fetchone() response = eval(match[2].encode('utf-8')) t1 = int(match[3]) t2 = int(time.time()) update = (abs(t2 - t1) / 3600) >= int(time_out) if not update: return response except: pass try: r = definition(*args) if (r is None or r == []) and response is not None: return response elif r is None or r == []: return r except: return try: r = repr(r) t = int(time.time()) dbcur.execute("CREATE TABLE IF NOT EXISTS %s (""func TEXT, ""args TEXT, ""response TEXT, ""added TEXT, ""UNIQUE(func, args)"");" % table) dbcur.execute("DELETE FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) dbcur.execute("INSERT INTO %s Values (?, ?, ?, ?)" % table, (f, a, r, t)) dbcon.commit() except: pass try: return eval(r.encode('utf-8')) except: pass
def get(function, timeout, *args, **table): try: response = None f = repr(function) f = re.sub('.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f) a = hashlib.md5() for i in args: a.update(str(i)) a = str(a.hexdigest()) except: pass try: table = table['table'] except: table = 'rel_list' try: control.makeFile(control.dataPath) dbcon = database.connect(control.cacheFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) match = dbcur.fetchone() response = eval(match[2].encode('utf-8')) t1 = int(match[3]) t2 = int(time.time()) update = (abs(t2 - t1) / 3600) >= int(timeout) if update == False: return response except: pass try: r = function(*args) if (r == None or r == []) and not response == None: return response elif (r == None or r == []): return r except: return try: r = repr(r) t = int(time.time()) dbcur.execute("CREATE TABLE IF NOT EXISTS %s (""func TEXT, ""args TEXT, ""response TEXT, ""added TEXT, ""UNIQUE(func, args)"");" % table) dbcur.execute("DELETE FROM %s WHERE func = '%s' AND args = '%s'" % (table, f, a)) dbcur.execute("INSERT INTO %s Values (?, ?, ?, ?)" % table, (f, a, r, t)) dbcon.commit() except: pass try: return eval(r.encode('utf-8')) except: pass
def get(): try: control.makeFile(control.dataPath) dbcon = database.connect(control.bookmarksFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM bookmark") items = dbcur.fetchall() items = [eval(i[1].encode('utf-8')) for i in items] return items except: pass
def insert(data): try: control.makeFile(control.dataPath) cacheFile = os.path.join(control.dataPath, 'regex.db') dbcon = database.connect(cacheFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS regex (""regex TEXT, ""response TEXT, ""UNIQUE(regex)"");") for i in data: try: dbcur.execute("INSERT INTO regex Values (?, ?)", (i['regex'], i['response'])) except: pass dbcon.commit() except: return
def deleteBookmark(name, imdb='0'): try: idFile = hashlib.md5() for i in name: idFile.update(str(i)) for i in imdb: idFile.update(str(i)) idFile = str(idFile.hexdigest()) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS bookmark (""idFile TEXT, ""timeInSeconds TEXT, ""UNIQUE(idFile)"");") dbcur.execute("DELETE FROM bookmark WHERE idFile = '%s'" % idFile) dbcon.commit() except: pass
def deleteBookmark(name, imdb='0'): try: idFile = hashlib.md5() for i in name: idFile.update(str(i)) for i in imdb: idFile.update(str(i)) idFile = str(idFile.hexdigest()) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() _create_db_(dbcur) dbcur.execute("DELETE FROM bookmark WHERE idFile = '%s'" % idFile) dbcon.commit() except: pass
def getBookmark(name, imdb='0'): try: offset = '0' idFile = hashlib.md5() for i in name: idFile.update(str(i)) for i in imdb: idFile.update(str(i)) idFile = str(idFile.hexdigest()) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM bookmark WHERE idFile = '%s'" % idFile) match = dbcur.fetchone() offset = str(match[1]) dbcon.commit() return offset except: return '0'
def addBookmark(currentTime, name, imdb='0'): try: idFile = hashlib.md5() for i in name: idFile.update(str(i)) for i in imdb: idFile.update(str(i)) idFile = str(idFile.hexdigest()) timeInSeconds = str(currentTime) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() _create_db_(dbcur) dbcur.execute("DELETE FROM bookmark WHERE idFile = '%s'" % idFile) dbcur.execute("INSERT INTO bookmark Values (?, ?)", (idFile, timeInSeconds)) dbcon.commit() except: pass
def create_folder(folder): try: folder = xbmc.makeLegalFilename(folder) control.makeFile(folder) try: if not 'ftp://' in folder: raise Exception() from ftplib import FTP ftparg = re.compile('ftp://(.+?):(.+?)@(.+?):?(\\d+)?/(.+/?)').findall(folder) ftp = FTP(ftparg[0][2], ftparg[0][0], ftparg[0][1]) try: ftp.cwd(ftparg[0][4]) except: ftp.mkd(ftparg[0][4]) ftp.quit() except: pass except: pass
def setView(content, viewDict=None): for i in range(0, 200): if control.condVisibility('Container.Content(%s)' % content): try: skin = control.skin record = (skin, content) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute("SELECT * FROM views WHERE skin = '%s' AND view_type = '%s'" % (record[0], record[1])) view = dbcur.fetchone() view = view[2] if view == None: raise Exception() return control.execute('Container.SetViewMode(%s)' % str(view)) except: try: return control.execute('Container.SetViewMode(%s)' % str(viewDict[skin])) except: return control.sleep(100)
def addView(content): try: skin = control.skin skinPath = control.skinPath xml = os.path.join(skinPath, 'addon.xml') file = control.openFile(xml) read = file.read().replace('\n', '') file.close() try: src = re.compile('defaultresolution="(.+?)"').findall(read)[0] except: src = re.compile('<res.+?folder="(.+?)"').findall(read)[0] src = os.path.join(skinPath, src) src = os.path.join(src, 'MyVideoNav.xml') file = control.openFile(src) read = file.read().replace('\n', '') file.close() views = re.compile('<views>(.+?)</views>').findall(read)[0] views = [int(x) for x in views.split(',')] for view in views: label = control.infoLabel('Control.GetLabel(%s)' % (view)) if not (label == '' or label == None): break record = (skin, content, str(view)) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute("CREATE TABLE IF NOT EXISTS views (" "skin TEXT, " "view_type TEXT, " "view_id TEXT, " "UNIQUE(skin, view_type)" ");") dbcur.execute( "DELETE FROM views WHERE skin = '%s' AND view_type = '%s'" % (record[0], record[1])) dbcur.execute("INSERT INTO views Values (?, ?, ?)", record) dbcon.commit() viewName = control.infoLabel('Container.Viewmode') control.infoDialog('This View is now Default', heading=viewName) except: return
def clear(table=None): try: if table == None: table = ['rel_list', 'rel_lib'] elif not type(table) == list: table = [table] yes = control.yesnoDialog('Are you sure?', '', '') if not yes: return control.makeFile(control.dataPath) dbcon = database.connect(control.cacheFile) dbcur = dbcon.cursor() for t in table: try: dbcur.execute("DROP TABLE IF EXISTS %s" % t) dbcur.execute("VACUUM") dbcon.commit() except: pass control.infoDialog('Process Complete') except: pass
def setView(content, viewDict=None): for i in range(0, 200): if control.condVisibility('Container.Content(%s)' % content): try: skin = control.skin record = (skin, content) control.makeFile(control.dataPath) dbcon = database.connect(control.databaseFile) dbcur = dbcon.cursor() dbcur.execute( "SELECT * FROM views WHERE skin = '%s' AND view_type = '%s'" % (record[0], record[1])) view = dbcur.fetchone() view = view[2] if view == None: raise Exception() return control.execute('Container.SetViewMode(%s)' % str(view)) except: try: return control.execute('Container.SetViewMode(%s)' % str(viewDict[skin])) except: return control.sleep(100)
def download(name, image, url, dest=addon.getSetting('download_path')): try: xbmc.executebuiltin("ActivateWindow(busydialog)") import control if url == None: return control.infoDialog(control.lang(30501).encode('utf-8')) headers = ("Authorization", "Bearer " + str(xbmcaddon.Addon().getSetting('rd_access'))) url = url.split('|')[0] content = re.compile('(.+?)\sS(\d*)E\d*$').findall(name) transname = name.translate('\/:*?"<>|').strip('.') levels = ['../../../..', '../../..', '../..', '..'] dest = control.transPath(dest) for level in levels: try: control.makeFile(os.path.abspath(os.path.join(dest, level))) except: pass control.makeFile(dest) dest = os.path.join(dest, os.path.splitext(transname)[0]) control.makeFile(dest) dest = os.path.join(dest, transname) sysheaders = urllib.quote_plus(json.dumps(headers)) sysurl = urllib.quote_plus(url) systitle = urllib.quote_plus(name) sysimage = urllib.quote_plus(image) sysdest = urllib.quote_plus(dest) script = inspect.getfile(inspect.currentframe()) cmd = 'RunScript(%s, %s, %s, %s, %s, %s)' % ( script, sysurl, sysdest, systitle, sysimage, sysheaders) xbmc.executebuiltin(cmd) except: xbmc.executebuiltin("Dialog.Close(busydialog)") util.notify(ADDON_ID, "Error Downloading")
def get(function, timeout, *args): try: response = None f = repr(function) f = re.sub('.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f) a = hashlib.md5() for i in args: a.update(str(i)) a = str(a.hexdigest()) except: pass try: control.makeFile(control.dataPath) dbcon = database.connect(control.cachemetaFile) dbcur = dbcon.cursor() dbcur.execute( "SELECT * FROM rel_list WHERE func = '%s' AND args = '%s'" % (f, a)) match = dbcur.fetchone() response = eval(match[2].encode('utf-8')) t1 = int(match[3]) t2 = int(time.time()) update = (abs(t2 - t1) / 3600) >= int(timeout) if update == False: return response except: pass try: r = function(*args) if (r == None or r == []) and not response == None: return response elif (r == None or r == []): return r except: return try: insert = True if r['cover_url'] == '' or r['backdrop_url'] == '': insert = False r = repr(r) t = int(time.time()) dbcur.execute("CREATE TABLE IF NOT EXISTS rel_list (" "func TEXT, " "args TEXT, " "response TEXT, " "added TEXT, " "UNIQUE(func, args)" ");") dbcur.execute( "DELETE FROM rel_list WHERE func = '%s' AND args = '%s'" % (f, a)) if insert == True: dbcur.execute("INSERT INTO rel_list Values (?, ?, ?, ?)", (f, a, r, t)) dbcon.commit() except: pass try: return eval(r.encode('utf-8')) except: pass
def download(name, image, url, image2): import control if url == None: return control.infoDialog(control.lang(30501).encode('utf-8')) try: headers = dict(urlparse.parse_qsl(url.rsplit('|', 1)[1])) except: headers = dict('') url = url.split('|')[0] content = re.compile('(.+?)\sS(\d*)E\d*$').findall(name) p = re.compile("(\[[A-Za-z0-9-_\ \.]+\])") transname = p.match(name.encode("utf-8")).group(1).replace("[", "").replace( "]", "") levels = ['../../../..', '../../..', '../..', '..'] dest = addon.getSetting('download_path') dest = control.transPath(dest) for level in levels: try: control.makeFile(os.path.abspath(os.path.join(dest, level))) except: pass control.makeFile(dest) dest = os.path.join(dest, transname) control.makeFile(dest) """ if "/" in dest: fo = codecs.open(dest+"/"+transname+".nfo", "a", "utf-8") else: fo = codecs.open(dest+"\"+transname+".nfo", "a", "utf-8") fo.write(u'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n<movie>\n<title>'+name+'</title>\n<genre>JAV</genre>\n</movie>'); # Close opend file fo.close()""" f = xbmcvfs.File(dest + "/" + transname + ".nfo", 'w') f.write( '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n<movie>\n<title>' + name.encode('utf-8') + '</title>\n<genre>JAV</genre>\n</movie>'.encode('utf-8')) f.close() #f = open(dest+"/fanart.jpg",'wb') f = xbmcvfs.File(dest + "/fanart.jpg", 'w') f.write(getIMAGE(image2).read()) f.close() f = xbmcvfs.File(dest + "/poster.jpg", 'w') #f = open(dest+"/poster.jpg",'wb') f.write(getIMAGE(image).read()) f.close() ext = os.path.splitext(urlparse.urlparse(url).path)[1][1:] if not ext in ['mp4', 'mkv', 'flv', 'avi', 'mpg']: ext = 'mp4' dest = os.path.join(dest, transname.decode('utf-8') + '.' + ext) sysheaders = urllib.quote_plus(json.dumps(headers)) sysurl = urllib.quote_plus(url) systitle = urllib.quote_plus(name.encode('utf-8')) sysimage = urllib.quote_plus(image) sysdest = urllib.quote_plus(dest) script = inspect.getfile(inspect.currentframe()) cmd = 'RunScript(%s, %s, %s, %s, %s, %s)' % ( script, sysurl, sysdest, systitle, sysimage, sysheaders) xbmc.executebuiltin(cmd)
def download(name, image, url, image2): import control if url == None: return control.infoDialog(control.lang(30501).encode('utf-8')) try: headers = dict(urlparse.parse_qsl(url.rsplit('|', 1)[1])) except: headers = dict('') url = url.split('|')[0] content = re.compile('(.+?)\sS(\d*)E\d*$').findall(name) p=re.compile("(\[[A-Za-z0-9-_\ \.]+\])") transname = p.match(name.encode("utf-8")).group(1).replace("[", "").replace("]", "") levels =['../../../..', '../../..', '../..', '..'] dest = addon.getSetting('download_path') dest = control.transPath(dest) for level in levels: try: control.makeFile(os.path.abspath(os.path.join(dest, level))) except: pass control.makeFile(dest) dest = os.path.join(dest, transname) control.makeFile(dest) """ if "/" in dest: fo = codecs.open(dest+"/"+transname+".nfo", "a", "utf-8") else: fo = codecs.open(dest+"\"+transname+".nfo", "a", "utf-8") fo.write(u'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n<movie>\n<title>'+name+'</title>\n<genre>JAV</genre>\n</movie>'); # Close opend file fo.close()""" f =xbmcvfs.File (dest+"/"+transname+".nfo", 'w') f.write('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n<movie>\n<title>'+name.encode('utf-8')+'</title>\n<genre>JAV</genre>\n</movie>'.encode('utf-8')) f.close() #f = open(dest+"/fanart.jpg",'wb') f =xbmcvfs.File (dest+"/fanart.jpg", 'w') f.write(getIMAGE(image2).read()) f.close() f =xbmcvfs.File (dest+"/poster.jpg", 'w') #f = open(dest+"/poster.jpg",'wb') f.write(getIMAGE(image).read()) f.close() ext = os.path.splitext(urlparse.urlparse(url).path)[1][1:] if not ext in ['mp4', 'mkv', 'flv', 'avi', 'mpg']: ext = 'mp4' dest = os.path.join(dest, transname.decode('utf-8') + '.' + ext) sysheaders = urllib.quote_plus(json.dumps(headers)) sysurl = urllib.quote_plus(url) systitle = urllib.quote_plus(name.encode('utf-8')) sysimage = urllib.quote_plus(image) sysdest = urllib.quote_plus(dest) script = inspect.getfile(inspect.currentframe()) cmd = 'RunScript(%s, %s, %s, %s, %s, %s)' % (script, sysurl, sysdest, systitle, sysimage, sysheaders) xbmc.executebuiltin(cmd)
def run(self): def download(): return [] result = cache.get(download, 600000000, table='rel_dl') for item in result: self.name = item['name'] self.image = item['image'] self.url = item['url'] sysname = self.name.translate(None, '\/:*?"<>|').strip('.') url = self.url.split('|')[0] try: headers = dict(urlparse.parse_qsl(self.url.rsplit('|', 1)[1])) except: headers = dict('') ext = os.path.splitext(urlparse.urlparse(url).path)[1][1:].lower() if not ext in ['mp4', 'mkv', 'flv', 'avi', 'mpg']: ext = 'mp4' hdlr = re.compile('.+? ([(]\d{4}[)]|S\d*E\d*)$').findall(self.name) if len(hdlr) == 0: self.content = 'Uncategorised' hdlr = re.compile('.+? (S\d*E\d*)$').findall(self.name) if len(hdlr) > 0: self.content = 'TVShows' hdlr = re.compile('.+? [(](\d{4})[)]$').findall(self.name) if len(hdlr) > 0: self.content = 'Movies' if self.content == 'Movies': dest = os.path.join(downloadPath, 'Movies') control.makeFile(dest) dest = os.path.join(dest, sysname) control.makeFile(dest) elif self.content == 'TVShows': d = re.compile('(.+?) S(\d*)E(\d*)$').findall(sysname)[0] dest = os.path.join(downloadPath, 'TVShows') control.makeFile(dest) dest = os.path.join(dest, d[0]) control.makeFile(dest) dest = os.path.join(dest, 'Season %01d' % int(d[1])) control.makeFile(dest) else: dest = os.path.join(downloadPath, 'Uncategorised') control.makeFile(dest) dest = os.path.join(dest, sysname + '.' + ext) control.infoDialog(self.name + ' Is Downloading', 'Downloads Started', self.image, time=7000) try: req = urllib2.Request(url, headers=headers) resp = urllib2.urlopen(req, timeout=30) except Exception, e: removeDownload(self.url) print '%s ERROR - File Failed To Open' % (dest) continue try: self.size = int(resp.headers['Content-Length']) except: self.size = 0 if self.size < 1: removeDownload(self.url) print '%s Unknown filesize - Unable to download' % (dest) continue try: resumable = 'bytes' in resp.headers['Accept-Ranges'].lower() except: resumable = False size = 1024 * 1024 if self.size < size: size = self.size gb = '%.2f GB' % (float(self.size) / 1073741824) start = time.clock() total = 0 notify = 0 errors = 0 count = 0 resume = 0 sleep = 0 self.clear() control.window.setProperty(property + '.status', 'downloading') control.window.setProperty(property + '.name', str(self.name)) control.window.setProperty(property + '.image', str(self.image)) control.window.setProperty(property + '.size', str(gb)) f = control.openFile(dest, 'wb') chunk = None chunks = [] while True: downloaded = total for c in chunks: downloaded += len(c) percent = min(100 * downloaded / self.size, 100) self.speed = str( int((downloaded / 1024) / (time.clock() - start))) + ' KB/s' self.percent = str(percent) + '%' control.window.setProperty(property + '.percent', str(self.percent)) control.window.setProperty(property + '.speed', str(self.speed)) if percent >= notify: control.infoDialog('Downloaded %s' % self.percent, self.name, self.image, time=5000) notify += 10 chunk = None error = False try: chunk = resp.read(size) if not chunk: if self.percent < 99: error = True else: while len(chunks) > 0: c = chunks.pop(0) f.write(c) del c f.close() print '%s download complete' % (dest) break except Exception, e: print str(e) error = True sleep = 10 errno = 0 if hasattr(e, 'errno'): errno = e.errno if errno == 10035: # 'A non-blocking socket operation could not be completed immediately' pass if errno == 10054: #'An existing connection was forcibly closed by the remote host' errors = 10 #force resume sleep = 30 if errno == 11001: # 'getaddrinfo failed' errors = 10 #force resume sleep = 30 if chunk: errors = 0 chunks.append(chunk) if len(chunks) > 5: c = chunks.pop(0) f.write(c) total += len(c) del c if error: errors += 1 count += 1 print '%d Error(s) whilst downloading %s' % (count, dest) control.sleep(sleep * 1000) if (resumable and errors > 0) or errors >= 10: if (not resumable and resume >= 50) or resume >= 500: #Give up! print '%s download canceled - too many error whilst downloading' % ( dest) break resume += 1 errors = 0 if resumable: chunks = [] #create new response print 'Download resumed (%d) %s' % (resume, dest) h = headers h['Range'] = 'bytes=%d-' % int(total) try: resp = urllib2.urlopen(urllib2.Request(url, headers=h), timeout=10) except: resp = None else: #use existing response pass if control.window.getProperty(property + '.status') == 'stop': control.infoDialog('Process Complete', 'Downloads', time=5000) return self.clear()
def _get_connection(): control.makeFile(control.dataPath) conn = db.connect(control.cacheFile) conn.row_factory = _dict_factory return conn
def run(self): def download(): return [] result = cache.get(download, 600000000, table='rel_dl') for item in result: self.name = item['name'] ; self.image = item['image'] ; self.url = item['url'] sysname = self.name.translate(None, '\/:*?"<>|').strip('.') url = self.url.split('|')[0] try: headers = dict(urlparse.parse_qsl(self.url.rsplit('|', 1)[1])) except: headers = dict('') ext = os.path.splitext(urlparse.urlparse(url).path)[1][1:].lower() if not ext in ['mp4', 'mkv', 'flv', 'avi', 'mpg']: ext = 'mp4' hdlr = re.compile('.+? ([(]\d{4}[)]|S\d*E\d*)$').findall(self.name) if len(hdlr) == 0: self.content = 'Uncategorised' hdlr = re.compile('.+? (S\d*E\d*)$').findall(self.name) if len(hdlr) > 0: self.content = 'TVShows' hdlr = re.compile('.+? [(](\d{4})[)]$').findall(self.name) if len(hdlr) > 0: self.content = 'Movies' if self.content == 'Movies': dest = os.path.join(downloadPath, 'Movies') control.makeFile(dest) dest = os.path.join(dest, sysname) control.makeFile(dest) elif self.content == 'TVShows': d = re.compile('(.+?) S(\d*)E(\d*)$').findall(sysname)[0] dest = os.path.join(downloadPath, 'TVShows') control.makeFile(dest) dest = os.path.join(dest, d[0]) control.makeFile(dest) dest = os.path.join(dest, 'Season %01d' % int(d[1])) control.makeFile(dest) else: dest = os.path.join(downloadPath, 'Uncategorised') control.makeFile(dest) dest = os.path.join(dest, sysname + '.' + ext) control.infoDialog(self.name + ' Is Downloading', 'Downloads Started', self.image, time=7000) try: req = urllib2.Request(url, headers=headers) resp = urllib2.urlopen(req, timeout=30) except Exception,e: removeDownload(self.url) print '%s ERROR - File Failed To Open' % (dest) continue try: self.size = int(resp.headers['Content-Length']) except: self.size = 0 if self.size < 1: removeDownload(self.url) print '%s Unknown filesize - Unable to download' % (dest) continue try: resumable = 'bytes' in resp.headers['Accept-Ranges'].lower() except: resumable = False size = 1024 * 1024 if self.size < size: size = self.size gb = '%.2f GB' % (float(self.size) / 1073741824) start = time.clock() total = 0 ; notify = 0 ; errors = 0 ; count = 0 ; resume = 0 ; sleep = 0 self.clear() control.window.setProperty(property + '.status', 'downloading') control.window.setProperty(property + '.name', str(self.name)) control.window.setProperty(property + '.image', str(self.image)) control.window.setProperty(property + '.size', str(gb)) f = control.openFile(dest, 'wb') chunk = None chunks = [] while True: downloaded = total for c in chunks: downloaded += len(c) percent = min(100 * downloaded / self.size, 100) self.speed = str(int((downloaded / 1024) / (time.clock() - start))) + ' KB/s' self.percent = str(percent) + '%' control.window.setProperty(property + '.percent', str(self.percent)) control.window.setProperty(property + '.speed', str(self.speed)) if percent >= notify: control.infoDialog('Downloaded %s' % self.percent, self.name, self.image, time=5000) notify += 10 chunk = None error = False try: chunk = resp.read(size) if not chunk: if self.percent < 99: error = True else: while len(chunks) > 0: c = chunks.pop(0) f.write(c) del c f.close() print '%s download complete' % (dest) break except Exception, e: print str(e) error = True sleep = 10 errno = 0 if hasattr(e, 'errno'): errno = e.errno if errno == 10035: # 'A non-blocking socket operation could not be completed immediately' pass if errno == 10054: #'An existing connection was forcibly closed by the remote host' errors = 10 #force resume sleep = 30 if errno == 11001: # 'getaddrinfo failed' errors = 10 #force resume sleep = 30 if chunk: errors = 0 chunks.append(chunk) if len(chunks) > 5: c = chunks.pop(0) f.write(c) total += len(c) del c if error: errors += 1 count += 1 print '%d Error(s) whilst downloading %s' % (count, dest) control.sleep(sleep*1000) if (resumable and errors > 0) or errors >= 10: if (not resumable and resume >= 50) or resume >= 500: #Give up! print '%s download canceled - too many error whilst downloading' % (dest) break resume += 1 errors = 0 if resumable: chunks = [] #create new response print 'Download resumed (%d) %s' % (resume, dest) h = headers ; h['Range'] = 'bytes=%d-' % int(total) try: resp = urllib2.urlopen(urllib2.Request(url, headers=h), timeout=10) except: resp = None else: #use existing response pass if control.window.getProperty(property + '.status') == 'stop': control.infoDialog('Process Complete', 'Downloads', time=5000) return self.clear()