# -*- coding: utf-8 -*- # -*- Python -*- import os from cgi import parse_qs def render_form(message=''): webform = """
Serie Season code Language
""" return webform def results_form(serie='', season_code=1, language='es'): parser = MUParser(serie, season_code) res = """
    %s
    
""" % (serie, season_code, language, parser) return res def application(environ, start_response): """ Main wsgi application """ method = environ['REQUEST_METHOD'] if method == 'GET': # show the form start_response('200 OK', [('content-type', 'text/html')]) return render_form() if method == 'POST': # process the form req_size = int(environ['CONTENT_LENGTH']) params = parse_qs(environ['wsgi.input'].read(req_size)) serie = params.get("serie",[""])[0] season_code = params.get("season_code",[""])[0] language = params.get("language",[""])[0] start_response('200 OK', [('content-type', 'text/html')]) return results_form(serie, season_code, language) class MUParser(object): """ Parser to get megaupload links from a website. You can use it just creating an instance of it this way: >>> from muparser import MUParser >>> parser = MUParser('http://como-conoci-a-vuestra-madre.seriespepito.com/') >>> parser.run() Once run is finished, you can access the links through the parser.links dictionary """ def __init__(self, url=None, season=None): """ self.season_code: string code used to find the proper links within the site self.megaupload: string code used to identify megaupload links self.links: dictionary containing each episode identifier as a key, a list of megaupload links as the value """ if not url: raise Exception('You have to provide me with an URL') self.url = url self.season_code = 'temporada-' if season: try: int(season) except ValueError: print 'Season must be an integer value, assuming all seasons' else: self.season_code += season self.megaupload = 'megaupload.com' self.links = {} def get_mu_links(self, url): """ Get all the megaupload links from the specified url """ print ' --> Getting megaupload.com links' page = parse(url).getroot() title = page.find_class('subtitle')[0].text.strip() self.links[title] = [] for link in page.iterlinks(): if self.megaupload in link[2]: print ' ---> Adding link:', link[2] self.links[title].append(link[2]) def run(self): """ Start parsing self.url """ print '[MUParser - init]', self.url page = parse(self.url).getroot() for link in page.iterlinks(): if self.url in link[2]: if self.season_code in link[2]: print ' -> Found link to episode:', link[2] self.get_mu_links(link[2])