Detail: muparser-web (python)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
# -*- Python -*-

import os
from cgi import parse_qs
  
def render_form(message=''):

    webform = """
    <form action="" method="post">
        Serie <input type="text" name="serie" />
        Season code <input type="text" name="season_code" />
        Language <input type="text" name="language" />
        <input type="submit" value="submit" />
    </form>
    """
    
    return webform



def results_form(serie='', season_code=1, language='es'):

    parser = MUParser(serie, season_code)
    res = """
    <ul>
        <li>Serie: %s</li>
        <li>Season code: %s</li>
        <li>Language: %s</li>
    </ul>
    <pre>
    %s
    </pre>
    """ % (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])

[raw] - Pasted by: r0sk on python on Sept. 29, 2011