def run(self): global globalThreadReport try: while not PuxGlobal.joinPlease[0]: globalThreadReport['Torrent'] = BFun.tic() self.checkTorrentList() if self.counters['add'] == 0: print('Updating show list') self.addShows() if self.counters['check'] == 0: for show in self.showList: print('Checking torrents of ' + show.SHOW_NAME) try: show.checkTorrents() except Exception as ex: BFun.ezLog( 'Error when calling show.checkTorrents() in PuxThreads.TorrentT.run()', ex) print( 'Error when calling show.checkTorrents() in PuxThreads.TorrentT.run()' ) self.counters['add'] = self.counterFreq['add'] - 1 self.advanceCounters() except Exception as ex: BFun.ezLog('Torrent thread broke', ex) # If it gets to here, it means we're trying to join. for show in self.showList: # So make sure all data is saved. show.saveData()
def run(self): global globalThreadReport try: while not PuxGlobal.joinPlease[0]: globalThreadReport['Torrent'] = BFun.tic() self.checkTorrentList() if self.counters['add'] == 0: print('Updating show list') self.addShows() if self.counters['check'] == 0: for show in self.showList: print('Checking torrents of '+show.SHOW_NAME) try: show.checkTorrents() except Exception as ex: BFun.ezLog('Error when calling show.checkTorrents() in PuxThreads.TorrentT.run()',ex) print('Error when calling show.checkTorrents() in PuxThreads.TorrentT.run()') self.counters['add'] = self.counterFreq['add'] - 1 self.advanceCounters() except Exception as ex: BFun.ezLog('Torrent thread broke',ex) # If it gets to here, it means we're trying to join. for show in self.showList: # So make sure all data is saved. show.saveData()
def run(self): global globalThreadReport while not PuxGlobal.joinPlease[0]: globalThreadReport['PushBullet'] = BFun.tic() print('Getting new pushes...') a = BFun.tic() commandList = self.pBullet.commandFinder() if len(commandList) > 0: print('Found %d commands' % len(commandList)) for aDict in commandList: sender = aDict['sender'] arguments = aDict['arguments'] try: command = arguments.pop(0) except IndexError as ex: print('Argument list was empty') command = '' commandLower = command.lower() if commandLower == 'speak': title = None body = 'Woof!' if len(arguments) > 0: if (arguments[0].lower() == 'jpn') or ('japan' in arguments[0].lower()): body = 'Wan!' print('Sending message') self.pBullet.sendNote(title, body, sender) elif 'torrent' in commandLower: self.torrentCommand(arguments, sender) elif 'join' in commandLower: self.joinCommand(arguments, sender) elif 'upgrade' in commandLower: self.upgradeCommand(arguments, sender) elif 'run' in commandLower: self.runtimeCommand(sender) else: wholeCommand = command + ' ' + ' '.join(arguments) self.invalidCommand(wholeCommand, sender) # End of commands print('Took %0.1f seconds to check and process pushes' % BFun.toc(a)) print('Sleeping for %0.0f seconds. (%d rates left)' % (self.pBullet.sleepTime, self.pBullet.previousRates)) time.sleep( self.pBullet.sleepTime) # Wait to avoid getting ratelimited
def checkTorrents(self): # global PuxGlobal.globalRecentCompleteTorrents # List of dictionaries # global PuxGlobal.globalCurrentTorrentsProgress # Dictionary of list of dictionaries # Reduce self.requestSleep. if self.requestSleep > 1: self.requestSleep = self.requestSleep - 1 progressList = [] # [{'percent}] for torrentDict in self.torrentList: if torrentDict["completed"] < 1: # refresh the information about the torrent. fileDicts = self.transmissionSession.get_torrent(torrentDict["hashString"]).files() # self.DOWNLOAD_PATH sizeList = [] completeList = [] for n in torrentDict["wantedFileIDs"]: sizeList.append(int(fileDicts[n]["size"])) completeList.append(int(fileDicts[n]["completed"])) totalSize = sum(sizeList) totalComplete = sum(completeList) if totalComplete == totalSize: # Append to globalRecentCompleteTorrents completeDictionary = {} completeDictionary["show"] = self.SHOW_NAME completeDictionary["epNum"] = torrentDict["epNum"] completeDictionary["tic"] = BFun.tic() PuxGlobal.globalRecentCompleteTorrents.append(completeDictionary) # Move files to fileServer # Make sure directory exists try: # Try to get file list from show folder on file server. temp = os.listdir(self.SHOW_PATH) except FileNotFoundError as ex: # Make the directory os.mkdir(self.SHOW_PATH) for n in torrentDict["wantedFileIDs"]: fileName = fileDicts[n]["name"] startPath = self.DOWNLOAD_PATH + "/" + fileName endPath = self.SHOW_PATH print("Copying %s Ep %d" % (completeDictionary["show"], completeDictionary["epNum"])) shutil.copy(startPath, endPath) torrentDict["completed"] = -1 BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH) print("Finished copying") time.sleep(0.1) # Sleep for no reason? # Remove torrent and files from transmission self.transmissionSession.remove_torrent(torrentDict["hashString"], delete_data=True) torrentDict["completed"] = 1 BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH) tempBody = self.SHOW_NAME + " Ep: %d complete" % torrentDict["epNum"] self.pBullet.sendBrowsers(body=tempBody) else: # Add progress to progressDictionary progressDictionary = {} progressDictionary["epNum"] = torrentDict["epNum"] progressDictionary["percent"] = 100.0 * totalComplete / totalSize progressList.append(progressDictionary) PuxGlobal.globalCurrentTorrentsProgress[self.SHOW_NAME] = progressList
def run(self): global globalThreadReport while not PuxGlobal.joinPlease[0]: globalThreadReport['PushBullet'] = BFun.tic() print('Getting new pushes...') a = BFun.tic() commandList = self.pBullet.commandFinder() if len(commandList) > 0: print('Found %d commands'%len(commandList)) for aDict in commandList: sender = aDict['sender'] arguments = aDict['arguments'] try: command = arguments.pop(0) except IndexError as ex: print('Argument list was empty') command = '' commandLower = command.lower() if commandLower == 'speak': title = None body = 'Woof!' if len(arguments) > 0: if (arguments[0].lower() == 'jpn') or ('japan' in arguments[0].lower()): body = 'Wan!' print('Sending message') self.pBullet.sendNote(title,body,sender) elif 'torrent' in commandLower: self.torrentCommand(arguments,sender) elif 'join' in commandLower: self.joinCommand(arguments,sender) elif 'upgrade' in commandLower: self.upgradeCommand(arguments,sender) elif 'run' in commandLower: self.runtimeCommand(sender) else: wholeCommand = command+' '+' '.join(arguments) self.invalidCommand(wholeCommand,sender) # End of commands print('Took %0.1f seconds to check and process pushes'%BFun.toc(a)) print('Sleeping for %0.0f seconds. (%d rates left)'%(self.pBullet.sleepTime,self.pBullet.previousRates)) time.sleep(self.pBullet.sleepTime) # Wait to avoid getting ratelimited
def puxRequest(self, method, url, postdata=None, params=None, files=None): # Makes a request then calculates sleep time to avoid getting ratelimited. headers = { "Accept": "application/json", "Content-Type": "application/json", "User-Agent": "asdf" } if postdata: postdata = json.dumps(postdata) # Make request and calculate time. status_codes = [0] while status_codes[-1] != 200: timer1 = BFun.tic() try: r = requests.request(method, url, data=postdata, params=params, headers=headers, files=files, auth=HTTPBasicAuth(self.ACCESS_TOKEN, ""), timeout=30) status_codes.append(r.status_code) except requests.exceptions.ConnectTimeout as ex: print('PushBullet request timedout.') status_codes.append(1) except Exception as ex: BFun.ezLog('Error when making pushbullet request:', ex) if status_codes[-1] == 403: self.getAccessToken() if len(status_codes) > 100: print('Failed requests 100 times') for d in status_codes: print('Status Code %d' % d) break if not self.sendOnly: timeForRequest = BFun.toc(timer1) # Update sleep time. timeUntilReset = float(r.headers.get('X-Ratelimit-Reset', 1)) - time.time() remainingRates = int(r.headers.get('X-Ratelimit-Remaining', 0)) self.ratesPerRequest.append(self.previousRates - remainingRates) self.previousRates = remainingRates remainingRequests = math.floor(1. * remainingRates / self.getAvgRates()) timeBetweenRequests = 1. * timeUntilReset / remainingRequests # Double sleep time so we can run 2. This one and the test one. self.sleepTime = 2. * math.ceil( max([timeBetweenRequests - timeForRequest, 1])) return r
def puxRequest(self,method, url, postdata=None, params=None, files=None): # Makes a request then calculates sleep time to avoid getting ratelimited. headers = {"Accept": "application/json", "Content-Type": "application/json", "User-Agent": "asdf"} if postdata: postdata = json.dumps(postdata) # Make request and calculate time. status_codes = [0] while status_codes[-1] != 200: timer1 = BFun.tic() try: r = requests.request(method, url, data=postdata, params=params, headers=headers, files=files, auth=HTTPBasicAuth(self.ACCESS_TOKEN, ""),timeout=30) status_codes.append(r.status_code) except requests.exceptions.ConnectTimeout as ex: print('PushBullet request timedout.') status_codes.append(1) except Exception as ex: BFun.ezLog('Error when making pushbullet request:',ex) if status_codes[-1] == 403: self.getAccessToken() if len(status_codes) > 100: print('Failed requests 100 times') for d in status_codes: print('Status Code %d'%d) break if not self.sendOnly: timeForRequest = BFun.toc(timer1) # Update sleep time. timeUntilReset = float(r.headers.get('X-Ratelimit-Reset',1)) - time.time() remainingRates = int(r.headers.get('X-Ratelimit-Remaining',0)) self.ratesPerRequest.append(self.previousRates-remainingRates) self.previousRates = remainingRates remainingRequests = math.floor(1.*remainingRates/self.getAvgRates()) timeBetweenRequests = 1.*timeUntilReset/remainingRequests # Double sleep time so we can run 2. This one and the test one. self.sleepTime = 2.*math.ceil(max([timeBetweenRequests - timeForRequest,1])) return r
def __init__(self): super(PushBulletT, self).__init__() global globalPBullet self.pBullet = globalPBullet self.threadInitTic = BFun.tic()
def checkTorrents(self): #global PuxGlobal.globalRecentCompleteTorrents # List of dictionaries #global PuxGlobal.globalCurrentTorrentsProgress # Dictionary of list of dictionaries # Reduce self.requestSleep. if self.requestSleep > 1: self.requestSleep = self.requestSleep - 1 progressList = [] # [{'percent}] for torrentDict in self.torrentList: if torrentDict['completed'] < 1: # refresh the information about the torrent. fileDicts = self.transmissionSession.get_torrent( torrentDict['hashString']).files() #self.DOWNLOAD_PATH sizeList = [] completeList = [] for n in torrentDict['wantedFileIDs']: sizeList.append(int(fileDicts[n]['size'])) completeList.append(int(fileDicts[n]['completed'])) totalSize = sum(sizeList) totalComplete = sum(completeList) if totalComplete == totalSize: # Append to globalRecentCompleteTorrents completeDictionary = {} completeDictionary['show'] = self.SHOW_NAME completeDictionary['epNum'] = torrentDict['epNum'] completeDictionary['tic'] = BFun.tic() PuxGlobal.globalRecentCompleteTorrents.append( completeDictionary) # Move files to fileServer # Make sure directory exists try: # Try to get file list from show folder on file server. temp = os.listdir(self.SHOW_PATH) except FileNotFoundError as ex: # Make the directory os.mkdir(self.SHOW_PATH) for n in torrentDict['wantedFileIDs']: fileName = fileDicts[n]['name'] startPath = self.DOWNLOAD_PATH + '/' + fileName endPath = self.SHOW_PATH print('Copying %s Ep %d' % (completeDictionary['show'], completeDictionary['epNum'])) shutil.copy(startPath, endPath) torrentDict['completed'] = -1 BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH) print('Finished copying') time.sleep(0.1) # Sleep for no reason? # Remove torrent and files from transmission self.transmissionSession.remove_torrent( torrentDict['hashString'], delete_data=True) torrentDict['completed'] = 1 BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH) tempBody = self.SHOW_NAME + ' Ep: %d complete' % torrentDict[ 'epNum'] self.pBullet.sendBrowsers(body=tempBody) else: # Add progress to progressDictionary progressDictionary = {} progressDictionary['epNum'] = torrentDict['epNum'] progressDictionary[ 'percent'] = 100. * totalComplete / totalSize progressList.append(progressDictionary) PuxGlobal.globalCurrentTorrentsProgress[self.SHOW_NAME] = progressList
import PuxGlobal import BFun import PuxThreads from PuxBulletf import PuxBullet from PuxShowf import PuxShow import json import operator import time import math import threading import os programRunTic = BFun.tic() ppBullet = PuxBullet(sendOnly=True) # Just make sure the lg BFun.ezLog('Program started at %0.1f'%programRunTic) # Make the threads threadList = [] threadList.append(PuxThreads.TorrentT()) threadList.append(PuxThreads.PushBulletT()) # Start the threads for n in range(len(threadList)): print('Starting thread %d'%n) threadList[n].start() # Program is basically over now. for oneThread in threadList: # Wait until each thread joins (finishes) oneThread.join() # Perform program end things. runTimeString = BFun.toc2Str(programRunTic)
import PuxGlobal import BFun import PuxThreads from PuxBulletf import PuxBullet from PuxShowf import PuxShow import json import operator import time import math import threading import os programRunTic = BFun.tic() ppBullet = PuxBullet(sendOnly=True) # Just make sure the lg BFun.ezLog('Program started at %0.1f' % programRunTic) # Make the threads threadList = [] threadList.append(PuxThreads.TorrentT()) threadList.append(PuxThreads.PushBulletT()) # Start the threads for n in range(len(threadList)): print('Starting thread %d' % n) threadList[n].start() # Program is basically over now. for oneThread in threadList: # Wait until each thread joins (finishes) oneThread.join() # Perform program end things. runTimeString = BFun.toc2Str(programRunTic)