示例#1
0
def get_tags(url):
    """
    queries the public API of delicious.com and retrieves a dictionary of all
    tags that have been used for the url, with the number of users that have
    used each tag
    """

    split = urlparse.urlsplit(url)
    if split.path == '':
        split = urlparse.SplitResult(split.scheme, split.netloc, '/', split.query, split.fragment)
    url = split.geturl()

    m = hashlib.md5()
    m.update(url.encode('ascii'))

    url_md5 = m.hexdigest()
    req = 'http://feeds.delicious.com/v2/json/urlinfo/%s' % url_md5

    resp = urllib.urlopen(req).read()
    try:
        resp_obj = json.loads(resp)
    except ValueError:
        return {}

    tags = {}
    for o in resp_obj:
        if (not 'top_tags' in o) or (not o['top_tags']):
            return {}
        for tag, count in o['top_tags'].iteritems():
            tags[tag] = count


    return tags
示例#2
0
文件: utils.py 项目: fk-lx/mygpo
def parse_request_body(request):
    """ returns the parsed request body, handles gzip encoding """

    raw_body = request.body
    content_enc = request.META.get('HTTP_CONTENT_ENCODING')

    if content_enc == 'gzip':
        raw_body = zlib.decompress(raw_body)

    return json.loads(raw_body)
示例#3
0
文件: tests.py 项目: 4johndoe/mygpo
    def test_set_get_subscriptions(self):
        """ Tests that an upload subscription is returned back correctly """

        # upload a subscription
        response = self.client.post(self.url, json.dumps(self.action_data),
                                    content_type="application/json",
                                    **self.extra)
        self.assertEqual(response.status_code, 200, response.content)

        # verify that the subscription is returned correctly
        response = self.client.get(self.url, {'since': '0'}, **self.extra)
        self.assertEqual(response.status_code, 200, response.content)
        response_obj = json.loads(response.content)
        self.assertEqual(self.action_data['add'], response_obj['add'])
        self.assertEqual([], response_obj.get('remove', []))
示例#4
0
文件: flickr.py 项目: fk-lx/mygpo
def get_photo_sizes(photo_id):
    api_key = settings.FLICKR_API_KEY
    request = 'http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=%s&photo_id=%s&format=json' % (api_key, photo_id)

    resp = urllib.urlopen(request).read()

    extract_re = '^jsonFlickrApi\((.*)\)$'
    m = re.match(extract_re, resp)
    if not m:
        return []

    resp_obj = json.loads(m.group(1))

    try:
        return resp_obj['sizes']['size']
    except KeyError:
        return []
示例#5
0
文件: tests.py 项目: 4johndoe/mygpo
    def test_episode_actions(self):
        url = reverse(episodes, kwargs={
            'version': '2',
            'username': self.user.username,
        })

        # upload actions
        response = self.client.post(url, json.dumps(self.action_data),
                                    content_type="application/json",
                                    **self.extra)
        self.assertEqual(response.status_code, 200, response.content)

        response = self.client.get(url, {'since': '0'}, **self.extra)
        self.assertEqual(response.status_code, 200, response.content)
        response_obj = json.loads(response.content)
        actions = response_obj['actions']
        self.assertTrue(self.compare_action_list(self.action_data, actions))
示例#6
0
文件: tests.py 项目: fk-lx/mygpo
    def test_episode_actions(self):
        url = reverse(episodes, kwargs=dict(version='2', username=self.user.username))

        # upload actions
        response = self.client.post(url, json.dumps(self.action_data),
                content_type="application/json", **self.extra)
        self.assertEqual(response.status_code, 200, response.content)


        # get all
        extra = deepcopy(self.extra)
        extra['since'] = 0

        response = self.client.get(url, **extra)
        self.assertEqual(response.status_code, 200, response.content)
        response_obj = json.loads(response.content)
        actions = response_obj['actions']
        self.assertTrue(self.compare_action_list(self.action_data, actions))
示例#7
0
文件: simple.py 项目: 4johndoe/mygpo
def parse_subscription(raw_post_data, format):
    """ Parses the data according to the format """
    if format == 'txt':
        urls = raw_post_data.split('\n')

    elif format == 'opml':
        begin = raw_post_data.find('<?xml')
        end = raw_post_data.find('</opml>') + 7
        i = Importer(content=raw_post_data[begin:end])
        urls = [p['url'] for p in i.items]

    elif format == 'json':
        begin = raw_post_data.find('[')
        end = raw_post_data.find(']') + 1
        urls = json.loads(raw_post_data[begin:end])

    else:
        return []

    urls = filter(None, urls)
    urls = map(normalize_feed_url, urls)
    return urls
示例#8
0
文件: flattr.py 项目: 4johndoe/mygpo
                    settings.FLATTR_SECRET)
        elif self.user.profile.settings.get_setting('flattr_token', ''):
            headers['Authorization'] = 'Bearer ' + self.user.profile.settings.get_wksetting(FLATTR_TOKEN)

        if data is not None:
            data = json.dumps(data)

        try:
            response = utils.urlopen(url, headers, data)
        except urllib2.HTTPError, error:
            return {'_gpodder_statuscode': error.getcode()}
        except urllib2.URLError, error:
            return {'_gpodder_no_connection': False}

        if response.getcode() == 200:
            return json.loads(response.read())

        return {'_gpodder_statuscode': response.getcode()}

    def get_auth_url(self):
        return self.AUTH_URL_TEMPLATE % {
                'client_id': settings.FLATTR_KEY,
                'redirect_uri': self._get_callback(),
        }

    def has_token(self):
        return bool(self.user.profile.settings.get_wksetting(FLATTR_TOKEN))

    def process_retrieved_code(self, url):
        url_parsed = urlparse.urlparse(url)
        query = urlparse.parse_qs(url_parsed.query)