Esempio n. 1
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()
Esempio n. 2
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)
Esempio n. 3
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
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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()
Esempio n. 7
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)
Esempio 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
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
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
Esempio n. 12
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
Esempio n. 13
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
Esempio n. 14
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
Esempio n. 15
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
Esempio n. 16
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
Esempio n. 17
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)
Esempio n. 18
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)