Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: latin-1 -*-
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- from xml.dom.minidom import parse, Document
- import optparse
- import sys
- import os
- import re
- import glob
- import unittest
- # for running the script directly from command line
- sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'..'))
- try:
- from rescene.rescene import merge_srrs
- except:
- from rescene import merge_srrs
- def main(options, args):
- relFileMapping = {}
- fileRelMapping = {}
- unknown = []
- def read_nzb(nzb):
- doc = parse(nzb, bufsize=1000)
- for file_node in doc.getElementsByTagName("file"):
- subject = file_node.getAttribute("subject")
- relname, srr = parseSubject(subject)
- if srr == None:
- unknown.append(subject)
- if options.unknowns:
- print(subject)
- else:
- relname = relname.strip()
- l = list()
- l.append(srr)
- try:
- relFileMapping[relname] += [srr]
- except:
- relFileMapping[relname] = [srr]
- fileRelMapping[srr] = relname
- # show info while parsing
- if options.releases:
- print(relname)
- if options.srrs:
- print(srr)
- # create new nzb files to download samples
- if options.separate and relname[0:4] != "Con.":
- # Con.Artist, Con.Air folders are not possible in Windows
- newdoc = Document()
- top_element = doc.createElementNS(
- "http://www.newzbin.com/DTD/2003/nzb",
- "nzb")
- newdoc.appendChild(top_element)
- top_element.appendChild(file_node)
- try: # when the "release name" has a \
- f = os.path.join(options.separate, relname + ".nzb")
- print("Writing nzb %s" % os.path.basename(f))
- with open(f, "wb") as nzb_file:
- nzb_file.write(newdoc.toxml("utf-8"))
- except Exception, e:
- print(e)
- for nzb in args:
- try:
- print("Reading %s" % os.path.basename(nzb))
- read_nzb(nzb)
- except:
- # resolve wild cards
- for path in glob.glob(nzb):
- print("Reading %s" % os.path.basename(nzb))
- read_nzb(path)
- if options.rename_dir:
- failed = []
- # try to rename all available files in the directory
- # for single SRR files
- for file in os.listdir(options.rename_dir):
- if fileRelMapping.has_key(file):
- bad = renameSrr(options.rename_dir, file, fileRelMapping[file])
- if bad:
- failed.append(bad)
- else:
- print("File '%s' not in NZB." % file)
- printList(failed)
- if options.join_dir:
- # join srr files from a multiple cd release before renaming
- failed = []
- failed_join = []
- haveSubs = []
- # rel -> srr: "dupe" files (e.g. a repost)
- # srr -> rel: needs to be joined
- for release in relFileMapping.keys():
- # remove duplicates and sort
- files = sorted(set(relFileMapping[release]))
- # a file with the name 'subs' or 'extras' in it? put it at the end
- for file in files:
- if re.match(".*(subs|extras|proof|sample).*", file, re.I):
- files.remove(file)
- files.append(file)
- haveSubs.append(release)
- relFileMapping[release] = files
- if len(files) > 1: # join SRR files to renamed one
- if options.joins:
- print(release)
- for srr in files:
- print("\t%s" % srr)
- else:
- bad = joinSrr(options.join_dir, release, files)
- if bad:
- failed_join.append(bad)
- else: # rename SRR file
- bad = renameSrr(options.join_dir, file, release)
- if bad:
- failed.append(bad)
- print("Failed files: ")
- printList(failed)
- print("Have subs SRR files: ")
- printList(set(haveSubs))
- print("Failed to join files: ")
- printList(failed_join)
- # list all SRR files under the release name
- if options.both:
- for release in relFileMapping.keys():
- print(release)
- for srr in relFileMapping[release]:
- print("\t%s" % srr)
- if options.list_dir:
- # lists all files in the given directory without their extension
- # so it can be used for the list search on srrdb.com
- for file in os.listdir(options.list_dir):
- if os.path.isfile(os.path.join(options.list_dir, file)):
- print(file[:-4])
- def renameSrr(dir, file, releaseName):
- old = os.path.join(dir, file)
- new = os.path.join(dir, "renamed", releaseName + ".srr")
- print(("Renaming %s to %s..." % (old, new))),
- try:
- os.renames(old, new)
- print("done!")
- except:
- print("failed!")
- # move unrenamed files to a separate dir
- new = os.path.join(dir, "unrenamed", file)
- print("Renaming %s to %s..." % (old, new))
- try:
- os.renames(old, new)
- except: pass
- return file
- def joinSrr(dir, release, files):
- dir = os.path.abspath(dir)
- try:
- os.makedirs(os.path.join(dir, "joined"))
- except: pass # Path already exists
- try:
- merge_srrs([os.path.join(dir, f) for f in files],
- os.path.join(dir, "joined", release + ".srr"),
- "pyReScene Merge Script")
- # move original unjoined files
- for f in files:
- os.renames(os.path.join(dir, f),
- os.path.join(dir, "joined-orig", f))
- except:
- # one of the files was not found
- return files
- def printList(list):
- for item in list:
- print("\t%s" % item)
- def parseSubject(subject): #[#altbin@EFNet]-[FULL]-[RELNAM
- exts = "\.(srr|srs|avi|mkv|par2)"
- patternEfnet = (".*\[.*EFNet\]-(\[(FULL|PART)\]-)?"
- "\[\s?(?P<release>[^\s\[\]]+?)(\s.*)?\]-?"
- ".*("|\")(?P<file>.*" + exts + ")"
- "("|\").*")
- patternXvid = ("#alt.binaries.movies.xvid: (?P<release>[^\s]+"
- ") - ("|\")(?P<file>.*" + exts + ")"
- "("|\").*")
- m = re.match(patternEfnet, subject, re.IGNORECASE)
- if m:
- return m.group("release", "file")
- m = re.match(patternXvid, subject, re.IGNORECASE)
- if m:
- return m.group("release", "file")
- else:
- return (None, None)
- class TestParse(unittest.TestCase):
- def test_parse(self):
- teevee = ("""[71733]-[FULL]-[#a.b.teevee@EFNet]-"""
- "[ RELNAME ]-[23/29] - ""
- """FILENAME.srr" yEnc (1/1)""")
- teevee2 = ("[42377]-[FULL]-[#a.b.teevee@EFNet]-[ RELNAME ]-[02/29] -"
- """ "FILENAME.mkv" yEnc (1/100)""")
- moovee = ("[1014]-[FULL]-[#a.b.moovee@EFNet]-[ RELNAME"
- """ ]- "FILENAME.srr" (1/1)""")
- moovee2 = ("[1060]-[FULL]-[#a.b.moovee@EFNet]-[ RELNAME"
- """ ] "FILENAME.avi" (39/39)""")
- hdtv = ("[7895]-[a.b.hdtv.x264@EFNet]-RELNAME- "
- ""FILENAME.mkv" (144/144)")
- moviesdivx = ("[26750]-[#altbin@EFNet]-[FULL]-[RELNAME"
- "]- "FILENAME.avi"")
- moviesdivx2 = ("[26750]-[#altbin@EFNet]-[FULL]-[RELNAME - Sample"
- "]- "FILENAME.avi"")
- moviesxvid = ("#alt.binaries.movies.xvid: RELNAME - ""
- "FILENAME.srr" (1/1))")
- self.assertEqual(parseSubject(teevee), ("RELNAME", "FILENAME.srr"))
- self.assertEqual(parseSubject(teevee2), ("RELNAME", "FILENAME.mkv"))
- self.assertEqual(parseSubject(moovee), ("RELNAME", "FILENAME.srr"))
- self.assertEqual(parseSubject(moovee2), ("RELNAME", "FILENAME.avi"))
- self.assertEqual(parseSubject(moviesdivx), ("RELNAME", "FILENAME.avi"))
- self.assertEqual(parseSubject(moviesdivx2), ("RELNAME", "FILENAME.avi"))
- self.assertEqual(parseSubject(moviesxvid), ("RELNAME", "FILENAME.srr"))
- if __name__ == '__main__':
- parser = optparse.OptionParser(
- usage="Usage: %prog [nzb files] [options]'\n"
- "This tool will list the scene names and the srr name.\n",
- version="%prog 0.2 (2011-10-19)") # --help, --version
- parser.add_option("-r", "--releases", help="prints releases",
- action="store_true", default=False, dest="releases")
- parser.add_option("-s", "--srrs", help="prints SRRs",
- action="store_true", default=False, dest="srrs")
- parser.add_option("-b", "--both", help="prints both releases and the SRRs",
- action="store_true", default=False, dest="both")
- parser.add_option("-j", "--joins", help="prints SRRs to be joined"
- "(no actual joining will occur)",
- action="store_true", default=False, dest="joins")
- parser.add_option("-u", "--unknowns", help="prints unparseable subjects",
- action="store_true", default=False, dest="unknowns")
- parser.add_option("--rename", help="renames SRR files in DIRECTORY "
- "(stop using this one)",
- dest="rename_dir", metavar="DIRECTORY")
- parser.add_option("--join",
- help="joins before renaming SRR files in DIRECTORY",
- dest="join_dir", metavar="DIRECTORY")
- parser.add_option("--list", help="list release names of SRR files",
- dest="list_dir", metavar="DIRECTORY")
- parser.add_option("--separate", dest="separate", metavar="DIRECTORY",
- help="split NZB to [release name].nzb in DIRECTORY")
- parser.add_option("--unittest", help="runs the unit tests", dest="test",
- action="store_true", default=False)
- # no arguments given
- if len(sys.argv) < 2:
- print(parser.format_help())
- else:
- (options, args) = parser.parse_args()
- if options.test:
- suite = unittest.TestLoader().loadTestsFromTestCase(TestParse)
- unittest.TextTestRunner(verbosity=2).run(suite)
- else:
- main(options, args)
- """
- Shows which lines in new.txt aren't in mine.txt:
- cat mine.txt new.txt | sort | uniq -d | cat new.txt - | sort | uniq -u
- change -u at the end to -d to get the duplicates
- grep 'does not exist' all.txt > does_not_exist.txt
- 19:38 < sha0lin> they have this regex on hdbits, maybe you will get more groups with it
- 19:38 < sha0lin> \b(/(C/)Z|AE|AJ8|AJP|Arucard|AW|BBW|BG|BoK|CRiSC|Crow|CtrlHD|D4|DiGG|DiR|DiRTY|disc|DBO|DON|DoNOLi|D/-Z0N3|EbP|ESiR|ETH|fLAMEhd|FPG|FSK|Ft4U|fty|Funner|GMoRK|GoLDSToNE|H2|h264iRMU|HDB|HDC|HDBiRD|HDL|HDxT|H/@M|hymen|HZ|iLL|IMDTHS|iNFLiKTED|iOZO|J4F|JAVLiU|JCH|k2|KTN|KweeK|lulz|M794|MAGiC|MCR|MdM|MMI|Mojo|NaRB|NiX|NWO|OAS|ONYX|PerfectionHD|PHiN|PiNG|Prestige|Prime|PXE|QDP|QXE|Redµx|REPTiLE|RuDE|S26|sJR|SK|SLO|SPeSHaL|SrS|Thora|tK|TM|toho|
- iconv < file.nfo -f cp437 -t utf8 > file.utf8
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement