def __init__(self): self.session = requests.Session() self.server = 'http://www.addic7ed.com' self.session.headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', 'Referer' : 'http://www.addic7ed.com', 'Pragma': 'no-cache'} self.logged_in = False
def OpenSubtitlesLogin(opensubtitlesusername=None, opensubtitlespasswd=None): data = { 'user': autosub.OPENSUBTITLESUSER, 'password': autosub.OPENSUBTITLESPASSWD, 'a': 'login', 'redirect': '/nl', 'remember': 'on' } # Expose to test login # When fields are empty it will check the config file if opensubtitlesusername and opensubtitlespasswd: data['user'] = opensubtitlesusername data['password'] = opensubtitlespasswd log.debug('OpenSubtitlesLogin: Test login with User: %s' % data['user']) else: data['user'] = autosub.OPENSUBTITLESUSER data['password'] = autosub.OPENSUBTITLESPASSWD log.debug('OpenSubtitlesLogin: Normal Login with User %s' % data['user']) if autosub.OPENSUBTITLESLOGGED_IN: log.debug('OpenSubtitlesLogin: Already Logged in with user %s' % data['user']) return True if data['user'] and data['password']: pass else: log.debug('OpenSubtitlesLogin: Username or password empty') return False autosub.OPENSUBTTITLESSESSION = requests.Session() autosub.OPENSUBTTITLESSESSION.headers = { 'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', 'referer': autosub.OPENSUBTITLESURL } try: TimeOut() RequestResult = autosub.OPENSUBTTITLESSESSION.post( autosub.OPENSUBTITLESURL + '/login', data, timeout=10) except: log.debug('OpenSubtitlesLogin: Login post request exception.') autosub.OPENSUBTITLESLOGGED_IN = False return False try: TimeOut() RequestResult = autosub.OPENSUBTTITLESSESSION.get( autosub.OPENSUBTITLESURL + '/xml', timeout=10) except: log.debug( 'OpenSubtitlesLogin: Could not get a page from OpenSubtitles.') return False if 'text/xml' not in RequestResult.headers['Content-Type']: log.info('OpenSubtitlesLogin: Could not login on OpenSubtitles.') return False try: root = ET.fromstring(RequestResult.content) except: log.info('OpenSubtitlesLogin: Could not login on OpenSubtitles.') return False try: if root.find('.//logged_as').text == data['user']: autosub.OPENSUBTITLESLOGGED_IN = True Rank = root.find('.//top_user_rank').text log.info("OpenSubtitlesLogin: Logged in as %s with rank: %s" % (data['user'], Rank)) return True except: pass log.info('OpenSubtitlesLogin: Login of User %s failed' % data['user']) autosub.OPENSUBTITLESLOGGED_IN = False return False
def UpdateAutoSub(): ''' Update Autosub. ''' log.debug('UpdateAutoSub: Update started') # Piece of Code to let you test the reboot of autosub after an update, without actually updating anything #RestartTest = True #if RestartTest: # log.debug('UpdateAutoSub: Module is in restart Test mode') # args = [] # args = sys.argv[:] # args.insert(0, sys.executable) # args.append('-u') # time.sleep(5) # log.debug('UpdateAutoSub: Python exec arguments are %s' %(args)) # os.execv(sys.executable, args) # Get the version number from github GithubVersion = CheckVersion() if autosub.VERSION >= int(GithubVersion.split('.')[0]) * 1000 + int(GithubVersion.split('.')[1]) * 100 + int(GithubVersion.split('.')[2]) * 10: message = 'No update available. Current version: ' + autosubversion + '. GitHub version: ' + GithubVersion log.info('UpdateAutoSub: %s' % message) return message else: autosub.UPDATED = False #First we make a connection to github to get the zipfile with the release log.info('Starting upgrade.') Session = requests.Session() try: Result = Session.get(autosub.ZIPURL,verify=autosub.CERTIFICATEPATH) ZipData= Result.content except Exception as error: log.error('UpdateAutoSub: Could not connect to github. Error is %s' % error) return error log.debug('UpdateAutoSub: Zipfile located on Github') # exstract the zipfile to the autosub root directory try: zf = zipfile.ZipFile(StringIO.StringIO(Result.content)) ZipRoot = zf.namelist()[0][:-1] if ZipRoot: ReleasePath = os.path.join(autosub.PATH,ZipRoot) if os.path.isdir(ReleasePath): try: remove_tree(ReleasePath) except Exception as error: log.debug('UpdateAutoSub: Problem removing old release folder. Error is: %s' %error) return error else: return 'No correct zipfile could be downloaded' Result = zf.extractall(autosub.PATH) log.debug('UpdateAutoSub: Zipfile extracted') except Exception as error: log.error('UpdateAutoSub: Problem extracting zipfile from github. Error is %s' % error) return # copy the release try: copy_tree(ReleasePath,autosub.PATH) except Exception as error: log.error('UpdateAutoSub: Could not(fully) copy the updated tree. Error is %s' % error) return error log.debug('UpdateAutoSub: updated tree copied.') # remove the release folder after the update if os.path.isdir(ReleasePath): try: remove_tree(ReleasePath) except Exception as error: log.error('UpdateAutoSub: Problem removing old release folder. Error is: %s' % error) return error args =[] args = sys.argv[:] args.insert(0, sys.executable) args.append('-u') log.info('UpdateAutoSub: Update to version %s. Now restarting autosub...' % GithubVersion) log.debug('UpdateAutoSub: Python exec arguments are %s, %s' %(sys.executable,args)) os.execv(sys.executable, args)
def UpdateAutoSub(): ''' Update Autosub. ''' autosub.MESSAGE = '' if time.time() - autosub.STARTTIME < 15: autosub.UPDATING = False return if autosub.SEARCHBUSY: autosub.SEARCHSTOP = True log.debug('Update started') CheckVersion() if autosub.GITHUBVERSION == '?.?.?': autosub.UPDATING = False autosub.MESSAGE = "Could not get a correct version from Github" return New = autosub.GITHUBVERSION.split('.') Current = autosub.version.autosubversion.split('.') if not (int(New[0]) > int(Current[0]) or int(New[1]) > int(Current[1]) or int(New[2]) > int(Current[2])): autosub.MESSAGE = "No higer version on github available" log.info( 'No update available. Current version: %s GitHub version: %s' % (autosub.version.autosubversion, autosub.GITHUBVERSION)) autosub.UPDATING = False return #First we make a connection to github to get the zipfile with the release log.info('Starting upgrade.') with requests.Session() as Session: try: Result = Session.get(autosub.ZIPURL, verify=autosub.CERT, timeout=16) except Exception as error: autosub.MESSAGE = error.message log.error(error.message) autosub.UPDATING = False return log.debug('Zipfile downloaded from Github') # exstract the zipfile to the autosub root directory try: zf = ZipFile(StringIO(Result.content)) ZipRoot = zf.namelist()[0][:-1] if ZipRoot: ReleasePath = os.path.join(autosub.PATH, ZipRoot) if os.path.isdir(ReleasePath): try: remove_tree(ReleasePath) except Exception as error: autosub.MESSAGE = error.message log.error(error.message) autosub.UPDATING = False return else: autosub.MESSAGE = 'The zipfile was corrupted' log.error(autosub.MESSAGE) autosub.UPDATING = False return Result = zf.extractall(autosub.PATH) log.debug('Zipfile extracted') except Exception as error: autosub.MESSAGE = error.message log.error(error.message) autosub.UPDATING = False return # copy the release try: copy_tree(ReleasePath, autosub.PATH) log.debug('updated tree copied.') except Exception as error: autosub.MESSAGE = error.message log.error('Could not(fully) copy the updated tree. Error is %s' % error) autosub.UPDATING = False return # remove the release folder after the update if os.path.isdir(ReleasePath): try: remove_tree(ReleasePath) except Exception as error: autosub.MESSAGE = error.message log.error('Problem removing old release folder. Error is: %s' % error) autosub.UPDATING = False return Count = 0 # Wait untill the Search thread has finished while autosub.SEARCHBUSY: time.sleep(1) Count += 1 if Count > 35: log.error('Update problem. Had to use a forced stop on the Search') autosub.Scheduler.stop(99)