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