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


Python Fetcher.get_subscriptions方法代码示例

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


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

示例1: __init__

# 需要导入模块: from fetcher import Fetcher [as 别名]
# 或者: from fetcher.Fetcher import get_subscriptions [as 别名]
class Handler:

    def __init__(self, directory, api_key, username, max_vids ):
        self.API_KEY = api_key
        self.USERNAME = username

        self.SAVE_FILE_PATH = os.path.normpath(directory + 'ytsubs_' + self.USERNAME.lower() + '.json')
        self.fetcher = Fetcher(self.USERNAME, self.API_KEY)

        self.MAX_VIDEOS = max_vids
        self.watched = []
        self.additions = []
        self.raw_videos = []
        self.load()

    def add_to_watched(self, addition):
        if addition is not None and addition not in self.watched:
            self.watched.append(addition)
            self.save()
            # TODO Maybe add a data changed flag
            # A video marked as watched can safely be deleted from the local 'own additions'.
            self._remove_from_additions(addition)

    def _remove_from_watched(self, removal):
        if removal in self.watched:
            self.watched.remove(removal)
            # TODO Maybe add a data changed flag

    def _get_vids(self):
        videos = self.fetcher.get_subscriptions()
        # TODO Maybe add a data changed flag
        self.raw_videos = videos

    # Adds a video with the id 'addition'
    def add_video(self, addition):
        # Write each video id in a separate line in a file
        if addition is not None and addition not in [add['id'] for add in self.additions]:
            self.additions.append(self.fetcher.query_video_information(addition))
            # TODO Maybe add a data changed flag
            # This ensures added videos to show up even if they have previously been marked as watched.
            self._remove_from_watched(addition)

    def _remove_from_additions(self, removal):
        # Check if the id for the video to be removed is in the 'own additions'.
        # if removal in [i['id'] for i in self.additions]:
        # Actually remove it from the list.
        self.additions = [i for i in self.additions if not i['id'] == removal]
        # TODO Maybe add a data changed flag

    def update_videos(self):
        self._get_vids()
        if reload:
            self.save()

    def save(self):
        with open(self.SAVE_FILE_PATH, 'w') as save_file:
            save_file.write(self.jsonize_data())

    def load(self):
        try:
            with open(self.SAVE_FILE_PATH, 'r') as read_file:
                data = json.load(read_file)
        except (ValueError, IOError):
            return

        for type, list in data.iteritems():
            if type == 'watched':
                self.watched = [v['id'] for v in list]
            elif type == 'additions':
                self.additions = list
            elif type == 'raw_videos':
                self.raw_videos = list

    def jsonize_data(self):
        data = {}
        data['watched'] = []
        for id in self.watched:
            if id is not None:
                video = {}
                video['id'] = id
                data['watched'].append(video)

        for type, list in {'additions': self.additions, 'raw_videos': [vid for vid in self.raw_videos if vid['id'] not in self.watched][:self.MAX_VIDEOS]}.iteritems():
            data[type] = []
            for addition in list:
                video = {}
                video['id'] = addition['id']
                video['snippet'] = {}
                video['contentDetails'] = {}
                video['snippet']['title'] = addition['snippet']['title']
                #video['snippet']['description'] = addition['snippet']['description']
                video['snippet']['publishedAt'] = addition['snippet']['publishedAt']
                video['contentDetails']['duration'] = addition['contentDetails']['duration']
                video['snippet']['categoryId'] = addition['snippet']['categoryId']
                video['snippet']['channelTitle'] = addition['snippet']['channelTitle']
                video['snippet']['channelId'] = addition['snippet']['channelId']

                data[type].append(video)
        return json.dumps(data)

#.........这里部分代码省略.........
开发者ID:JohnAZoidberg,项目名称:ytsubs,代码行数:103,代码来源:handler.py


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