本文整理匯總了Python中fetcher.Fetcher.query_video_information方法的典型用法代碼示例。如果您正苦於以下問題:Python Fetcher.query_video_information方法的具體用法?Python Fetcher.query_video_information怎麽用?Python Fetcher.query_video_information使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fetcher.Fetcher
的用法示例。
在下文中一共展示了Fetcher.query_video_information方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from fetcher import Fetcher [as 別名]
# 或者: from fetcher.Fetcher import query_video_information [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)
#.........這裏部分代碼省略.........