class DeliciousV1(object):

    """Simple API frontend"""

    posturl = 'https://api.del.icio.us/v1/posts/add'
    title = re.compile(r'<title>(.*?)</title>', re.I+re.DOTALL)

    def __init__(self, username, password, logger=None):
        password_mgr = HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, self.posturl, username, password)
        auth_handler = HTTPBasicAuthHandler(password_mgr)
        self.ua = UserAgent(handlers=[auth_handler])
        self.log = logger

    def get_title(self, url):
        try:
            html = self.ua.open(url, size=2048)
            title = strip_html(self.title.search(html).group(1))
        except:
            title = url
        return title

    def post(self, url, tags=None):
        self.log.info('Posting to delicious: %s', url)
        parameters = {'url': url, 'description': self.get_title(url), 'replace': u'no', 'shared': u'yes'}
        if tags:
            parameters['tags'] = u' '.join(tags)
        for key, val in parameters.iteritems():
            parameters[key] = val.encode('utf-8')
        self.process(parameters)

    def process(self, parameters):
        self.ua.open(self.posturl, opts=parameters)
示例#2
0
class DeliciousV1(object):
    """Simple API frontend"""

    posturl = 'https://api.del.icio.us/v1/posts/add'
    title = re.compile(r'<title>(.*?)</title>', re.I + re.DOTALL)

    def __init__(self, username, password, logger=None):
        password_mgr = HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, self.posturl, username, password)
        auth_handler = HTTPBasicAuthHandler(password_mgr)
        self.ua = UserAgent(handlers=[auth_handler])
        self.log = logger

    def get_title(self, url):
        try:
            html = self.ua.open(url, size=2048)
            title = strip_html(self.title.search(html).group(1))
        except:
            title = url
        return title

    def post(self, url, tags=None):
        self.log.info('Posting to delicious: %s', url)
        parameters = {
            'url': url,
            'description': self.get_title(url),
            'replace': u'no',
            'shared': u'yes'
        }
        if tags:
            parameters['tags'] = u' '.join(tags)
        for key, val in parameters.iteritems():
            parameters[key] = encode(val, 'utf-8')
        self.process(parameters)

    def process(self, parameters):
        self.ua.open(self.posturl, opts=parameters)
示例#3
0
 def __init__(self,
              consumer_key,
              consumer_secret,
              token_key,
              token_secret,
              session_handle,
              logger=None):
     import oauth2 as oauth
     self.method = oauth.SignatureMethod_HMAC_SHA1()
     self.token = oauth.Token(key=token_key, secret=token_secret)
     self.consumer = oauth.Consumer(key=consumer_key,
                                    secret=consumer_secret)
     self.session_handle = session_handle
     self.last_refresh = 0
     self.valid_for = 0
     self.ua = UserAgent()
     self.log = logger
示例#4
0
文件: google.py 项目: Havvy/madcow
 def __init__(self):
     self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent=AGENT, debug=False)
示例#5
0
文件: google.py 项目: Havvy/madcow
class Google(object):

    baseurl = u'http://www.google.com/'
    search = urljoin(baseurl, u'/search')
    luckyopts = {u'hl': u'en', u'btnI': u'I', u'aq': u'f', u'safe': u'off'}
    calcopts = {u'hl': u'en', u'safe': u'off', u'c2coff': 1, u'btnG': u'Search'}
    reConversionDetected = re.compile(u'More about (calculator|currency)')
    reConversionResult = re.compile(u'<h2 class=r.*?>.*?<b>(.*?)<\/b><\/h2>')
    sup_re = re.compile(r'(<sup>.*?</sup>)', re.I | re.DOTALL)
    clock_re = re.compile(r'/images/icons/onebox/clock')
    sun_re = re.compile(r'/images/icons/onebox/weather_sun')
    whitespace_re = re.compile(r'\s{2,}')

    def __init__(self):
        self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent=AGENT, debug=False)

    def lucky(self, query):
        """Return I'm Feeling Lucky URL for given query"""
        opts = dict(self.luckyopts.items())
        opts[u'q'] = query
        result = self.ua.open(self.search, opts=opts, referer=self.baseurl,
                              size=1024)
        if not result.startswith(u'http'):
            raise NonRedirectResponse
        return result

    calc_re = re.compile(r'calculator')
    white_re = re.compile(r'\s+')

    def calculator(self, query):
        """Try to use google calculator for given query"""
        opts = dict(self.calcopts)
        opts[u'q'] = query
        doc = self.ua.open(self.search, opts=opts)
        soup = BeautifulSoup(doc)
        values = []
        conv_left = soup.find('input', id='ucw_lhs_d')
        conv_right = soup.find('input', id='ucw_rhs_d')
        if not (conv_left is None or conv_right is None):
            left_value = conv_left['value'].strip()
            left_unit = conv_left.findNext('option').renderContents().strip()
            right_value = conv_right['value'].strip()
            right_unit = conv_right.findNext('option').renderContents().strip()
            values.append('(%s) %s = (%s) %s' % (left_unit, left_value, right_unit, right_value))
        calculation = soup.find('span', 'cwcot')
        if calculation is not None:
            values.append(calculation.renderContents())
        try:
            values.append(soup.find('h3', 'r').b.renderContents())
        except StandardError:
            pass
        #ipython()
        result = u', '.join(filter(None, (decode(strip_html(value)).strip() for value in values)))
        if result:
            return result

    def sunrise_sunset(self, query, location):
        """Ask google for the sunrise or sunset from location"""
        soup = BeautifulSoup(self.ua.open(self.search, {'q': '%s in %s' % (query, location)}))
        result = strip_html(soup.find('li', 'knavi').renderContents())
        return self.whitespace_re.sub(u' ', result.strip())

    def clock(self, query):
        """Use google to look up time in a given location"""
        doc = self.ua.open(self.search, {'q': 'time in %s' % query})
        soup = BeautifulSoup(doc)
        table = soup.find('li', attrs={'class': re.compile('obcontainer')})
        [subtable.extract() for subtable in table.findAll('table')]
        return re.sub(r'\s{2,}', ' ', strip_html(self.decode(table).strip())).strip()

    @staticmethod
    def decode(node):
        return decode(node.renderContents())
 def __init__(self, username, password, logger=None):
     password_mgr = HTTPPasswordMgrWithDefaultRealm()
     password_mgr.add_password(None, self.posturl, username, password)
     auth_handler = HTTPBasicAuthHandler(password_mgr)
     self.ua = UserAgent(handlers=[auth_handler])
     self.log = logger
示例#7
0
 def __init__(self, username, password, logger=None):
     password_mgr = HTTPPasswordMgrWithDefaultRealm()
     password_mgr.add_password(None, self.posturl, username, password)
     auth_handler = HTTPBasicAuthHandler(password_mgr)
     self.ua = UserAgent(handlers=[auth_handler])
     self.log = logger
示例#8
0
 def __init__(self):
     self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1")
示例#9
0
class Google(object):

    baseurl = u'http://www.google.com/'
    search = urljoin(baseurl, u'/search')
    luckyopts = {u'hl': u'en', u'btnI': u'I', u'aq': u'f', u'safe': u'off'}
    calcopts = {u'hl': u'en', u'safe': u'off', u'c2coff': 1, u'btnG': u'Search'}
    reConversionDetected = re.compile(u'More about (calculator|currency)')
    reConversionResult = re.compile(u'<h2 class=r.*?>.*?<b>(.*?)<\/b><\/h2>')
    sup_re = re.compile(r'(<sup>.*?</sup>)', re.I | re.DOTALL)
    clock_re = re.compile(r'/images/icons/onebox/clock')
    sun_re = re.compile(r'/images/icons/onebox/weather_sun')
    whitespace_re = re.compile(r'\s{2,}')

    def __init__(self):
        self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1")

    def lucky(self, query):
        """Return I'm Feeling Lucky URL for given query"""
        opts = dict(self.luckyopts.items())
        opts[u'q'] = query
        result = self.ua.open(self.search, opts=opts, referer=self.baseurl,
                              size=1024)
        if not result.startswith(u'http'):
            raise NonRedirectResponse
        return result

    calc_re = re.compile(r'calculator')
    white_re = re.compile(r'\s+')

    def calculator(self, query):
        """Try to use google calculator for given query"""
        opts = dict(self.calcopts)
        opts[u'q'] = query
        doc = self.ua.open(self.search, opts=opts)
        soup = BeautifulSoup(doc)
        response = soup.find('img', src=self.calc_re).parent.findNext('h2').renderContents()
        response = ' '.join(response.splitlines())
        response = response.decode('utf-8', 'ignore')

        # turn super scripts into utf8
        parts = []
        for part in self.sup_re.split(response):
            if self.sup_re.match(part):
                part = superscript(part)
            parts.append(part)
        response = u''.join(parts)
        response = self.white_re.sub(' ', strip_html(response).strip())
        return response

    def sunrise_sunset(self, query, location):
        """Ask google for the sunrise or sunset from location"""
        soup = BeautifulSoup(self.ua.open(self.search, {'q': '%s in %s' % (query, location)}))
        image = soup.find('img', src=self.sun_re)
        row1 = image.findNext('td')
        row2 = row1.findNext('td')
        result = strip_html(u'%s (%s)' % (self.decode(row1), self.decode(row2)))
        return self.whitespace_re.sub(u' ', result.strip())

    def clock(self, query):
        """Use google to look up time in a given location"""
        try:
            doc = self.ua.open(self.search, {'q': 'time in %s' % query})
            soup = BeautifulSoup(doc)
            table = soup.find('table', 'obcontainer')
            time = table.find('td', style='font-size:medium')
            return strip_html(time.renderContents().decode('utf-8')).strip()
        except:
            raise

    @staticmethod
    def decode(node):
        return node.renderContents().decode('utf-8', 'ignore')
示例#10
0
 def __init__(self):
     self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent=AGENT)
示例#11
0
class Google(object):

    baseurl = u'http://www.google.com/'
    search = urljoin(baseurl, u'/search')
    luckyopts = {u'hl': u'en', u'btnI': u'I', u'aq': u'f', u'safe': u'off'}
    calcopts = {
        u'hl': u'en',
        u'safe': u'off',
        u'c2coff': 1,
        u'btnG': u'Search'
    }
    reConversionDetected = re.compile(u'More about (calculator|currency)')
    reConversionResult = re.compile(u'<h2 class=r.*?>.*?<b>(.*?)<\/b><\/h2>')
    sup_re = re.compile(r'(<sup>.*?</sup>)', re.I | re.DOTALL)
    clock_re = re.compile(r'/images/icons/onebox/clock')
    sun_re = re.compile(r'/images/icons/onebox/weather_sun')
    whitespace_re = re.compile(r'\s{2,}')

    def __init__(self):
        self.ua = UserAgent(handlers=[NoRedirects, NoErrors], agent=AGENT)

    def lucky(self, query):
        """Return I'm Feeling Lucky URL for given query"""
        opts = dict(self.luckyopts.items())
        opts[u'q'] = query
        result = self.ua.open(self.search,
                              opts=opts,
                              referer=self.baseurl,
                              size=1024)
        if not result.startswith(u'http'):
            raise NonRedirectResponse
        return result

    calc_re = re.compile(r'calculator')
    white_re = re.compile(r'\s+')

    def calculator(self, query):
        """Try to use google calculator for given query"""
        opts = dict(self.calcopts)
        opts[u'q'] = query
        doc = self.ua.open(self.search, opts=opts)
        soup = BeautifulSoup(doc)
        values = []
        conv_left = soup.find('input', id='ucw_lhs_d')
        conv_right = soup.find('input', id='ucw_rhs_d')
        if not (conv_left is None or conv_right is None):
            left_value = conv_left['value'].strip()
            left_unit = conv_left.findNext('option').renderContents().strip()
            right_value = conv_right['value'].strip()
            right_unit = conv_right.findNext('option').renderContents().strip()
            values.append('(%s) %s = (%s) %s' %
                          (left_unit, left_value, right_unit, right_value))
        calculation = soup.find('span', 'cwcot')
        if calculation is not None:
            values.append(calculation.renderContents())
        result = u', '.join(
            filter(None,
                   (decode(strip_html(value)).strip() for value in values)))
        if result:
            return result

    def sunrise_sunset(self, query, location):
        """Ask google for the sunrise or sunset from location"""
        soup = BeautifulSoup(
            self.ua.open(self.search, {'q': '%s in %s' % (query, location)}))
        image = soup.find('img', src=self.sun_re)
        row1 = image.findNext('td')
        row2 = row1.findNext('td')
        result = strip_html(u'%s (%s)' %
                            (self.decode(row1), self.decode(row2)))
        return self.whitespace_re.sub(u' ', result.strip())

    def clock(self, query):
        """Use google to look up time in a given location"""
        try:
            doc = self.ua.open(self.search, {'q': 'time in %s' % query})
            soup = BeautifulSoup(doc)
            table = soup.find('div', 'obcontainer')
            time = table.find('td', style='font-size:medium')
            return strip_html(self.decode(time).strip())
        except:
            raise

    @staticmethod
    def decode(node):
        return decode(node.renderContents())