当前位置: 首页>>代码示例>>Python>>正文


Python Pocket.bulk_add方法代码示例

本文整理汇总了Python中pocket.Pocket.bulk_add方法的典型用法代码示例。如果您正苦于以下问题:Python Pocket.bulk_add方法的具体用法?Python Pocket.bulk_add怎么用?Python Pocket.bulk_add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pocket.Pocket的用法示例。


在下文中一共展示了Pocket.bulk_add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle

# 需要导入模块: from pocket import Pocket [as 别名]
# 或者: from pocket.Pocket import bulk_add [as 别名]
    def handle(self, *args, **options):
        users = User.objects.all().order_by('id')
        # we don't care about items older than 3 days
        limit_days = timedelta(days=7)
        now = datetime.now()
        for user in users:
            feeds = Feed.objects.filter(user=user).order_by('id')
            pocket_instance = Pocket(settings.POCKET_CONSUMER_KEY,
                                     user.profile.pocket_access_token)
            entries = []
            print('TOTAL FEEDS = %d for USER = %s' %
                  (len(feeds), user.username.encode('utf8')))

            for i, feed in enumerate(feeds):
                entry_count = 0

                # get new items only
                d = feedparser.parse(feed.link, etag=feed.etag,
                                     modified=feed.last_modified)
                # update etag and last_modified attributes of the feeds
                if hasattr(d, 'etag') and feed.etag != d.etag:
                    feed.etag = d.etag
                if hasattr(d, 'modified_parsed'):
                    feed.last_modified = timezone.make_aware(
                        datetime.fromtimestamp(mktime(d.modified_parsed)),
                        timezone.get_default_timezone())
                feed.save()

                for entry in d.entries:
                    if hasattr(entry, 'published_parsed'):
                        published = datetime.fromtimestamp(
                            mktime(entry.published_parsed))
                    elif hasattr(entry, 'updated_parsed'):
                        published = datetime.fromtimestamp(
                            mktime(entry.updated_parsed))
                    elif hasattr(entry, 'created_parsed'):
                        published = datetime.fromtimestamp(
                            mktime(entry.created_parsed))
                    else:
                        # better than nothing
                        published = datetime.now()

                    published = timezone.make_aware(
                        published,
                        timezone.get_default_timezone()
                    )

                    # filter out old entries
                    if published + limit_days < timezone.now():
                        break

                    # check if entry already exists in the db
                    try:
                        # we cannot rely on dates
                        new_entry, new_entry_created = Entry.objects.get_or_create(
                            link=entry.link[:512],
                            title=entry.title[:512],
                            # published=published,
                            feed=feed,
                            user=user
                        )
                    except Exception as e:
                        print(e.encode('utf8'))
                        continue

                    # save the date now
                    if new_entry_created:
                        new_entry.published = published
                        new_entry.save()
                    else:
                        continue

                    entries.append({
                        'published': published,
                        'link': entry.link,
                        'title': entry.title,
                        'tags': [x['text'] for x in feed.tags.values('text')] +
                            ['pocketfeed']
                    })
                    entry_count += 1

                print('    Parsed feeds #%d with %d entries: %s.' %
                      (i + 1, entry_count, feed.title.encode('utf8')))

            # sort by date
            entries.sort(key=lambda x: x['published'])

            # create data for submission to pocket
            for entry in entries:
                pocket_instance = pocket_instance.bulk_add(
                    url=entry['link'],
                    title=entry['title'],
                    tags=entry['tags']
                )

            # submit to pocket
            if len(entries):
                try:
                    pocket_response, pocket_headers = pocket_instance.commit()
                    if not int(pocket_headers['x-limit-user-remaining']) >= 1:
#.........这里部分代码省略.........
开发者ID:kodchi,项目名称:pocketfeed,代码行数:103,代码来源:parse_feeds.py


注:本文中的pocket.Pocket.bulk_add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。