def main(): ss = Preferences() torrentlist = bencode.decode_from_file(ss.get("utresumedat")) partiallist = [] # set up an empty container for desired data to get put into for later fileguarduseless = torrentlist.pop(b".fileguard",None) rec = torrentlist.pop(b"rec",None) #Remove this. #(dict. comprehension expects only dicts as the root keys) #create a reverse lookup dict with "Dict comprehension". nice and simple eh? ;-) reverselookup={base64.b16encode(value[b"info"]):[value[b"path"],value[b"caption"],origkey] for origkey,value in torrentlist.items()} for thehash,value in reverselookup.items(): partiallist.append([value[0].decode('utf-8'),value[1].decode('utf-8'),thehash.decode('utf-8')]) #Those 3 lines replace all of this: # for key,value in torrentlist.items(): # sentinel = False # reset before each while-loop # if b"path" in value: # path = value[b"path"].decode('utf-8') # if b"caption" in value: # caption = value[b"caption"].decode('utf-8') # if b"info" in value: # infoHash = base64.b16encode(value[b"info"]).decode('utf-8') # sentinel = True # need this because theres other dictionaries INside each file-entries' dict... # # and this will trigger the partiallist.append to write only file-entry dicts. # if sentinel == True: # partiallist.append([path,caption,infoHash]) partiallist.sort() writelistfile = open(os.path.join(ss.get("maindir"),"TorrentList.txt"),'w',encoding='utf-8') # write-out a text file with one entry per line. for eachline in partiallist: writelistfile.write(eachline[0] + " / " + eachline[1] + " / " + eachline[2] + "\n") #path / #caption / #infohash writelistfile.close()
def main(): ss = Preferences() torrentlist = bencode.decode_from_file(ss.get("utresumedat")) partiallist = [ ] # set up an empty container for desired data to get put into for later fileguarduseless = torrentlist.pop(".fileguard", None) rec = torrentlist.pop("rec", None) #Remove this. #(dict. comprehension expects only dicts as the root keys) #create a reverse lookup dict with "Dict comprehension". nice and simple eh? ;-) reverselookup = { base64.b16encode(value["info"]): [value["path"], value["caption"], origkey] for origkey, value in torrentlist.iteritems() } for thehash, value in reverselookup.iteritems(): partiallist.append([value[0], value[1], thehash]) partiallist.sort() writelistfile = open( os.path.join(ss.get("maindir"), "TorrentList.txt"), 'wb') # write-out a text file with one entry per line. for eachline in partiallist: writelistfile.write(eachline[0] + " / " + eachline[1] + " / " + eachline[2] + "\n") #path / #caption / #infohash writelistfile.close() print "Finished writing: TorrentList.txt"
def main(): ss = Preferences() script1sourcedir = ss.getwpath( u"script1sourcedir" ) + u'' #("seeding\"), needs unicode u for file opening. files = [ os.path.join(script1sourcedir, filename) for filename in next(os.walk(script1sourcedir))[2] ] #gives absolute paths + names currentfile = 0 container = [ ] #set up an empty container for desired data to get put into for later for eachfile in files: metainfo = bencode.decode_from_file(eachfile) # #need to manually SHA1 hash the torrent file's info-dict to get the info-hash infodict = metainfo['info'] info_hash = hashlib.sha1(bencode.bencode(infodict)).hexdigest().upper() internalname = infodict['name'] torrentfilename = eachfile[eachfile.rfind("\\") + 1:] locextension = torrentfilename.find( ".torrent") #location of extension (char position) locid = torrentfilename.rfind( "-") + 1 #location of torrentID (char position) torrentid = torrentfilename[locid:locextension] #grab torrentID container.append([torrentfilename, internalname, info_hash, torrentid]) currentfile += 1 print currentfile, torrentfilename.encode( 'ascii', errors='ignore' ) #console output is ascii only, cannot print unicode - chars are omitted #WRITE FILE 1 writelistfile = codecs.open( ss.getwpath("outpath1"), 'wb', "utf-8" ) # write-out a text file with torrentID and Hash (on one line) ("1seeding_ID+Hash+Filename.txt") for eachline in container: writelistfile.write(eachline[3] + " / " + eachline[2] + " / " + eachline[0] + "\n") #output torrentID / Hash / torrentfilename writelistfile.close()
def main(): ss = Preferences() torrentlist = bencode.decode_from_file(ss.get("utresumedat")) partiallist = [] # set up an empty container for desired data to get put into for later fileguarduseless = torrentlist.pop(".fileguard",None) rec = torrentlist.pop("rec",None) #Remove this. #(dict. comprehension expects only dicts as the root keys) #create a reverse lookup dict with "Dict comprehension". nice and simple eh? ;-) reverselookup={base64.b16encode(value["info"]):[value["path"],value["caption"],origkey] for origkey,value in torrentlist.iteritems()} for thehash,value in reverselookup.iteritems(): partiallist.append([value[0],value[1],thehash]) partiallist.sort() writelistfile = open(os.path.join(ss.get("maindir"),"TorrentList.txt"),'wb') # write-out a text file with one entry per line. for eachline in partiallist: writelistfile.write(eachline[0] + " / " + eachline[1] + " / " + eachline[2] + "\n") #path / #caption / #infohash writelistfile.close() print "Finished writing: TorrentList.txt"
def main(): ss = Preferences() script1sourcedir = ss.getwpath(u"script1sourcedir") + u"" # ("seeding\"), needs unicode u for file opening. files = [ os.path.join(script1sourcedir, filename) for filename in next(os.walk(script1sourcedir))[2] ] # gives absolute paths + names currentfile = 0 container = [] # set up an empty container for desired data to get put into for later for eachfile in files: metainfo = bencode.decode_from_file(eachfile) # #need to manually SHA1 hash the torrent file's info-dict to get the info-hash infodict = metainfo["info"] info_hash = hashlib.sha1(bencode.bencode(infodict)).hexdigest().upper() internalname = infodict["name"] torrentfilename = eachfile[eachfile.rfind("\\") + 1 :] locextension = torrentfilename.find(".torrent") # location of extension (char position) locid = torrentfilename.rfind("-") + 1 # location of torrentID (char position) torrentid = torrentfilename[locid:locextension] # grab torrentID container.append([torrentfilename, internalname, info_hash, torrentid]) currentfile += 1 print currentfile, torrentfilename.encode( "ascii", errors="ignore" ) # console output is ascii only, cannot print unicode - chars are omitted # WRITE FILE 1 writelistfile = codecs.open( ss.getwpath("outpath1"), "wb", "utf-8" ) # write-out a text file with torrentID and Hash (on one line) ("1seeding_ID+Hash+Filename.txt") for eachline in container: writelistfile.write( eachline[3] + " / " + eachline[2] + " / " + eachline[0] + "\n" ) # output torrentID / Hash / torrentfilename writelistfile.close()
def main(): ss = Preferences() script1sourcedir = ss.getwpath( u"script1sourcedir" ) + u'' #("seeding\"), needs unicode u for file opening. files = [ os.path.join(script1sourcedir, filename) for filename in next(os.walk(script1sourcedir))[2] ] #gives absolute paths + names currentfile = 0 container = [ ] #set up an empty container for desired data to get put into for later for eachfile in files: metainfo = bencode.decode_from_file(eachfile) # #need to manually SHA1 hash the torrent file's info-dict to get the info-hash infodict = metainfo['info'] info_hash = hashlib.sha1(bencode.bencode(infodict)).hexdigest().upper() internalname = infodict['name'] torrentfilename = eachfile[eachfile.rfind("\\") + 1:] locextension = torrentfilename.find( ".torrent") #location of extension (char position) locid = torrentfilename.rfind( "-") + 1 #location of torrentID (char position) torrentid = torrentfilename[locid:locextension] #grab torrentID torrentfilename = torrentfilename[:locid - 1] #####-------------replace banned characters with unicode section-----------------###### ### # Forward slashes are strange. "FullWidth" is very wide and would be too wide if theres already spaces around it. torrentfilename = torrentfilename.replace( " / ", u"/") # U+FFOF (wide) FULLWIDTH SOLIDUS # "Division" slash is too narrow and needs spaces inserted surrounding it (and is still less width than the fullwidth) torrentfilename = torrentfilename.replace( "/", u" ∕ ") # U+2215 (narrow) DIVISION SLASH # Backslash (requires two slashes in python) torrentfilename = torrentfilename.replace( "\\", u"\") # U+FF3C FULLWIDTH REVERSE SOLIDUS # Colon torrentfilename = torrentfilename.replace( ":", u"꞉") # U+A789 MODIFIER LETTER COLON # asterisk torrentfilename = torrentfilename.replace( "*", u"※") # U+203B REFERENCE MARK # question mark (replacement is backwards, sorry) torrentfilename = torrentfilename.replace( "?", u"؟") # U+061F ARABIC QUESTION MARK # Double-quote torrentfilename = torrentfilename.replace( '"', u"ʺ") # U+02BA MODIFIER LETTER DOUBLE PRIME # Left angle bracket torrentfilename = torrentfilename.replace( "<", u"˂") # U+02C2 MODIFIER LETTER LEFT ARROWHEAD # right angle bracket torrentfilename = torrentfilename.replace( ">", u"˃") # U+02C3 MODIFIER LETTER RIGHT ARROWHEAD # Pipe torrentfilename = torrentfilename.replace( "|", u"ǀ") # U+01C0 LATIN LETTER DENTAL CLICK ### #####----------windows filename banned chars replacement with unicode-----------###### container.append([torrentfilename, internalname, info_hash, torrentid]) currentfile += 1 print currentfile, torrentfilename.encode('ascii', errors='ignore') print "\nReminder: Console output is ascii only, Cannot Print Unicode. (chars omitted)" ##File Output. The Master List file of everything.## # when the loop exits, Sort it, and write it to the file. container.sort() writelistfile = codecs.open( ss.getwpath("outpath3"), 'wb', "utf-8" ) # write-out a text file with one entry per line. main output file (3propernames.txt) for eachline in container: writelistfile.write(eachline[0] + " / " + eachline[2] + "\n") #torrentname / infohash writelistfile.close() print "Completed. Unicode File Written to: ", os.path.basename( ss.getwpath("outpath3"))
def main(): ss = Preferences() newfile = open(os.path.join(ss.get("maindir"), "NEWDAT.dat"), 'wb') namesandhashfile = open( ss.getwpath("outpath3"), 'r', encoding='utf-8').readlines() #("3propernames.txt") beforeafterpath = ss.getwpath( "outpath4" ) #this holds the intermediate changes to happen before actually renaming so you have a chance to edit/change it. (4beforepath-afterpath.txt) #torrentlist = decoder.decode_from_file(ss.get("utresumedat")) #works 10.645s 12315181 function calls #torrentlist = bencode2en.decode_from_file(ss.get("utresumedat")) #works 8.462s 13745202 function calls torrentlist = bencode.decode_from_file( ss.get("utresumedat")) #works 8.057ss 10908143 function calls #These two things interfere with the processing on the next line fileguarduseless = torrentlist.pop(b".fileguard", None) rec = torrentlist.pop(b"rec", None) #Remove this. #(dict. comprehension expects only dicts as the root keys) #create a reverse lookup dict with "Dict comprehension". nice and simple eh? ;-) reverselookup = { base64.b16encode(value[b"info"]): [key, value[b"caption"], value[b"path"]] for key, value in torrentlist.items() } listofbeforeafter = [] #to modify paths in reverse lookup dict, start by getting the names and hash out of the namesandhashfile for eachline in namesandhashfile: nameandhash = eachline.strip().split( ' / ' ) #strip out the \n with strip() and split on the " / " i put there as a seperator. theNewname = nameandhash[0] thehash = nameandhash[1] #searches the dict's keys for a Hash, if exists. and if so, can be used as the [indexid] if bytes(thehash, 'utf-8') in reverselookup: key = reverselookup[bytes(thehash, 'utf-8')][0] theOldPath = torrentlist[key][b"path"].decode('utf-8') theNewPath = os.path.join(os.path.dirname(theOldPath), theNewname) if theOldPath != theNewPath: listofbeforeafter.append( [theOldPath, theNewPath, thehash] ) # make a list of a list (stringtoOutputtoFile=[0], hash=[1]) #sort, then write file detailing changes to path (before / after) listofbeforeafter.sort() beforeafterfile = open(beforeafterpath, 'w', encoding='utf-8') for eachline in listofbeforeafter: beforeafterfile.write( eachline[0] + " / " + eachline[2] + "\n" ) #write oldpath + hash on 1st line /The hash is duplicated for error checking in case the user accidentally bungles a character while editing... beforeafterfile.write(eachline[1] + " / " + eachline[2] + "\n") #write newpath + hash on 2nd line / beforeafterfile.close() #At this point the script pauses, and asks the user to confirm changes shown in the beforepath-afterpath.txt file input("Press Enter to begin Renaming files.......\\> " ) #wait for the user to press Enter before continuing with anything. #WRITE TORRENT RESUME.DAT beforeafterfile = open(beforeafterpath, 'r', encoding='utf-8').readlines() for i in range(0, len(beforeafterfile), 2): beforeandhash = beforeafterfile[i].strip().split(' / ') afterandhash = beforeafterfile[i + 1].strip().split(' / ') before = beforeandhash[0] beforehash = beforeandhash[1] after = afterandhash[0] afterhash = afterandhash[1] if beforehash == afterhash: thehash = beforehash else: print( "Error. You have inadvertently modified one of the hash files, and there is a hash mismatch between before/after entries." ) print( "Cannot continue. Exiting. Please save your changes into a new file, locate your error, and re-run and fix it..." ) print( "Another possibility is you were missing a / (with 1 character of whitespace on each side surrounding it) as a seperator." ) #searches the dict's keys for a Hash, if exists. and if so, can be used as the [indexid] if bytes(thehash, 'utf-8') in reverselookup: key = reverselookup[bytes(thehash, 'utf-8')][0] torrentlist[key][b"caption"] = bytes(after[after.rfind("\\") + 1:], 'utf-8') try: # prints a number to console to show progress. corresponds to the numbers in the file (every-two-lines). (tip:) to show incremental numbers use (((i+1)/2)+1) # filenames printed to console, will be missing any unicode chars because the windows console is not unicode compatible!!!! (annoying) print(i, before.encode('ascii', errors='ignore').decode()) print(i + 1, after.encode('ascii', errors='ignore').decode()) os.rename(before, after) except Exception as e: traceback.print_exc( ) #will output any errors to console but keep going torrentlist[key][b"path"] = bytes(after, 'utf-8') if after.endswith(".mp3") or after.endswith( ".flac" ): #.mp3 .flac = I personally didnt have any "Single file" .ogg, .aac, etc that needed special handling in this manner if b"targets" in torrentlist[ key]: #these lines are a quick fix, for an oversight in the uTorrent process. changing path is not enough torrentlist[key][b"targets"][0][1] = torrentlist[key][ b"caption"] #single-file-mode torrents have a "targets" list that controls the filename torrentlist[ b"rec"] = rec #add the thing we removed back in so we dont break anything (not sure what this is) #fileguard does not need to go back, in fact, purposefully needs to stay out. #newfile.write(encode.encode(torrentlist)) #works 10.295s 15361310 function calls #newfile.write(bencode2en.bencode2(torrentlist)) #v.slow 31.872s 12452142 function calls #newfile.write(bencode2en.bencode4(torrentlist)) #works 7.864s 10906619 function calls newfile.write( bencode.bencode(torrentlist)) #works 7.699s 10906619 function calls newfile.close() print( "\nPlease note that the filenames shown are missing any unicode characters due to Windows Command Prompt limitations." ) print("Finished writing: ", newfile.name)
def main(): ss = Preferences() newfile = open(os.path.join(ss.get("maindir"),"NEWDAT.dat"),'wb') namesandhashfile = open(ss.getwpath("outpath3"),'r',encoding='utf-8').readlines() #("3propernames.txt") beforeafterpath = ss.getwpath("outpath4") #this holds the intermediate changes to happen before actually renaming so you have a chance to edit/change it. (4beforepath-afterpath.txt) #torrentlist = decoder.decode_from_file(ss.get("utresumedat")) #works 10.645s 12315181 function calls #torrentlist = bencode2en.decode_from_file(ss.get("utresumedat")) #works 8.462s 13745202 function calls torrentlist = bencode.decode_from_file(ss.get("utresumedat")) #works 8.057ss 10908143 function calls #These two things interfere with the processing on the next line fileguarduseless = torrentlist.pop(b".fileguard",None) rec = torrentlist.pop(b"rec",None) #Remove this. #(dict. comprehension expects only dicts as the root keys) #create a reverse lookup dict with "Dict comprehension". nice and simple eh? ;-) reverselookup={base64.b16encode(value[b"info"]):[key,value[b"caption"],value[b"path"]] for key,value in torrentlist.items()} listofbeforeafter = [] #to modify paths in reverse lookup dict, start by getting the names and hash out of the namesandhashfile for eachline in namesandhashfile: nameandhash = eachline.strip().split(' / ') #strip out the \n with strip() and split on the " / " i put there as a seperator. theNewname = nameandhash[0] thehash = nameandhash[1] #searches the dict's keys for a Hash, if exists. and if so, can be used as the [indexid] if bytes(thehash,'utf-8') in reverselookup: key = reverselookup[bytes(thehash,'utf-8')][0] theOldPath = torrentlist[key][b"path"].decode('utf-8') theNewPath = os.path.join(os.path.dirname(theOldPath),theNewname) if theOldPath != theNewPath: listofbeforeafter.append([theOldPath,theNewPath,thehash]) # make a list of a list (stringtoOutputtoFile=[0], hash=[1]) #sort, then write file detailing changes to path (before / after) listofbeforeafter.sort() beforeafterfile = open(beforeafterpath,'w',encoding='utf-8') for eachline in listofbeforeafter: beforeafterfile.write(eachline[0] + " / " + eachline[2] + "\n") #write oldpath + hash on 1st line /The hash is duplicated for error checking in case the user accidentally bungles a character while editing... beforeafterfile.write(eachline[1] + " / " + eachline[2] + "\n") #write newpath + hash on 2nd line / beforeafterfile.close() #At this point the script pauses, and asks the user to confirm changes shown in the beforepath-afterpath.txt file input("Press Enter to begin Renaming files.......\\> ") #wait for the user to press Enter before continuing with anything. #WRITE TORRENT RESUME.DAT beforeafterfile = open(beforeafterpath,'r',encoding='utf-8').readlines() for i in range(0, len(beforeafterfile), 2): beforeandhash = beforeafterfile[i].strip().split(' / ') afterandhash = beforeafterfile[i+1].strip().split(' / ') before = beforeandhash[0] beforehash = beforeandhash[1] after = afterandhash[0] afterhash = afterandhash[1] if beforehash == afterhash: thehash = beforehash else: print("Error. You have inadvertently modified one of the hash files, and there is a hash mismatch between before/after entries.") print("Cannot continue. Exiting. Please save your changes into a new file, locate your error, and re-run and fix it...") print("Another possibility is you were missing a / (with 1 character of whitespace on each side surrounding it) as a seperator.") #searches the dict's keys for a Hash, if exists. and if so, can be used as the [indexid] if bytes(thehash,'utf-8') in reverselookup: key = reverselookup[bytes(thehash,'utf-8')][0] torrentlist[key][b"caption"] = bytes(after[after.rfind("\\")+1:],'utf-8') try: # prints a number to console to show progress. corresponds to the numbers in the file (every-two-lines). (tip:) to show incremental numbers use (((i+1)/2)+1) # filenames printed to console, will be missing any unicode chars because the windows console is not unicode compatible!!!! (annoying) print(i,before.encode('ascii', errors='ignore').decode()) print(i+1,after.encode('ascii', errors='ignore').decode()) os.rename(before, after) except Exception as e: traceback.print_exc() #will output any errors to console but keep going torrentlist[key][b"path"] = bytes(after,'utf-8') if after.endswith(".mp3") or after.endswith(".flac"): #.mp3 .flac = I personally didnt have any "Single file" .ogg, .aac, etc that needed special handling in this manner if b"targets" in torrentlist[key]: #these lines are a quick fix, for an oversight in the uTorrent process. changing path is not enough torrentlist[key][b"targets"][0][1] = torrentlist[key][b"caption"] #single-file-mode torrents have a "targets" list that controls the filename torrentlist[b"rec"]=rec #add the thing we removed back in so we dont break anything (not sure what this is) #fileguard does not need to go back, in fact, purposefully needs to stay out. #newfile.write(encode.encode(torrentlist)) #works 10.295s 15361310 function calls #newfile.write(bencode2en.bencode2(torrentlist)) #v.slow 31.872s 12452142 function calls #newfile.write(bencode2en.bencode4(torrentlist)) #works 7.864s 10906619 function calls newfile.write(bencode.bencode(torrentlist)) #works 7.699s 10906619 function calls newfile.close() print("\nPlease note that the filenames shown are missing any unicode characters due to Windows Command Prompt limitations.") print("Finished writing: ", newfile.name)
def main(): ss = Preferences() script1sourcedir = ss.getwpath(u"script1sourcedir")+u'' #("seeding\"), needs unicode u for file opening. files = [os.path.join(script1sourcedir,filename) for filename in next(os.walk(script1sourcedir))[2]] #gives absolute paths + names currentfile = 0 container = [] #set up an empty container for desired data to get put into for later for eachfile in files: metainfo = bencode.decode_from_file(eachfile) # #need to manually SHA1 hash the torrent file's info-dict to get the info-hash infodict = metainfo['info'] info_hash = hashlib.sha1(bencode.bencode(infodict)).hexdigest().upper() internalname = infodict['name'] torrentfilename = eachfile[eachfile.rfind("\\")+1:] locextension = torrentfilename.find(".torrent") #location of extension (char position) locid = torrentfilename.rfind("-")+1 #location of torrentID (char position) torrentid = torrentfilename[locid:locextension] #grab torrentID torrentfilename = torrentfilename[:locid-1] #####-------------replace banned characters with unicode section-----------------###### ### # Forward slashes are strange. "FullWidth" is very wide and would be too wide if theres already spaces around it. torrentfilename = torrentfilename.replace(" / ",u"/") # U+FFOF (wide) FULLWIDTH SOLIDUS # "Division" slash is too narrow and needs spaces inserted surrounding it (and is still less width than the fullwidth) torrentfilename = torrentfilename.replace("/",u" ∕ ") # U+2215 (narrow) DIVISION SLASH # Backslash (requires two slashes in python) torrentfilename = torrentfilename.replace("\\",u"\") # U+FF3C FULLWIDTH REVERSE SOLIDUS # Colon torrentfilename = torrentfilename.replace(":",u"꞉") # U+A789 MODIFIER LETTER COLON # asterisk torrentfilename = torrentfilename.replace("*",u"※") # U+203B REFERENCE MARK # question mark (replacement is backwards, sorry) torrentfilename = torrentfilename.replace("?",u"؟") # U+061F ARABIC QUESTION MARK # Double-quote torrentfilename = torrentfilename.replace('"',u"ʺ") # U+02BA MODIFIER LETTER DOUBLE PRIME # Left angle bracket torrentfilename = torrentfilename.replace("<",u"˂") # U+02C2 MODIFIER LETTER LEFT ARROWHEAD # right angle bracket torrentfilename = torrentfilename.replace(">",u"˃") # U+02C3 MODIFIER LETTER RIGHT ARROWHEAD # Pipe torrentfilename = torrentfilename.replace("|",u"ǀ") # U+01C0 LATIN LETTER DENTAL CLICK ### #####----------windows filename banned chars replacement with unicode-----------###### container.append([torrentfilename, internalname, info_hash, torrentid]) currentfile += 1 print currentfile, torrentfilename.encode('ascii', errors='ignore') print "\nReminder: Console output is ascii only, Cannot Print Unicode. (chars omitted)" ##File Output. The Master List file of everything.## # when the loop exits, Sort it, and write it to the file. container.sort() writelistfile = codecs.open(ss.getwpath("outpath3"), 'wb', "utf-8") # write-out a text file with one entry per line. main output file (3propernames.txt) for eachline in container: writelistfile.write(eachline[0] + " / " + eachline[2] + "\n") #torrentname / infohash writelistfile.close() print "Completed. Unicode File Written to: ", os.path.basename(ss.getwpath("outpath3"))