コード例 #1
0
ファイル: unittests.py プロジェクト: poochin/feedbyselectors
    def testFeedData(self):
        user = models.User(user=users.User("*****@*****.**"))
        user.put()

        feed = models.CustomFeed(parent=user, name=u"test")
        feed.setbypost(posts)
        feed.put()

        soup = Soup(defines.defaulttesthtml)
        titles = common.selectortext(soup, posts["item_title_selector"], posts["item_title_attr"])
        links = common.selectortext(soup, posts["item_link_selector"], posts["item_link_attr"])
        descriptions = common.selectortext(soup, posts["item_description_selector"], posts["item_description_attr"])

        items = [dict([("title", t), ("link", l), ("description", d)]) for t, l, d in zip(titles, links, descriptions)]
        rss_title = posts["rss_title"].encode("UTF-8")
        rss_link = posts["rss_link"].encode("UTF-8")
        rss_description = posts["rss_description"].encode("UTF-8")

        fd = models.FeedData(parent=feed)
        fd.atom = common.buildatom("Anon", rss_title, rss_link, rss_description, items)
        if common.django.VERSION < (1, 1, 5):
            # django <= 1.1.4 でなければ RSS と RDF の作成時にエラーが出る
            fd.rss = common.buildrss("Anon", posts["rss_title"], posts["rss_link"], posts["rss_description"], items)
            fd.rdf = common.buildrdf("Anon", posts["rss_title"], posts["rss_link"], posts["rss_description"], items)
        fd.put()

        self.assertEqual(1, models.FeedData.all().ancestor(user).count())
コード例 #2
0
    def get(self):
        user = nextusermodel()
        if not user:
            common.error(self, 404, "user not found")
            return

        models.db.run_in_transaction(logdeletion, user.key())

        customfeeds = models.CustomFeed.all().ancestor(user).order("time")
        for cf in customfeeds:
            if not cf.rss_link:
                continue

            ref = fetch(cf.rss_link).content
            soup = Soup(ref)

            dict_compilelist = []

            message = u""
            if cf.item_title_enable:
                titles = common.selectortext(soup, cf.item_title_selector, cf.item_title_attr)
                dlist_title = [("title", t) for t in titles]
                dict_compilelist.append(dlist_title)
                message += u"(title %d 個) " % (len(titles))

            if cf.item_link_enable:
                links = common.selectortext(soup, cf.item_link_selector, cf.item_link_attr)
                dlist_link = [("link", l) for l in links]
                dict_compilelist.append(dlist_link)
                message += u"(links %d 個) " % (len(links))

            if cf.item_description_enable:
                descriptions = common.selectortext(soup, cf.item_description_selector, cf.item_description_attr)
                dlist_description = [("description", d) for d in descriptions]
                dict_compilelist.append(dlist_description)
                message += u"(descriptions %d 個) " % (len(descriptions))

            if cf.item_date_enable:
                dates = common.selectortext(soup, cf.item_date_selector, cf.item_date_attr)
                dlist_date = [("pubdate", dateparser(d)) for d in dates]
                dict_compilelist.append(dlist_date)
                message += u"(dates %d 個) " % (len(dates))

            message += u"見つかりました。"
            models.Log(feedname=cf.name, type=models.Log._types["info"], message=message, parent=user).put()

            items = []
            for dl in zip(*dict_compilelist):
                d = {"title": "", "link": "", "description": ""}
                d.update(dict(dl))
                items.append(d)

            feeddata = models.FeedData.get_by_key_name(cf.name, parent=cf)
            if not feeddata:
                feeddata = models.FeedData(parent=cf, key_name=cf.name)

            try:
                rss_title = cf.rss_title.encode("UTF-8")
                rss_link = cf.rss_link.encode("UTF-8")
                rss_description = cf.rss_description.encode("UTF-8")

                feeddata.atom = common.buildatom("Anon", rss_title, rss_link, rss_description, items).decode("UTF-8")
                feeddata.rss = common.buildrss("Anon", rss_title, rss_link, rss_description, items).decode("UTF-8")
                feeddata.rdf = common.buildrdf("Anon", rss_title, rss_link, rss_description, items).decode("UTF-8")

                if not feeddata.put():
                    raise ValueError  # TODO: save Error
            except:
                message = u"何かエラーが発生しました。"
                models.Log(feedname=cf.name, type=models.Log._types["error"], message=message, parent=user).put()
                raise

            else:
                message = u"カスタムフィードの作成に成功しました。"
                models.Log(feedname=cf.name, type=models.Log._types["success"], message=message, parent=user).put()