API no longer logs to error

This commit is contained in:
Jonas Depoix 2019-07-15 15:48:01 +02:00
parent 8ca8609b7d
commit 0151413ad1
2 changed files with 9 additions and 19 deletions

View File

@ -9,16 +9,11 @@ from xml.etree import ElementTree
import re import re
import logging
import requests import requests
from ._html_unescaping import unescape from ._html_unescaping import unescape
logger = logging.getLogger(__name__)
class YouTubeTranscriptApi(): class YouTubeTranscriptApi():
class CouldNotRetrieveTranscript(Exception): class CouldNotRetrieveTranscript(Exception):
""" """
@ -57,11 +52,11 @@ class YouTubeTranscriptApi():
:param continue_after_error: if this is set the execution won't be stopped, if an error occurs while retrieving :param continue_after_error: if this is set the execution won't be stopped, if an error occurs while retrieving
one of the video transcripts one of the video transcripts
:type continue_after_error: bool :type continue_after_error: bool
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
:return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of :return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of
video ids, which could not be retrieved video ids, which could not be retrieved
:rtype: ({str: [{'text': str, 'start': float, 'end': float}]}, [str]} :rtype: ({str: [{'text': str, 'start': float, 'end': float}]}, [str]}
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
:rtype {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
""" """
data = {} data = {}
unretrievable_videos = [] unretrievable_videos = []
@ -89,19 +84,14 @@ class YouTubeTranscriptApi():
do so. As I can't provide a complete list of all working language codes with full certainty, you may have to do so. As I can't provide a complete list of all working language codes with full certainty, you may have to
play around with the language codes a bit, to find the one which is working for you! play around with the language codes a bit, to find the one which is working for you!
:type languages: [str] :type languages: [str]
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
:return: a list of dictionaries containing the 'text', 'start' and 'duration' keys :return: a list of dictionaries containing the 'text', 'start' and 'duration' keys
:rtype: [{'text': str, 'start': float, 'end': float}] :rtype: [{'text': str, 'start': float, 'end': float}]
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
:rtype {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
""" """
try: try:
return _TranscriptParser(_TranscriptFetcher(video_id, languages, proxies).fetch()).parse() return _TranscriptParser(_TranscriptFetcher(video_id, languages, proxies).fetch()).parse()
except Exception: except Exception:
logger.error(
YouTubeTranscriptApi.CouldNotRetrieveTranscript.ERROR_MESSAGE.format(
video_url=_TranscriptFetcher.WATCH_URL.format(video_id=video_id)
)
)
raise YouTubeTranscriptApi.CouldNotRetrieveTranscript(video_id) raise YouTubeTranscriptApi.CouldNotRetrieveTranscript(video_id)

View File

@ -18,17 +18,17 @@ class YouTubeTranscriptCli():
if parsed_args.http_proxy != '' or parsed_args.https_proxy != '': if parsed_args.http_proxy != '' or parsed_args.https_proxy != '':
proxies = {"http": parsed_args.http_proxy, "https": parsed_args.https_proxy} proxies = {"http": parsed_args.http_proxy, "https": parsed_args.https_proxy}
transcripts, _ = YouTubeTranscriptApi.get_transcripts( transcripts, unretrievable_videos = YouTubeTranscriptApi.get_transcripts(
parsed_args.video_ids, parsed_args.video_ids,
languages=parsed_args.languages, languages=parsed_args.languages,
continue_after_error=True, continue_after_error=True,
proxies=proxies proxies=proxies
) )
if parsed_args.json: return '\n\n'.join(
return json.dumps(transcripts) [str(YouTubeTranscriptApi.CouldNotRetrieveTranscript(video_id)) for video_id in unretrievable_videos]
else: + [json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)]
return pprint.pformat(transcripts) )
def _parse_args(self): def _parse_args(self):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(