Merge pull request #18 from jdepoix/bugfix/ISSUE-17

API no longer logs to error
This commit is contained in:
jdepoix 2019-07-15 15:56:41 +02:00 committed by GitHub
commit 69812e0024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 19 deletions

View File

@ -9,16 +9,11 @@ from xml.etree import ElementTree
import re
import logging
import requests
from ._html_unescaping import unescape
logger = logging.getLogger(__name__)
class YouTubeTranscriptApi():
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
one of the video transcripts
: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
video ids, which could not be retrieved
: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 = {}
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
play around with the language codes a bit, to find the one which is working for you!
: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
: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:
return _TranscriptParser(_TranscriptFetcher(video_id, languages, proxies).fetch()).parse()
except Exception:
logger.error(
YouTubeTranscriptApi.CouldNotRetrieveTranscript.ERROR_MESSAGE.format(
video_url=_TranscriptFetcher.WATCH_URL.format(video_id=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 != '':
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,
languages=parsed_args.languages,
continue_after_error=True,
proxies=proxies
)
if parsed_args.json:
return json.dumps(transcripts)
else:
return pprint.pformat(transcripts)
return '\n\n'.join(
[str(YouTubeTranscriptApi.CouldNotRetrieveTranscript(video_id)) for video_id in unretrievable_videos]
+ [json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)]
)
def _parse_args(self):
parser = argparse.ArgumentParser(