"God.Bless.America.2011.HDTVRiP720.mkv"
I get a file with the name ", God Bless America ( Bobcat Goldthwait) (2011) , , , , HDTVRip-AVC.mkv"
# -*- encoding: utf-8 -*- import os import re import urllib.request import shutil import sys import ctypes INPUT_DIR = 'D:/Downloads/uTorrent/Completed' OUTPUT_DIR = 'D:/Video/Movies' TORRENT_DIR = 'D:/Downloads/uTorrent/torrent' MOVE_ALGORITHM = 'link' def GetMoveAlgorithms(): return {'move' : MoveFile, 'copy' : CopyFile, 'link' : CreateHardLink} def Print(msg): msg = msg.encode('cp866', 'replace') msg = msg.decode('cp866') print(msg) def GetTorrentFilePath(fileName): filePath = os.path.join(TORRENT_DIR, fileName + '.torrent') if not os.path.exists(filePath): Print('Skiped, .torrent is not found: "%s' % filePath) return None return filePath def GetTrackerUrl(torrentFilePath): try: torrentFile = open(torrentFilePath, 'r', encoding='ascii', errors='replace') fileData = torrentFile.read() trackerUrlLen, trackerUrl = re.search(r'comment([0-9]{2}):(.+)', fileData).groups() trackerUrl = re.search(r'(.{' + trackerUrlLen + '})', trackerUrl).groups()[0] return trackerUrl except: Print("Error, can't extract tracker url from .torrent file %s" % torrentFilePath) return None def LoadTrackerPage(trackerUrl): try: response = urllib.request.urlopen(trackerUrl) htmlPage = response.read() except: Print("Error, Can't load tracker page '%s'" % trackerUrl) return None htmlPage = htmlPage.decode('cp1251') return htmlPage def PrepareFileName(fileName): try: #remove special symbols fileName = re.sub(r'[\\/:"\*?<>|]+', '', fileName, 0, re.UNICODE) #remove repeating spaces fileName = re.sub(r'[ ]+', ' ', fileName, 0, re.UNICODE) fileName = fileName.strip() except: Print("Error, can't prepare file name '%s'" % fileName) return None return fileName class FileInfo: pass def ParseTrackerPage(htmlPage): try: pageTitle = re.search(r'<title>(.+?) :: .+?</title>', htmlPage, re.UNICODE).groups()[0] except: Print("Error, Can't parse <title>") return None fileInfo = FileInfo() fileInfo.name = "" fileInfo.year = "" fileInfo.descr = "" try: fileInfo.name, fileInfo.year, fileInfo.descr = re.search(r'(.+?) \[([0-9]{4}).*?, (.+?)\]', pageTitle, re.UNICODE).groups() except: Print("Warning, Can't parse page title: %s" % pageTitle) try: fileInfo.name, fileInfo.year, fileInfo.descr = re.search(r'(.+?)([0-9]{4}).*?, (.+?)$', pageTitle, re.UNICODE).groups() except: Print("Warning, Can't parse page title: %s" % pageTitle) fileInfo.name = pageTitle return fileInfo def GetDataFromTorrent(fileName): torrentFilePath = GetTorrentFilePath(fileName) if not torrentFilePath: return None trackerUrl = GetTrackerUrl(torrentFilePath) if not trackerUrl: return None htmlPage = LoadTrackerPage(trackerUrl) if not htmlPage: return None return ParseTrackerPage(htmlPage) def PrepareNewFileName(fileName, fileInfo): tmp, ext = os.path.splitext(fileName) toPrepare = fileInfo.name + ' (' + fileInfo.year + ') ' + fileInfo.descr cleanName = PrepareFileName(toPrepare) newFileName = cleanName + ext return newFileName def MoveFile(src, dst): shutil.move(src, dst) def CopyFile(src, dst): if os.path.isdir(src): for fileName in os.listdir(src): if not os.path.exists(dst): os.mkdir(dst) subSrc = os.path.join(src, fileName) subDst = os.path.join(dst, fileName) CopyFile(src, dst) else: if not os.path.exists(dst): shutil.copy2(src, dst) def CreateHardLink(src, dst): CreateHardLinkW = ctypes.windll.kernel32.CreateHardLinkW CreateHardLinkW.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_void_p) CreateHardLinkW.restype = ctypes.c_int if os.path.isdir(src): for fileName in os.listdir(src): if not os.path.exists(dst): os.mkdir(dst) subSrc = os.path.join(src, fileName) subDst = os.path.join(dst, fileName) CreateHardLink(subSrc, subDst) else: if not os.path.exists(dst): if CreateHardLinkW(dst, src, 0) == 0: raise IOError def main(): Print('Hello, Find downloads in "%s" :' % INPUT_DIR) totalCount = 0 processedCount = 0 for fileName in os.listdir(INPUT_DIR): totalCount = totalCount + 1 Print('Process a file: "%s"' % fileName) fileInfo = GetDataFromTorrent(fileName) if fileInfo is None: continue sNewFileName = PrepareNewFileName(fileName, fileInfo) if sNewFileName: oldFilePath = os.path.join(INPUT_DIR, fileName) newFilePath = os.path.join(OUTPUT_DIR, sNewFileName) try: GetMoveAlgorithms()[MOVE_ALGORITHM](oldFilePath, newFilePath) processedCount = processedCount + 1 except: Print("Error, Can't move file from %s to %s" % (oldFilePath, newFilePath)) Print("%d friles were moved from %d total found files" % (processedCount, totalCount)) if __name__ == "__main__": main()
God.Bless.America.2011.HDTVRiP720.mkv
file seems not convincing to you, here is a list of top files from the root tracker.Source: https://habr.com/ru/post/144938/
All Articles