#!/usr/bin/python # Download subtitles from tvsubtitles.net (nautilus version) # # Carlos Acedo (carlos@linux-labs.net) # License GPL v2 showNum = { "24":38, "30 rock":46, "90210":244, "afterlife":200, "alias":5, "aliens in america":119, "ally mcbeal":158, "american dad":138, "andromeda":60, "andy barker: p.i.":49, "angel":98, "army wives":242, "arrested development":161, "ashes to ashes":151, "avatar: the last airbender":125, "back to you":183, "band of brothers":143, "battlestar galactica":42, "big day":237, "big love":88, "big shots":137, "bionic woman":113, "black adder":176, "black books":175, "blade":177, "blood ties":140, "bonekickers":227, "bones":59, "boston legal":77, "breaking bad":133, "brotherhood":210, "brothers & sisters":66, "buffy the vampire slayer":99, "burn notice":50, "californication":103, "carnivale":170, "carpoolers":146, "cashmere mafia":129, "charmed":87, "chuck":111, "city of vice":257, "cold case":95, "criminal minds":106, "csi":27, "csi miami":51, "csi ny":52, "curb your enthusiasm":69, "damages":124, "dark angel":131, "day break":6, "dead like me":13, "deadwood":48, "desperate housewives":29, "dexter":55, "dirt":145, "dirty sexy money":118, "do not disturb":252, "doctor who":141, "drive":97, "eli stone":149, "entourage":25, "er (e.r.)":39, "eureka":43, "everybody hates chris":81, "everybody loves raymond":86, "exes & ohs":199, "extras":142, "fallen":101, "family guy":62, "farscape":92, "fawlty towers":178, "fear itself":201, "felicity":217, "firefly":84, "flash gordon":134, "flashpoint":221, "friday night lights":57, "friends":65, "fringe":204, "futurama":126, "generation kill":223, "ghost whisperer":14, "gilmore girls":28, "gossip girl":114, "greek":102, "grey's anatomy":7, "heroes":8, "hidden palms":44, "hotel babylon":164, "house m.d.":9, "how i met your mother":110, "hustle":160, "in justice":144, "in plain sight":198, "in treatment":139, "into the west":256, "invasion":184, "it's always sunny in philadelphia":243, "jeeves and wooster":180, "jekyll":61, "jericho":37, "joey":83, "john adams":155, "john from cincinnati":79, "journeyman":108, "k-ville":107, "keeping up appearances":167, "knight rider":163, "kyle xy":10, "lab rats":233, "las vegas":75, "life":109, "life is wild":120, "life on mars (uk)":90, "lipstick jungle":150, "lost":3, "lost in austen":254, "lucky louie":238, "mad men":136, "meadowlands":45, "medium":12, "melrose place":189, "men in trees":127, "miami vice":208, "monk":85, "moonlight":117, "my name is earl":15, "ncis":30, "new amsterdam":153, "nip/tuck":23, "northern exposure":241, "numb3rs":11, "october road":132, "one tree hill":16, "over there":93, "oz":36, "painkiller jane":35, "pepper dennis":82, "police squad":190, "popetown":179, "pretender":245, "primeval":130, "prison break":2, "private practice":115, "privileged":248, "project runway":226, "psych":17, "pushing daisies":116, "queer as folk":229, "reaper":112, "regenesis":152, "rescue me":91, "robin hood":121, "rome":63, "roswell":159, "samantha who?":123, "samurai girl":255, "saving grace":104, "scrubs":26, "secret diary of a call girl":196, "seinfeld":89, "sex and the city":68, "shameless":193, "shark":24, "sharpe":186, "six feet under":94, "skins":147, "smallville":1, "sophie":203, "south park":71, "spooks":148, "standoff":70, "stargate atlantis":54, "stargate sg-1":53, "studio 60 on the sunset strip":33, "supernatural":19, "swingtown":202, "taken":67, "tell me you love me":182, "terminator: the sarah connor chronicles":128, "the 4400":20, "the andromeda strain":181, "the big bang theory":154, "the black donnellys":216, "the cleaner":225, "the closer":78, "the dead zone":31, "the dresden files":64, "the fixer":213, "the inbetweeners":197, "the it crowd":185, "the l word":74, "the middleman":222, "the net":174, "the no. 1 ladies' detective agency":162, "the o.c. (the oc)":21, "the office":58, "the outer limits":211, "the riches":156, "the secret life of the american teenager":218, "the shield":40, "the simple life":234, "the simpsons":32, "the sopranos":18, "the tudors":76, "the unit":47, "the war at home":80, "the west wing":168, "the wire":72, "the x-files":100, "threshold":96, "til death":171, "tin man":122, "top gear":232, "torchwood":135, "traveler":41, "tripping the rift":188, "tru calling":4, "true blood":205, "twin peaks":169, "two and a half men":56, "ugly betty":34, "ultimate force":194, "unhitched":157, "veronica mars":22, "weeds":73, "will & grace":172, "without a trace":105, "women's murder club":166, "wonderfalls":165 } import urllib import os import re import sgmllib import sys class MyParser(sgmllib.SGMLParser): "A simple parser class." def parse(self, s): "Parse the given string 's'." self.feed(s) self.close() def __init__(self, verbose=0): "Initialise an object, passing 'verbose' to the superclass." sgmllib.SGMLParser.__init__(self, verbose) self.hyperlinks = [] self.descriptions = [] self.inside_a_element = 0 self.starting_description = 0 def start_a(self, attributes): "Process a hyperlink and its 'attributes'." for name, value in attributes: if name == "href": if value.find("subtitle-")!= -1: self.hyperlinks.append(value) self.inside_a_element = 1 self.starting_description = 1 def end_a(self): "Record the end of a hyperlink." self.inside_a_element = 0 def handle_data(self, data): "Handle the textual 'data'." if self.inside_a_element: if self.starting_description: if data.find("subtitle") != -1: self.descriptions.append(data) self.starting_description = 0 def get_hyperlinks(self): "Return the list of hyperlinks." return self.hyperlinks def get_descriptions(self): "Return a list of descriptions." return self.descriptions # ==================== Main ===================================================== #origFileName = sys.argv[1] origFileName = os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].strip('\n') fileName = os.path.basename(origFileName.lower().replace("."," ")) fileName = os.path.basename(fileName.replace("_"," ")) # Format S01E01 s = re.search("[sS](\d\d)",fileName) e = re.search("[eE](\d\d)",fileName) if (s == None): # Format 1x01 s = re.search("(\d)x(\d\d)",fileName) if (s == None): os.system('zenity --error --text="Unable to find season"') else: season = s.group(1) episode = s.group(2) else: season = s.group(1) if (e == None): os.system('zenity --error --text="Unable to find episode"') else: episode = e.group(1) if (season[0]=="0"): season = season[1] ns = s.start() show = fileName[:ns-1] print 'Show: "' + show+'"' print 'Season: ' + season print 'Episode: ' + episode if (not showNum.has_key(show)): os.system('zenity --error --text="Show not found:' + show + '"') else: url = "http://www.tvsubtitles.net/" page = url+ "tvshow-" + str(showNum[show]) + "-" + season + ".html" sock = urllib.urlopen(page) htmlSource = sock.read() sock.close() #print htmlSource pos = htmlSource.find(season+"x"+episode) if (pos != -1): #print "Pos: " + str(pos) htmlSource = htmlSource[pos:] #print htmlSource pos = htmlSource.find("href=\"episode") # ENTCONTRAR EL ENLACE EPISODE-TAL.HTML Y UNA VEZ DENTRO HACER SELECCIONAR UN IDIOMA-RELEASE if (pos != -1): htmlSource = htmlSource[pos:] #print htmlSource languagePage = htmlSource[6:htmlSource.find("\">")] print languagePage sock = urllib.urlopen(url+languagePage) htmlSource = sock.read() sock.close() myparser = MyParser() myparser.parse(htmlSource) names = myparser.get_descriptions() links = myparser.get_hyperlinks() i=0 for item in names: names[i].replace("\n","") i=i+1 print names dialog_items = '' for item in names: dialog_items = dialog_items + '"' + item + '" ' resp = os.popen('zenity --width=400 --height=200 --list --text="Select subtitle from tvsubtitles.net:" --column="Subtitles" ' + dialog_items).readline() if resp != '': resp = resp[:-1] #resp.strip('\n') i=0 j=0 for name in names: if name == resp: j=i i=i+1 print links[j] #pos = htmlSource.find("\">") link = url + "download-" + links[j][10:] subName = origFileName[:-3] + "srt" response = os.system("wget -O - \"" + link + "\" |zcat > " + subName ) if response != 0: os.system('zenity --error --text="An error ocurred downloading or writing the subtitle"') else: os.system('zenity --info --text="Subtitle downloaded successfuly"') else: os.system('zenity --error --text="Season or episode not found"')