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 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 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