Exemplo n.º 1
0
    def test_autodiscovery(self):
        # Test discovery
        url = "http://boingboing.net"
        feed = Feed.autodiscover(url)
        self.assertEqual(feed, "http://boingboing.net/atom.xml")

        # Ensure passing an RSS link returns itself
        url = "http://www.rsspect.com/rss/asw.xml"
        feed = Feed.autodiscover(url)
        self.assertEqual(feed, url)

        # Bogus url
        url = "http://"
        feed = Feed.autodiscover(url)
        self.assertEqual(feed, None)
Exemplo n.º 2
0
    def _import_json_items(self, import_file):
        data = None
        for f in self.z.namelist():
            if import_file in f:
                data = json.loads(self.z.open(f).read(), strict=False)
                break

        if data is None:
            return False

        try:
            # This is like, weak sauce verification, hoping that we're
            # not about to get bogus data. Still, a carefully crafted
            # attack file could make it past this check.
            id = data['id']
            if not (id.endswith('starred') or
                    id.endswith('broadcast-friends') or
                    id.endswith('broadcast') or
                    id.endswith('post') or
                    id.endswith('like')):
                return False
        except KeyError:
            return False

        for i in data['items']:
            title = i['origin']['title']
            site = i['origin']['htmlUrl']
            link = i['origin']['streamId']
            if link.startswith('feed/'):
                link = link.replace('feed/', '', 1)
            # These are some weird bullshit links created by google
            # reader. Try and discover a real link instead.
            elif link.startswith('user/'):
                maybe = Feed.autodiscover(site)
                if maybe:
                    link = maybe

            feed = Feed.create_raw(title, link, site)

            item = {}
            item['id'] = i['id']
            item['title'] = i['title']
            item['url'] = i.get('canonical', i.get('alternate', ''))[0]['href']
            try:
                item['content'] = i['content']['content']
            except KeyError:
                try:
                    item['content'] = i['summary']['content']
                except KeyError:
                    # No idea if this is even possible, we should squawk
                    item['content'] = ''
            item['time'] = i['published']
            entry = FakeEntry(item)
            user_item = _new_user_item(self.user, feed, entry)

            for c in i.get('categories', []):
                if c.startswith('user/') and c.endswith('/like'):
                    user_item.tags.add('liked')
                elif c.startswith('user/') and c.endswith('/post'):
                    user_item.tags.add('notes')
                elif c.startswith('user/') and c.endswith('/created'):
                    user_item.tags.add('notes')
                # Annoyingly, if something is shared-with-you, it also
                # gets the /broadcast tag. So if we are processing the
                # shared-with-you.json file, don't mark those items as
                # things that *you've* shared
                elif c.startswith('user/') and c.endswith('/broadcast') \
                    and not id.endswith('broadcast-friends'):
                    user_item.tags.add('shared')
                elif c.startswith('user/') and c.endswith('/broadcast-friends'):
                    user_item.tags.add('shared-with-you')
                elif c.startswith('user/') and c.endswith('/starred'):
                    user_item.starred = True
                elif c.startswith('user/') and c.endswith('/read'):
                    user_item.read = True
                elif c.startswith('user/') and ('label' in c):
                    tag = c.split('/')[-1]
                    user_item.tags.append(tag)

            user_item.tags.add('imported')
            user_item.save()

            try:
                # This comes from the 'shared-with-you' category.
                friend_userid = i['via'][0]['href'].split('/')[-4]
                # Not sure how to save/model friend_userid yet
            except KeyError:
                pass

            # XXX: should we do something about i['comments'] too?
            for a in i['annotations']:
                # Following attributes are interesting, but not sure
                # how to model them yet.
                #  - a['content']
                #  - a['userId']
                #  - a['profileId']
                pass