subreddit.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:furi-kura 作者: benjamindean 项目源码 文件源码
def update_indicator(self):
        # Get subreddit name from config
        subreddit = self.indicator.config.get('subreddit')

        # Exit if not specified
        if not subreddit:
            return

        posts_type = self.indicator.config.get('posts_type', 1)
        posts_limit = self.indicator.config.get('posts_limit', '5')

        types = {
            1: 'new',
            2: 'hot',
            3: 'top',
            4: 'random'
        }

        type_string = types[int(posts_type)]

        # Get last posts
        data = self.indicator.request.get_subreddit(subreddit, type_string, posts_limit)

        # Exit if not specified
        if not data:
            return

        # Where to append
        menu = self.indicator.builder.get_object('furikura_menu')

        for child in menu.get_children():
            if child.get_name() == 'subreddit_post':
                child.destroy()

        # Show title
        name = self.indicator.builder.get_object('subreddit')
        name.set_label('/r/%s/%s' % (subreddit, type_string))
        name.show()

        # Show separator
        self.indicator.builder.get_object('subreddit_separator_one').show()
        self.indicator.builder.get_object('subreddit_separator_two').show()

        # Iterate through last posts and append them to the menu
        for post in data:
            title = post['title'][:40] + (post['title'][40:] and '...')
            item = Gtk.MenuItem('{upvotes}{gold} |  {title}'.format(
                upvotes=post['upvotes'],
                gold=' \u2605' if post['gilded'] else '',
                title=html.unescape(title)
            ))
            url = 'https://www.reddit.com' + post['permalink'] \
                if self.indicator.config.get('use_permalink') \
                else post['link']
            item.connect('activate', self.__open_url, url)
            item.set_name('subreddit_post')
            menu.add_child(self.indicator.builder, item)
            item.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号