Ejemplo n.º 1
0
 def commandFinder(self):
     # Returns a list of lists of the form:
     # [[command 1, argument 1, argument 2, etc.], [command 2, argument 1, etc.], etc.]
     r = self.refreshPushes()
     pushes = r.json().get('pushes', [])
     commandList = []
     for push in pushes:
         pushIden = push.get('iden', None)
         if pushIden in self.seenPushIdens:
             pass  #
         else:
             #pTitle = push.get('title','No Title') # Don't check title.
             pBody = push.get('body', '')
             pBodyS = pBody.split()
             try:
                 pBodyS0 = pBodyS.pop(0)
                 if pBodyS0.lower() in self.indicatorList:
                     sender = push.get('source_device_iden', None)
                     commandDict = {}
                     commandDict['sender'] = sender
                     commandDict['arguments'] = pBodyS
                     commandList.append(commandDict)
                     if pushIden:
                         #self.dismissPush(pushIden)
                         self.deletePush(pushIden)
             except Exception as ex:
                 BFun.ezLog(
                     'Message (%s) broke command finder in PuxBulletf. Search for 2832516'
                     % pBody, ex)
     return commandList
Ejemplo n.º 2
0
def addTorrentURL(someSession,torrentDict):
	# Add torrent by URL
	someURL = torrentDict['URL']
	fileExtension = torrentDict['extension']
	someSession.add_torrent(someURL)
	# Get list of all torrent objects
	torrents = someSession.get_torrents()
	# Get the torrent we just added.
	torrent = torrents[-1]
	# Note: tSess.get_torrent(1) is the same as torrents[0]

	# Get the unique hash of the torrent so working with torrents is easier.
	uniqueHash = torrent.hashString
	fileList = someSession.get_torrent(uniqueHash).files()
	#tSess2.get_torrent(uniqueHash).files()
	filePairs = []
	for m in range(len(fileList)):
		fileName = fileList[m]['name']
		if fileExtension.lower() in fileName.lower():
			BFun.ezPrint('Found %s in %s'%(fileExtension,fileName))
			filePairs.append((m,fileName))
	watchedTorrent = {}
	watchedTorrent['hash'] = uniqueHash
	watchedTorrent['show'] = 'test'
	watchedTorrent['epNum'] = 1
	watchedTorrent['filePairs'] = filePairs # list of (fileNumber,fileName)
	watchedTorrent['URL'] = someURL
	BFun.ezPrint('Added torrent with %s'%filePairs[0][1])
	return watchedTorrent
Ejemplo n.º 3
0
def addTorrentURL(someSession, torrentDict):
    # Add torrent by URL
    someURL = torrentDict['URL']
    fileExtension = torrentDict['extension']
    someSession.add_torrent(someURL)
    # Get list of all torrent objects
    torrents = someSession.get_torrents()
    # Get the torrent we just added.
    torrent = torrents[-1]
    # Note: tSess.get_torrent(1) is the same as torrents[0]

    # Get the unique hash of the torrent so working with torrents is easier.
    uniqueHash = torrent.hashString
    fileList = someSession.get_torrent(uniqueHash).files()
    #tSess2.get_torrent(uniqueHash).files()
    filePairs = []
    for m in range(len(fileList)):
        fileName = fileList[m]['name']
        if fileExtension.lower() in fileName.lower():
            BFun.ezPrint('Found %s in %s' % (fileExtension, fileName))
            filePairs.append((m, fileName))
    watchedTorrent = {}
    watchedTorrent['hash'] = uniqueHash
    watchedTorrent['show'] = 'test'
    watchedTorrent['epNum'] = 1
    watchedTorrent['filePairs'] = filePairs  # list of (fileNumber,fileName)
    watchedTorrent['URL'] = someURL
    BFun.ezPrint('Added torrent with %s' % filePairs[0][1])
    return watchedTorrent
Ejemplo n.º 4
0
 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()
Ejemplo n.º 5
0
 def upgradeCommand(self, arguments, sender):
     upgrade = -1
     title = None
     report = ' '
     if len(arguments) == 0:
         upgrade = 0
     elif arguments[0].lower() == 'please':
         # Perform upgrade.
         fileList = os.listdir(PuxGlobal.UPGRADE_PATH)
         #currentDir = os.getcwd()
         currentDir = PuxGlobal.PROGRAM_PATH
         report = ' Copying %d files to %s' % (len(fileList), currentDir)
         for aFile in fileList:
             sourcePath = PuxGlobal.UPGRADE_PATH + '/' + aFile
             if os.path.isfile(sourcePath):
                 BFun.ezLog('Copying %s' % sourcePath)
                 shutil.copy(sourcePath, currentDir)
         upgrade = 1
     else:
         upgrade = 0
     if upgrade == 0:
         body = "Correct usage is push/pull upgrade please. It's a safety feature."
     elif upgrade == 1:
         body = 'Upgrade complete. Join requested.' + report
         PuxGlobal.joinPlease[0] = True
         print('PuxGlobal.joinPlease[0] set to True')
     else:  # Upgrade not 1 or 0 means something went wrong.
         body = 'Error when upgrading. Code %d' % upgrade
         BFun.ezLog(body)
     self.pBullet.sendNote(title, body, sender)
Ejemplo n.º 6
0
def connectTransmission():
    # Connects to transmission and starts transmission if not already running.
    # Returns a transmission session object
    try:
        return transmissionrpc.Client()
    except transmissionrpc.error.TransmissionError as ex:
        if 'connection refused' in str(ex).lower():
            print('Starting Transmission')
            # Start transmission and return control to terminal
            os.system('transmission-gtk &')
            print('Transmission Starting')
            unConnected = 1
            # Try to connect to transmission until successful.
            while unConnected > 0:
                try:
                    return transmissionrpc.Client()
                    #unConnected = 0
                except transmissionrpc.error.TransmissionError as ex:
                    print('Failed to connect to Transmission. (%d times)' %
                          unConnected)
                    unConnected = unConnected + 1
                    time.sleep(1)  # Sleep 1 second for sanity.
        else:
            BFun.ezLog(
                'Unexpected error when connecting to transmission. Search for 1435813',
                ex)
    except Exception as ex:
        BFun.ezLog(
            'Unexpected error when connecting to transmission. Search for 1435813a',
            ex)
Ejemplo n.º 7
0
def connectTransmission():
	# Connects to transmission and starts transmission if not already running.
	# Returns a transmission session object
	try:
		return transmissionrpc.Client()
	except transmissionrpc.error.TransmissionError as ex:
		if 'connection refused' in str(ex).lower():
			print('Starting Transmission')
			# Start transmission and return control to terminal
			os.system('transmission-gtk &')
			print('Transmission Starting')
			unConnected = 1
			# Try to connect to transmission until successful.
			while unConnected > 0:
				try:
					return transmissionrpc.Client()
					#unConnected = 0
				except transmissionrpc.error.TransmissionError as ex:
					print('Failed to connect to Transmission. (%d times)'%unConnected)
					unConnected = unConnected + 1
					time.sleep(1) # Sleep 1 second for sanity.
		else:
			BFun.ezLog('Unexpected error when connecting to transmission. Search for 1435813',ex)
	except Exception as ex:
		BFun.ezLog('Unexpected error when connecting to transmission. Search for 1435813a',ex)
Ejemplo n.º 8
0
	def commandFinder(self):
		# Returns a list of lists of the form:
		# [[command 1, argument 1, argument 2, etc.], [command 2, argument 1, etc.], etc.]
		r = self.refreshPushes()
		pushes = r.json().get('pushes',[])
		commandList = []
		for push in pushes:
			pushIden = push.get('iden',None)
			if pushIden in self.seenPushIdens:
				pass #
			else:
				#pTitle = push.get('title','No Title') # Don't check title.
				pBody = push.get('body','')
				pBodyS = pBody.split()
				try:
					pBodyS0 = pBodyS.pop(0)
					if pBodyS0.lower() in self.indicatorList:
						sender = push.get('source_device_iden',None)
						commandDict = {}
						commandDict['sender'] = sender
						commandDict['arguments'] = pBodyS
						commandList.append(commandDict)
						if pushIden:
							#self.dismissPush(pushIden)
							self.deletePush(pushIden)
				except Exception as ex:
					BFun.ezLog('Message (%s) broke command finder in PuxBulletf. Search for 2832516'%pBody,ex)
		return commandList
Ejemplo n.º 9
0
 def updateTorrentList(self):
     # Consolidate torrents in transmission, previously completed torrents and
     # torrents already in torrentList
     # Load torrents from data file
     fileTorrents = BFun.jLoader(self.TORRENT_LIST_PATH, expectList=True)
     if len(self.torrentList) == 0:
         self.torrentList = fileTorrents
     else:
         consolidatedTorrents = []
         hashList = []
         for sTorrent in self.torrentList:
             hashList.append(sTorrent['hashString'])
         for fTorrent in fileTorrents:
             hashList.append(fTorrent['hashString'])
         hashSet = set(hashList)
         hashList = set(hashSet)
         for hashString in hashList:
             found = False
             for sTorrent in self.torrentList:
                 if hashString == sTorrent['hashString']:
                     consolidatedTorrents.append(sTorrent)
                     found = True
                     break
             if found == False:
                 for fTorrent in fileTorrents:
                     if hashString == fTorrent['hashString']:
                         consolidatedTorrents.append(fTorrent)
                         break
         self.torrentList = consolidatedTorrents
         BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
Ejemplo n.º 10
0
	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()
Ejemplo n.º 11
0
	def upgradeCommand(self,arguments,sender):
		upgrade = -1
		title = None
		report = ' '
		if len(arguments) == 0:
			upgrade = 0
		elif arguments[0].lower() == 'please':
			# Perform upgrade.
			fileList = os.listdir(PuxGlobal.UPGRADE_PATH)
			#currentDir = os.getcwd()
			currentDir = PuxGlobal.PROGRAM_PATH
			report = ' Copying %d files to %s'%(len(fileList),currentDir)
			for aFile in fileList:
				sourcePath = PuxGlobal.UPGRADE_PATH+'/'+aFile
				if os.path.isfile(sourcePath):
					BFun.ezLog('Copying %s'%sourcePath)
					shutil.copy(sourcePath,currentDir)
			upgrade = 1
		else:
			upgrade = 0
		if upgrade == 0:
			body = "Correct usage is push/pull upgrade please. It's a safety feature."
		elif upgrade == 1:
			body = 'Upgrade complete. Join requested.'+report
			PuxGlobal.joinPlease[0] = True
			print('PuxGlobal.joinPlease[0] set to True')
		else: # Upgrade not 1 or 0 means something went wrong.
			body = 'Error when upgrading. Code %d'%upgrade
			BFun.ezLog(body)
		self.pBullet.sendNote(title,body,sender)
Ejemplo n.º 12
0
 def updateTorrentList(self):
     # Consolidate torrents in transmission, previously completed torrents and
     # torrents already in torrentList
     # Load torrents from data file
     fileTorrents = BFun.jLoader(self.TORRENT_LIST_PATH, expectList=True)
     if len(self.torrentList) == 0:
         self.torrentList = fileTorrents
     else:
         consolidatedTorrents = []
         hashList = []
         for sTorrent in self.torrentList:
             hashList.append(sTorrent["hashString"])
         for fTorrent in fileTorrents:
             hashList.append(fTorrent["hashString"])
         hashSet = set(hashList)
         hashList = set(hashSet)
         for hashString in hashList:
             found = False
             for sTorrent in self.torrentList:
                 if hashString == sTorrent["hashString"]:
                     consolidatedTorrents.append(sTorrent)
                     found = True
                     break
             if found == False:
                 for fTorrent in fileTorrents:
                     if hashString == fTorrent["hashString"]:
                         consolidatedTorrents.append(fTorrent)
                         break
         self.torrentList = consolidatedTorrents
         BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
Ejemplo n.º 13
0
	def runtimeCommand(self,sender):
		global globalThreadReport
		body = 'PuxHelper has been running for %s.'%BFun.toc2Str(self.threadInitTic)
		keyList = list(globalThreadReport.keys())
		body = body+'\n%d Threads:'%len(keyList)
		for aKey in keyList:
			reportTime = globalThreadReport[aKey]
			body = body+'\n%s ago, %s'%(BFun.toc2Str(reportTime),aKey)
		self.pBullet.sendNote(None,body,sender)
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
 def runtimeCommand(self, sender):
     global globalThreadReport
     body = 'PuxHelper has been running for %s.' % BFun.toc2Str(
         self.threadInitTic)
     keyList = list(globalThreadReport.keys())
     body = body + '\n%d Threads:' % len(keyList)
     for aKey in keyList:
         reportTime = globalThreadReport[aKey]
         body = body + '\n%s ago, %s' % (BFun.toc2Str(reportTime), aKey)
     self.pBullet.sendNote(None, body, sender)
Ejemplo n.º 16
0
	def getBrowserIdens(self):
		devices = self.getDevices()
		for device in devices:
			icon = device.get('icon','')
			if icon.lower() == 'browser':
				# use try except since we don't want messages going to None if we can't get the iden
				try:
					self.browserIdens.append(device['iden'])
				except Exception as ex:
					BFun.ezLog('Error when getting browser idens in PuxBulletf. Search for 1751686',ex)
Ejemplo n.º 17
0
 def getBrowserIdens(self):
     devices = self.getDevices()
     for device in devices:
         icon = device.get('icon', '')
         if icon.lower() == 'browser':
             # use try except since we don't want messages going to None if we can't get the iden
             try:
                 self.browserIdens.append(device['iden'])
             except Exception as ex:
                 BFun.ezLog(
                     'Error when getting browser idens in PuxBulletf. Search for 1751686',
                     ex)
Ejemplo n.º 18
0
 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
Ejemplo n.º 19
0
 def addShows(self):
     # Load torrent list
     temp = BFun.ezRead(self.TORRENT_LIST_PATH)
     temp = temp.splitlines()
     # Delete old showList (for memory?) and make a new one.
     for show in self.showList:
         # Make sure all data is saved.
         show.saveData()
     self.showList.clear()
     self.showList = []
     for s in temp:
         # Check for comments
         s2 = s.split()
         if len(s2) > 0:
             if '**' in s2[0]:
                 print('Comment')
             else:
                 self.showList.append(PuxShow(s, self.pBullet))
                 print('Added %s' % s2[0])
         else:
             print('Empty line')
     for show in self.showList:
         show.newTorrentURLs()
         # Update global variables
         show.checkTorrents()
Ejemplo n.º 20
0
	def addShows(self):
		# Load torrent list
		temp = BFun.ezRead(self.TORRENT_LIST_PATH)
		temp = temp.splitlines()
		# Delete old showList (for memory?) and make a new one.
		for show in self.showList:
			# Make sure all data is saved.
			show.saveData()
		self.showList.clear()
		self.showList = []
		for s in temp:
			# Check for comments
			s2 = s.split()
			if len(s2) > 0:
				if '**' in s2[0]:
					print('Comment')
				else:
					self.showList.append(PuxShow(s,self.pBullet))
					print('Added %s'%s2[0])
			else:
				print('Empty line')
		for show in self.showList:
			show.newTorrentURLs()
			# Update global variables
			show.checkTorrents()
Ejemplo n.º 21
0
 def findLatestEpisode(self, override=0):
     #  Latest episode based on own records (torrentList json file)
     tempLatestEp = -1
     if len(self.torrentList) > 0:
         for torrentDict in self.torrentList:
             if torrentDict["epNum"] > tempLatestEp:
                 tempLatestEp = torrentDict["epNum"]
         print("Latest torrent in torrent list = %d" % tempLatestEp)
     if override > tempLatestEp:
         self.latestEpisode = override
         print("self.latestEpisode = %d" % self.latestEpisode)
         return self.latestEpisode
     else:
         self.latestEpisode = tempLatestEp
         print("self.latestEpisode = %d" % self.latestEpisode)
         return self.latestEpisode
         # ===========================
         # Maybe later. Probably never
         # ===========================
         # Find latest episode in directory.
     try:
         # Try to get file list from show folder on file server.
         fileList = os.listdir(self.SHOW_PATH)
     except FileNotFoundError as ex:
         # Make the directory
         os.mkdir(self.SHOW_PATH)
         fileList = []
         # Maybe put in a file?
     notEpNums = BFun.ezRead(self.CONFIG_PATH + "/PuxShow_notEpNums.txt").splitlines()
     for fileName in fileList:
         fileName2 = fileName.lower()
         for ss in notEpNums:
             fileName2.replace(ss, "")
         numbers = re.findall(r"\d+", fileName2)
         if len(numbers) > 0:
             epNumGuess = int(numbers[-1])  # It's probably the last number we found
             # _Latest.txt files replaced with override input.
             # Find latest episode from data file
     try:
         # Load data file (e.g. /Data/Come Home Love_Latest.txt)
         dataFile = open(self.LATEST_DATA_PATH, "r")
         dataLatest = int(dataFile.read())
         dataFile.close()
     except FileNotFoundError as ex:
         # Couldn't read file
         dataFile = open(self.LATEST_DATA_PATH, "w")
         dataFile.write("-1")  # Write -1
         dataFile.close()
         dataLatest = -1
     except ValueError as ex:
         # Didn't contain an integer
         dataLatest = -1
     if dataLatest < directoryLatest:
         dataFile = open(self.LATEST_DATA_PATH, "w")
         dataFile.write(directoryLatest)  # Update data file
         dataFile.close()
         self.latestEpisode = directoryLatest
     else:
         self.latestEpisode = dataLatest
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
	def getAvgRates(self):
		while min(self.ratesPerRequest) < 1:
			# Remove all rates less than 1
			# Happens when rate resets or during initilisation.
			self.ratesPerRequest.remove(min(self.ratesPerRequest))
		while len(self.ratesPerRequest) > 99:
			# Keep length of stored rates below 100 to maintain speed.
			self.ratesPerRequest.pop()
		# Median is cooler than mean cause it's harder to calculate
		return math.ceil(BFun.median(self.ratesPerRequest))
Ejemplo n.º 24
0
 def getAvgRates(self):
     while min(self.ratesPerRequest) < 1:
         # Remove all rates less than 1
         # Happens when rate resets or during initilisation.
         self.ratesPerRequest.remove(min(self.ratesPerRequest))
     while len(self.ratesPerRequest) > 99:
         # Keep length of stored rates below 100 to maintain speed.
         self.ratesPerRequest.pop()
     # Median is cooler than mean cause it's harder to calculate
     return math.ceil(BFun.median(self.ratesPerRequest))
Ejemplo n.º 25
0
 def __init__(self, torrentLine, PuxBulletObject):
     # Initialise PuxShow class
     self.pBullet = PuxBulletObject
     self.TILDA = os.environ['HOME']  # Home directory of linux
     self.SERVER_PATH = PuxGlobal.SERVER_PATH
     self.LOCAL_PATH = PuxGlobal.LOCAL_PATH
     self.CONFIG_PATH = PuxGlobal.CONFIG_PATH
     self.DATA_PATH = PuxGlobal.DATA_PATH
     self.DOWNLOAD_PATH = self.LOCAL_PATH + '/torrentDL'
     self.transmissionSession = PuxTorrents.connectTransmission()
     # Decode torrentLine
     # * Show Name*Show URL*File Extension*Parent Directory*Max Size (MB)*Min Size (kB)*episodeOverride
     # 0           1        2              3                4             5             6
     lineParts = torrentLine.split('*')
     self.SHOW_NAME = lineParts[0]
     self.SHOW_URL = lineParts[1]
     self.EXTENSION = lineParts[2]
     self.SHOW_PATH = self.SERVER_PATH + '/' + lineParts[
         3] + '/' + self.SHOW_NAME
     self.LATEST_DATA_PATH = self.DATA_PATH + '/' + self.SHOW_NAME + '_Latest.txt'
     self.TORRENT_LIST_PATH = self.DATA_PATH + '/' + self.SHOW_NAME + '_torrentList.txt'
     #self.latestEpisode = self.findLatestEpisode()
     self.torrentList = []  # Empty list
     self.requestSleep = 1.1  # Time to sleep between requests.
     self.updateTorrentList()
     try:
         latestEpOverride = int(lineParts[6])
     except IndexError as ex:
         # Option not present. Start from episode 0
         latestEpOverride = 0
     except ValueError as ex:
         # Option present but incorrect. Log and start from episode 0
         BFun.ezLog(
             '%s entry in torrentlist.txt is incorrect. Override value should be integer'
             % self.SHOW_NAME)
         latestEpOverride = 0
     self.latestEpisode = -1
     self.findLatestEpisode(latestEpOverride)
     print('self.latestEpisode = %d' % self.latestEpisode)
     self.MAX_FILE_SIZE = int(lineParts[4]) * 1024 * 1024  # in bytes
     self.MIN_FILE_SIZE = int(lineParts[5]) * 1024  # in bytes
     self.pendingTorrents = False
Ejemplo n.º 26
0
	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
Ejemplo n.º 27
0
 def __init__(self, torrentLine, PuxBulletObject):
     # Initialise PuxShow class
     self.pBullet = PuxBulletObject
     self.TILDA = os.environ["HOME"]  # Home directory of linux
     self.SERVER_PATH = PuxGlobal.SERVER_PATH
     self.LOCAL_PATH = PuxGlobal.LOCAL_PATH
     self.CONFIG_PATH = PuxGlobal.CONFIG_PATH
     self.DATA_PATH = PuxGlobal.DATA_PATH
     self.DOWNLOAD_PATH = self.LOCAL_PATH + "/torrentDL"
     self.transmissionSession = PuxTorrents.connectTransmission()
     # Decode torrentLine
     # * Show Name*Show URL*File Extension*Parent Directory*Max Size (MB)*Min Size (kB)*episodeOverride
     # 0           1        2              3                4             5             6
     lineParts = torrentLine.split("*")
     self.SHOW_NAME = lineParts[0]
     self.SHOW_URL = lineParts[1]
     self.EXTENSION = lineParts[2]
     self.SHOW_PATH = self.SERVER_PATH + "/" + lineParts[3] + "/" + self.SHOW_NAME
     self.LATEST_DATA_PATH = self.DATA_PATH + "/" + self.SHOW_NAME + "_Latest.txt"
     self.TORRENT_LIST_PATH = self.DATA_PATH + "/" + self.SHOW_NAME + "_torrentList.txt"
     # self.latestEpisode = self.findLatestEpisode()
     self.torrentList = []  # Empty list
     self.requestSleep = 1.1  # Time to sleep between requests.
     self.updateTorrentList()
     try:
         latestEpOverride = int(lineParts[6])
     except IndexError as ex:
         # Option not present. Start from episode 0
         latestEpOverride = 0
     except ValueError as ex:
         # Option present but incorrect. Log and start from episode 0
         BFun.ezLog("%s entry in torrentlist.txt is incorrect. Override value should be integer" % self.SHOW_NAME)
         latestEpOverride = 0
     self.latestEpisode = -1
     self.findLatestEpisode(latestEpOverride)
     print("self.latestEpisode = %d" % self.latestEpisode)
     self.MAX_FILE_SIZE = int(lineParts[4]) * 1024 * 1024  # in bytes
     self.MIN_FILE_SIZE = int(lineParts[5]) * 1024  # in bytes
     self.pendingTorrents = False
Ejemplo n.º 28
0
 def getExcludeList(self):
     # Get list of *_exclude.txt files from config directory.
     configFileList = os.listdir(self.CONFIG_PATH)
     excludeFileList = []
     for fn in configFileList:
         if "_exclude.txt" in fn.lower():  # Don't get other files like torrentList.txt
             subStrings = fn.split("_")
             excludeFileList.append(subStrings[0])  # Add the name of the site the file is for.
             self.EXCLUDE_PATH = False
     for subStr in excludeFileList:
         if subStr.lower() in self.SHOW_URL.lower():
             excludePath = self.CONFIG_PATH + "/" + subStr + "_exclude.txt"
             return BFun.ezRead(excludePath).splitlines()
     return []  # if file doesn't exist, return empty list
Ejemplo n.º 29
0
	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
Ejemplo n.º 30
0
 def getExcludeList(self):
     # Get list of *_exclude.txt files from config directory.
     configFileList = os.listdir(self.CONFIG_PATH)
     excludeFileList = []
     for fn in configFileList:
         if '_exclude.txt' in fn.lower(
         ):  # Don't get other files like torrentList.txt
             subStrings = fn.split('_')
             excludeFileList.append(
                 subStrings[0])  # Add the name of the site the file is for.
             self.EXCLUDE_PATH = False
     for subStr in excludeFileList:
         if subStr.lower() in self.SHOW_URL.lower():
             excludePath = self.CONFIG_PATH + '/' + subStr + '_exclude.txt'
             return BFun.ezRead(excludePath).splitlines()
     return []  # if file doesn't exist, return empty list
Ejemplo n.º 31
0
	def __init__(self,myName='PuxHelper',sendOnly=True):
		self.version = 1
		self.TILDA = os.environ['HOME'] # Home directory of linux
		self.CONFIG_DIRECTORY = self.TILDA+'/Mount/fileServer/PUBLIC/Nick/PythonData/Configs'
		self.getAccessToken()
		self.myName = myName
		self.myIden = None
		self.sleepTime = 1 # Time to sleep between pushes.
		self.pushList = [] # Need to load push list from local file.
		self.ratesPerRequest = [5] # Last n ratesPerRequest
		self.previousRates = -1
		self.seenPushIdens = []
		self.knownDevices = []
		self.browserIdens = []
		self.sendOnly = sendOnly
		self.indicatorList = BFun.ezRead(self.CONFIG_DIRECTORY+'/puxhelper_indicators.txt').lower().splitlines()
		# This should be the last thing in this init.
		if not self.sendOnly:
			print('Getting all pushes.')
			self.refreshPushes(newOnly=False)
		self.getBrowserIdens()
Ejemplo n.º 32
0
 def __init__(self, myName='PuxHelper', sendOnly=True):
     self.version = 1
     self.TILDA = os.environ['HOME']  # Home directory of linux
     self.CONFIG_DIRECTORY = self.TILDA + '/Mount/fileServer/PUBLIC/Nick/PythonData/Configs'
     self.getAccessToken()
     self.myName = myName
     self.myIden = None
     self.sleepTime = 1  # Time to sleep between pushes.
     self.pushList = []  # Need to load push list from local file.
     self.ratesPerRequest = [5]  # Last n ratesPerRequest
     self.previousRates = -1
     self.seenPushIdens = []
     self.knownDevices = []
     self.browserIdens = []
     self.sendOnly = sendOnly
     self.indicatorList = BFun.ezRead(
         self.CONFIG_DIRECTORY +
         '/puxhelper_indicators.txt').lower().splitlines()
     # This should be the last thing in this init.
     if not self.sendOnly:
         print('Getting all pushes.')
         self.refreshPushes(newOnly=False)
     self.getBrowserIdens()
Ejemplo n.º 33
0
 def saveData(self):
     BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
Ejemplo n.º 34
0
# PushBullet module for PuxHelper.py
import requests
import json
import BFun
import re
import os
import time
import math
import operator
from requests.auth import HTTPBasicAuth
'''
Notes:
Save and load to json with:
BFun.jSaver(someJSON,somePath)
someJSON = BFun.jLoader(somePath)
'''


class PuxBullet:
    def __init__(self, myName='PuxHelper', sendOnly=True):
        self.version = 1
        self.TILDA = os.environ['HOME']  # Home directory of linux
        self.CONFIG_DIRECTORY = self.TILDA + '/Mount/fileServer/PUBLIC/Nick/PythonData/Configs'
        self.getAccessToken()
        self.myName = myName
        self.myIden = None
        self.sleepTime = 1  # Time to sleep between pushes.
        self.pushList = []  # Need to load push list from local file.
        self.ratesPerRequest = [5]  # Last n ratesPerRequest
        self.previousRates = -1
        self.seenPushIdens = []
Ejemplo n.º 35
0
 def __init__(self):
     super(PushBulletT, self).__init__()
     global globalPBullet
     self.pBullet = globalPBullet
     self.threadInitTic = BFun.tic()
Ejemplo n.º 36
0
 def newTorrentURLs(self, test=False):
     # Find the latest torrent URLs, add them (paused) to Transmission and check their size.
     excludeList = self.getExcludeList()
     searchDict = {}  # Empty dictionary
     searchDict["URL"] = self.SHOW_URL
     # searchDict['extension'] = self.EXTENSION
     foundTorrents = PuxTorrents.torrentSearch(searchDict)
     urlList = []
     tempEpNum = -1
     for t in foundTorrents:  # tuples in foundTorrents
         epNum = t[0]
         url = t[1]
         alreadyAdded = False
         if epNum <= self.latestEpisode:
             print("Already have episode %d" % epNum)
             alreadyAdded = True
         else:
             for torrentDict in self.torrentList:
                 if torrentDict["URL"] == url:
                     print("Already added URL")
                     alreadyAdded = True
         if not alreadyAdded:
             print("Processing %s ep %d" % (self.SHOW_NAME, epNum))
             # aa = BFun.tic()
             # add_torrent() will return control once it has finished adding the torrent.
             print("Getting .torrent...")
             torrentRetrys = 0
             gotTorrent = False
             while torrentRetrys < 99:
                 try:
                     self.requestSleep += 1.1
                     print("Sleeping %0.1f seconds before retrieving torrent" % self.requestSleep)
                     time.sleep(self.requestSleep)
                     # r = requests.get(url,timeout=10)
                     fileLikeObject = urllib.request.urlopen(url, None, 30)
                     gotTorrent = True
                     break
                 except urllib.error.URLError as ex:
                     if "error timed out" in str(ex).lower():
                         torrentRetrys += 1
                         print("Timed out. Trying again. %d" % torrentRetrys)
                     else:
                         BFun.ezLog("Error when retrieving .torrent file. Search for 9217568", ex)
                 except Exception as ex:
                     BFun.ezLog("Error when retrieving .torrent file. Search for 3195897", ex)
             if gotTorrent:
                 # Write data to .torrent file.
                 localURI = self.DATA_PATH + "/lastTorrent.torrent"
                 fo = open(localURI, "wb")  # Gets overwritten.
                 # fo.write(r.content)
                 shutil.copyfileobj(fileLikeObject, fo)
                 fo.close()
                 try:
                     # .torrent file may be bad?
                     torrent = self.transmissionSession.add_torrent(localURI, paused=True)
                     hashString = torrent.hashString
                     torrentContents = self.transmissionSession.get_torrent(hashString).files()
                 except Exception as ex:
                     BFun.ezLog("Error when sending .torrent file to transmission. Search for 4523861", ex)
                     # Add bogus torrent to torrentList with URL and Completed.
                     # Skip the URL now but don't mark it so we can try it later.
                     torrentDict = {
                         "URL": "a",
                         "completed": 1,
                         "epNum": -epNum,
                         "hashString": "brokenURL",
                         "wantedFileIDs": [],
                         "comment": ["ep %d" % epNum, "brokenURL", url],
                     }
                     self.torrentList.append(torrentDict)
                     self.saveData()
                     torrentContents = []  # Empty list so it won't be processed
                     # Log what happened.
                     BFun.ezLog("Bad .torrent file from %s. (ep %d, %s)" % (url, epNum, localURI))
                 time.sleep(2)
             else:
                 print("Didn't get torrent after 99 retrys.")
                 break
                 # Get the unique hash of the torrent so working with torrents is easier.
                 # tSess2.get_torrent(hashString).files()
             fileInfos = []
             wantedFiles = []
             unwantedFiles = []
             # Check if torrent has files we want.
             desiredTorrent = False
             for m in range(len(torrentContents)):
                 desiredFile = True
                 fileName = torrentContents[m]["name"]
                 fileSize = torrentContents[m]["size"]
                 nameSubs = fileName.split(".")
                 if nameSubs[-1].lower() in self.EXTENSION.lower():  # self.EXTENSION has the '.'
                     print("Processing %s" % fileName)
                     if fileSize > self.MAX_FILE_SIZE:
                         desiredFile = False
                         print("File %d too big" % m)
                     if fileSize < self.MIN_FILE_SIZE:
                         desiredFile = False
                         # print('File %d too small'%m)
                     for exSubString in excludeList:
                         if exSubString.lower() in fileName.lower():
                             desiredFile = False
                             print("Found %s in %s" % (exSubString, fileName))
                 else:
                     desiredFile = False
                     # print('File %d has wrong extension'%m)
                 if desiredFile:
                     desiredTorrent = True
                     wantedFiles.append(m)
                 else:
                     unwantedFiles.append(m)
             if len(wantedFiles) > 8:
                 # If there are more than 8 files, it's a compliation torrent which we don't want.
                 # There may be week's worth of episodes which is ok.
                 desiredTorrent = False
                 print("Found %d files. Rejecting" % len(wantedFiles))
             print("unwantedFiles:")
             print(unwantedFiles)
             # If torrentContents has no items, it's because it wasn't added. Check "unique hash" for human readable description
             if len(torrentContents) > 0:
                 self.transmissionSession.change_torrent(hashString, files_unwanted=unwantedFiles)
                 self.transmissionSession.change_torrent(hashString, files_wanted=wantedFiles)
             if desiredTorrent:
                 urlList.append(url)
                 # If it's a behind the scenes or something, don't change the actual episode number.
                 if epNum != 9999999:
                     print("Found episode %d" % epNum)
                     tempEpNum = max([epNum, tempEpNum])  # Largest number in the found torrents.
                 else:
                     epNum = -1
                 if test:
                     # We're just testing so remove the torrent.
                     self.transmissionSession.remove_torrent(hashString, delete_data=True)
                 else:
                     # Start the torrent
                     self.transmissionSession.start_torrent(hashString)
                     self.pendingTorrents = True
                     # Add to self.torrentList
                     torrentDict = {}
                     torrentDict["hashString"] = hashString
                     torrentDict["wantedFileIDs"] = wantedFiles
                     # 0 = still downloading. -1 = complete on Transmission, not moved to fileserver. 1 = moved to fileserver, removed from transmission
                     torrentDict["completed"] = 0
                     torrentDict["URL"] = url
                     torrentDict["epNum"] = epNum
                     self.torrentList.append(torrentDict)
                     BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
             else:
                 # Not a torrent we want so remove from transmission.
                 if len(torrentContents) > 0:
                     self.transmissionSession.remove_torrent(hashString, delete_data=True)
                     # and Add bogus torrent to torrentList with URL and Completed so we don't download it again.
                     torrentDict = {
                         "URL": url,
                         "completed": 1,
                         "epNum": -epNum,
                         "hashString": "noFiles",
                         "wantedFileIDs": [],
                     }
                     self.torrentList.append(torrentDict)
                     self.saveData()
     if tempEpNum > self.latestEpisode:  # Update latest episode number
         print("tempEpNum = %d" % tempEpNum)
         self.latestEpisode = tempEpNum
         # dataFile = open(self.LATEST_DATA_PATH,'w')
         # dataFile.write('%d'%tempEpNum)
         # dataFile.close()
         print("Updated data file")
     if test:
         return urlList
     else:
         return urlList
Ejemplo n.º 37
0
	def torrentCommand(self,arguments,sender):
		# Trim completed torrents
		orMore = ''
		while len(PuxGlobal.globalRecentCompleteTorrents) > 10:
			PuxGlobal.globalRecentCompleteTorrents.pop(0)
			orMore = '+'
		# Build some strings
		completeBody = ''
		currentBody = ''
		if len(PuxGlobal.globalRecentCompleteTorrents) > 0:
			completeBody = 'Completed:'
		for completeDictionary in PuxGlobal.globalRecentCompleteTorrents:
			show = completeDictionary['show']
			epNum = completeDictionary['epNum']
			tic = completeDictionary['tic']
			tocString = BFun.toc2Str(tic)
			temp = '\n%s Ep%d (%s ago)'%(show,epNum,tocString)
			completeBody = completeBody+temp
		currentKeys = list(PuxGlobal.globalCurrentTorrentsProgress.keys())
		#currentBody = 'Transferring:'
		currentCounter = 0
		for showName in currentKeys:
			for episodeDict in PuxGlobal.globalCurrentTorrentsProgress[showName]:
				temp = '\n%0.0f%% - Ep%d of %s'%(episodeDict['percent'],episodeDict['epNum'],showName)
				currentBody = currentBody+temp
				currentCounter = currentCounter + 1
		if currentCounter > 0:
			currentBody =  'Transferring %d:'%currentCounter + currentBody
		body = ''
		if len(arguments) > 0:
			allArguments = ''.join(arguments).lower()
			if 'all' in allArguments:
				body = completeBody+'\n'+currentBody
			else:
				if 'complete' in allArguments:
					body = completeBody
				if 'current' in allArguments:
					# If no torrents are in progress, leave body empty.
					if (len(body) > 0) and (len(currentBody) > 0):
						body = body+'\n'
					body = body+currentBody
			if 'fast' in allArguments:
				body = 'fast did nothing.'
				# Check how long to speed up for. In hours+17 seconds.
				try:
					hours = int(arguments[-1])
				except ValueError as ex:
					hours = 1
				seconds = hours*3617
				PuxGlobal.torretSpeedResetTime[0] = time.time() +  seconds
				try:
					tempClient = transmissionrpc.Client()
					tempClient.set_session(None,alt_speed_down=99999) # Fast enough probably.
					body = 'Set high download speed for %0.0f hours'%hours
				except transmissionrpc.error.TransmissionError as ex:
					# Transmission not running?
					body = 'Couldn\'t connect to Transmission'
		else:
			if len(completeBody) > 0:
				body = completeBody
			else:
				body = currentBody
		# If body is still empty after everything, add a message
		if len(body) == 0:
			body = 'No torrents to report or invalid command.'
		title = None
		self.pBullet.sendNote(title,body,sender)
Ejemplo n.º 38
0
 def saveData(self):
     BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
Ejemplo n.º 39
0
 def newTorrentURLs(self, test=False):
     # Find the latest torrent URLs, add them (paused) to Transmission and check their size.
     excludeList = self.getExcludeList()
     searchDict = {}  # Empty dictionary
     searchDict['URL'] = self.SHOW_URL
     #searchDict['extension'] = self.EXTENSION
     foundTorrents = PuxTorrents.torrentSearch(searchDict)
     urlList = []
     tempEpNum = -1
     for t in foundTorrents:  # tuples in foundTorrents
         epNum = t[0]
         url = t[1]
         alreadyAdded = False
         if epNum <= self.latestEpisode:
             print('Already have episode %d' % epNum)
             alreadyAdded = True
         else:
             for torrentDict in self.torrentList:
                 if torrentDict['URL'] == url:
                     print('Already added URL')
                     alreadyAdded = True
         if not alreadyAdded:
             print('Processing %s ep %d' % (self.SHOW_NAME, epNum))
             #aa = BFun.tic()
             # add_torrent() will return control once it has finished adding the torrent.
             print('Getting .torrent...')
             torrentRetrys = 0
             gotTorrent = False
             while torrentRetrys < 99:
                 try:
                     self.requestSleep += 1.1
                     print(
                         'Sleeping %0.1f seconds before retrieving torrent'
                         % self.requestSleep)
                     time.sleep(self.requestSleep)
                     #r = requests.get(url,timeout=10)
                     fileLikeObject = urllib.request.urlopen(url, None, 30)
                     gotTorrent = True
                     break
                 except urllib.error.URLError as ex:
                     if 'error timed out' in str(ex).lower():
                         torrentRetrys += 1
                         print('Timed out. Trying again. %d' %
                               torrentRetrys)
                     else:
                         BFun.ezLog(
                             'Error when retrieving .torrent file. Search for 9217568',
                             ex)
                 except Exception as ex:
                     BFun.ezLog(
                         'Error when retrieving .torrent file. Search for 3195897',
                         ex)
             if gotTorrent:
                 # Write data to .torrent file.
                 localURI = self.DATA_PATH + '/lastTorrent.torrent'
                 fo = open(localURI, 'wb')  # Gets overwritten.
                 #fo.write(r.content)
                 shutil.copyfileobj(fileLikeObject, fo)
                 fo.close()
                 try:
                     # .torrent file may be bad?
                     torrent = self.transmissionSession.add_torrent(
                         localURI, paused=True)
                     hashString = torrent.hashString
                     torrentContents = self.transmissionSession.get_torrent(
                         hashString).files()
                 except Exception as ex:
                     BFun.ezLog(
                         'Error when sending .torrent file to transmission. Search for 4523861',
                         ex)
                     # Add bogus torrent to torrentList with URL and Completed.
                     # Skip the URL now but don't mark it so we can try it later.
                     torrentDict = {
                         'URL': 'a',
                         'completed': 1,
                         'epNum': -epNum,
                         'hashString': 'brokenURL',
                         'wantedFileIDs': [],
                         'comment': ['ep %d' % epNum, 'brokenURL', url]
                     }
                     self.torrentList.append(torrentDict)
                     self.saveData()
                     torrentContents = [
                     ]  # Empty list so it won't be processed
                     # Log what happened.
                     BFun.ezLog('Bad .torrent file from %s. (ep %d, %s)' %
                                (url, epNum, localURI))
                 time.sleep(2)
             else:
                 print("Didn't get torrent after 99 retrys.")
                 break
             # Get the unique hash of the torrent so working with torrents is easier.
             #tSess2.get_torrent(hashString).files()
             fileInfos = []
             wantedFiles = []
             unwantedFiles = []
             # Check if torrent has files we want.
             desiredTorrent = False
             for m in range(len(torrentContents)):
                 desiredFile = True
                 fileName = torrentContents[m]['name']
                 fileSize = torrentContents[m]['size']
                 nameSubs = fileName.split('.')
                 if nameSubs[-1].lower() in self.EXTENSION.lower(
                 ):  # self.EXTENSION has the '.'
                     print('Processing %s' % fileName)
                     if fileSize > self.MAX_FILE_SIZE:
                         desiredFile = False
                         print('File %d too big' % m)
                     if fileSize < self.MIN_FILE_SIZE:
                         desiredFile = False
                         #print('File %d too small'%m)
                     for exSubString in excludeList:
                         if exSubString.lower() in fileName.lower():
                             desiredFile = False
                             print('Found %s in %s' %
                                   (exSubString, fileName))
                 else:
                     desiredFile = False
                     #print('File %d has wrong extension'%m)
                 if desiredFile:
                     desiredTorrent = True
                     wantedFiles.append(m)
                 else:
                     unwantedFiles.append(m)
             if len(wantedFiles) > 8:
                 # If there are more than 8 files, it's a compliation torrent which we don't want.
                 # There may be week's worth of episodes which is ok.
                 desiredTorrent = False
                 print('Found %d files. Rejecting' % len(wantedFiles))
             print('unwantedFiles:')
             print(unwantedFiles)
             # If torrentContents has no items, it's because it wasn't added. Check "unique hash" for human readable description
             if len(torrentContents) > 0:
                 self.transmissionSession.change_torrent(
                     hashString, files_unwanted=unwantedFiles)
                 self.transmissionSession.change_torrent(
                     hashString, files_wanted=wantedFiles)
             if desiredTorrent:
                 urlList.append(url)
                 # If it's a behind the scenes or something, don't change the actual episode number.
                 if epNum != 9999999:
                     print('Found episode %d' % epNum)
                     tempEpNum = max([
                         epNum, tempEpNum
                     ])  # Largest number in the found torrents.
                 else:
                     epNum = -1
                 if test:
                     # We're just testing so remove the torrent.
                     self.transmissionSession.remove_torrent(
                         hashString, delete_data=True)
                 else:
                     # Start the torrent
                     self.transmissionSession.start_torrent(hashString)
                     self.pendingTorrents = True
                     # Add to self.torrentList
                     torrentDict = {}
                     torrentDict['hashString'] = hashString
                     torrentDict['wantedFileIDs'] = wantedFiles
                     # 0 = still downloading. -1 = complete on Transmission, not moved to fileserver. 1 = moved to fileserver, removed from transmission
                     torrentDict['completed'] = 0
                     torrentDict['URL'] = url
                     torrentDict['epNum'] = epNum
                     self.torrentList.append(torrentDict)
                     BFun.jSaver(self.torrentList, self.TORRENT_LIST_PATH)
             else:
                 # Not a torrent we want so remove from transmission.
                 if len(torrentContents) > 0:
                     self.transmissionSession.remove_torrent(
                         hashString, delete_data=True)
                     # and Add bogus torrent to torrentList with URL and Completed so we don't download it again.
                     torrentDict = {
                         'URL': url,
                         'completed': 1,
                         'epNum': -epNum,
                         'hashString': 'noFiles',
                         'wantedFileIDs': []
                     }
                     self.torrentList.append(torrentDict)
                     self.saveData()
     if tempEpNum > self.latestEpisode:  # Update latest episode number
         print('tempEpNum = %d' % tempEpNum)
         self.latestEpisode = tempEpNum
         #dataFile = open(self.LATEST_DATA_PATH,'w')
         #dataFile.write('%d'%tempEpNum)
         #dataFile.close()
         print('Updated data file')
     if test:
         return urlList
     else:
         return urlList
Ejemplo n.º 40
0
	def __init__(self):
		super(PushBulletT, self).__init__()
		global globalPBullet
		self.pBullet = globalPBullet
		self.threadInitTic = BFun.tic()
Ejemplo n.º 41
0
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)
Ejemplo n.º 42
0
 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
Ejemplo n.º 43
0
 def getAccessToken(self):
     temp = BFun.ezRead(self.TILDA + '/private/pushbullettoken.txt')
     self.ACCESS_TOKEN = temp.splitlines()[0]
Ejemplo n.º 44
0
	def getAccessToken(self):
		temp = BFun.ezRead(self.TILDA+'/private/pushbullettoken.txt')
		self.ACCESS_TOKEN = temp.splitlines()[0]
Ejemplo n.º 45
0
	def checkTorrentList(self):
		currentHash = BFun.ezHash(self.TORRENT_LIST_PATH)
		if self.previousTorrentListHash != currentHash:
			self.previousTorrentListHash = currentHash
			self.counters['add'] = 0
Ejemplo n.º 46
0
 def findLatestEpisode(self, override=0):
     #  Latest episode based on own records (torrentList json file)
     tempLatestEp = -1
     if len(self.torrentList) > 0:
         for torrentDict in self.torrentList:
             if torrentDict['epNum'] > tempLatestEp:
                 tempLatestEp = torrentDict['epNum']
         print('Latest torrent in torrent list = %d' % tempLatestEp)
     if override > tempLatestEp:
         self.latestEpisode = override
         print('self.latestEpisode = %d' % self.latestEpisode)
         return self.latestEpisode
     else:
         self.latestEpisode = tempLatestEp
         print('self.latestEpisode = %d' % self.latestEpisode)
         return self.latestEpisode
     # ===========================
     # Maybe later. Probably never
     # ===========================
     # Find latest episode in directory.
     try:
         # Try to get file list from show folder on file server.
         fileList = os.listdir(self.SHOW_PATH)
     except FileNotFoundError as ex:
         # Make the directory
         os.mkdir(self.SHOW_PATH)
         fileList = []
     # Maybe put in a file?
     notEpNums = BFun.ezRead(self.CONFIG_PATH +
                             '/PuxShow_notEpNums.txt').splitlines()
     for fileName in fileList:
         fileName2 = fileName.lower()
         for ss in notEpNums:
             fileName2.replace(ss, '')
         numbers = re.findall(r'\d+', fileName2)
         if len(numbers) > 0:
             epNumGuess = int(
                 numbers[-1])  # It's probably the last number we found
     # _Latest.txt files replaced with override input.
     # Find latest episode from data file
     try:
         # Load data file (e.g. /Data/Come Home Love_Latest.txt)
         dataFile = open(self.LATEST_DATA_PATH, 'r')
         dataLatest = int(dataFile.read())
         dataFile.close()
     except FileNotFoundError as ex:
         # Couldn't read file
         dataFile = open(self.LATEST_DATA_PATH, 'w')
         dataFile.write('-1')  # Write -1
         dataFile.close()
         dataLatest = -1
     except ValueError as ex:
         # Didn't contain an integer
         dataLatest = -1
     if dataLatest < directoryLatest:
         dataFile = open(self.LATEST_DATA_PATH, 'w')
         dataFile.write(directoryLatest)  # Update data file
         dataFile.close()
         self.latestEpisode = directoryLatest
     else:
         self.latestEpisode = dataLatest
Ejemplo n.º 47
0
# PushBullet module for PuxHelper.py
import requests
import json
import BFun
import re
import os
import time
import math
import operator
from requests.auth import HTTPBasicAuth

'''
Notes:
Save and load to json with:
BFun.jSaver(someJSON,somePath)
someJSON = BFun.jLoader(somePath)
'''

class PuxBullet:
	
	def __init__(self,myName='PuxHelper',sendOnly=True):
		self.version = 1
		self.TILDA = os.environ['HOME'] # Home directory of linux
		self.CONFIG_DIRECTORY = self.TILDA+'/Mount/fileServer/PUBLIC/Nick/PythonData/Configs'
		self.getAccessToken()
		self.myName = myName
		self.myIden = None
		self.sleepTime = 1 # Time to sleep between pushes.
		self.pushList = [] # Need to load push list from local file.
		self.ratesPerRequest = [5] # Last n ratesPerRequest
		self.previousRates = -1
Ejemplo n.º 48
0
 def checkTorrentList(self):
     currentHash = BFun.ezHash(self.TORRENT_LIST_PATH)
     if self.previousTorrentListHash != currentHash:
         self.previousTorrentListHash = currentHash
         self.counters['add'] = 0
Ejemplo n.º 49
0
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)
Ejemplo n.º 50
0
 def torrentCommand(self, arguments, sender):
     # Trim completed torrents
     orMore = ''
     while len(PuxGlobal.globalRecentCompleteTorrents) > 10:
         PuxGlobal.globalRecentCompleteTorrents.pop(0)
         orMore = '+'
     # Build some strings
     completeBody = ''
     currentBody = ''
     if len(PuxGlobal.globalRecentCompleteTorrents) > 0:
         completeBody = 'Completed:'
     for completeDictionary in PuxGlobal.globalRecentCompleteTorrents:
         show = completeDictionary['show']
         epNum = completeDictionary['epNum']
         tic = completeDictionary['tic']
         tocString = BFun.toc2Str(tic)
         temp = '\n%s Ep%d (%s ago)' % (show, epNum, tocString)
         completeBody = completeBody + temp
     currentKeys = list(PuxGlobal.globalCurrentTorrentsProgress.keys())
     #currentBody = 'Transferring:'
     currentCounter = 0
     for showName in currentKeys:
         for episodeDict in PuxGlobal.globalCurrentTorrentsProgress[
                 showName]:
             temp = '\n%0.0f%% - Ep%d of %s' % (
                 episodeDict['percent'], episodeDict['epNum'], showName)
             currentBody = currentBody + temp
             currentCounter = currentCounter + 1
     if currentCounter > 0:
         currentBody = 'Transferring %d:' % currentCounter + currentBody
     body = ''
     if len(arguments) > 0:
         allArguments = ''.join(arguments).lower()
         if 'all' in allArguments:
             body = completeBody + '\n' + currentBody
         else:
             if 'complete' in allArguments:
                 body = completeBody
             if 'current' in allArguments:
                 # If no torrents are in progress, leave body empty.
                 if (len(body) > 0) and (len(currentBody) > 0):
                     body = body + '\n'
                 body = body + currentBody
         if 'fast' in allArguments:
             body = 'fast did nothing.'
             # Check how long to speed up for. In hours+17 seconds.
             try:
                 hours = int(arguments[-1])
             except ValueError as ex:
                 hours = 1
             seconds = hours * 3617
             PuxGlobal.torretSpeedResetTime[0] = time.time() + seconds
             try:
                 tempClient = transmissionrpc.Client()
                 tempClient.set_session(
                     None, alt_speed_down=99999)  # Fast enough probably.
                 body = 'Set high download speed for %0.0f hours' % hours
             except transmissionrpc.error.TransmissionError as ex:
                 # Transmission not running?
                 body = 'Couldn\'t connect to Transmission'
     else:
         if len(completeBody) > 0:
             body = completeBody
         else:
             body = currentBody
     # If body is still empty after everything, add a message
     if len(body) == 0:
         body = 'No torrents to report or invalid command.'
     title = None
     self.pBullet.sendNote(title, body, sender)
Ejemplo n.º 51
0
import PuxGlobal
import BFun
import transmissionrpc
import re
import os
import time
import PuxTorrents
import shutil
import urllib.request
import requests
from PuxBulletf import PuxBullet

"""
Notes:
Save and load self.torrentList to a json with:
BFun.jSaver(someJSON,somePath)
someJSON = BFun.jLoader(somePath)

"""

# global PuxGlobal.globalRecentCompleteTorrents # List of dictionaries
# global PuxGlobal.globalCurrentTorrentsProgress # Dictionary of list of dictionaries


class PuxShow:
    # Pre define some functions so things work?
    # def findLatestEpisode(self):
    # pass
    # Class to handle show torrents, files on server etc.
    def __init__(self, torrentLine, PuxBulletObject):
        # Initialise PuxShow class