-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
youtube-transcript-api
library
- Loading branch information
Showing
9 changed files
with
421 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from tests.factories.transcript import TranscriptFactory | ||
from tests.factories.transcript_info import TranscriptInfoFactory | ||
from tests.factories.video import VideoFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from factory import Factory, Sequence | ||
|
||
from via.services.youtube_transcript import TranscriptInfo | ||
|
||
|
||
class TranscriptInfoFactory(Factory): | ||
class Meta: | ||
model = TranscriptInfo | ||
|
||
language_code = "en-us" | ||
name = "English (United States)" | ||
url = Sequence(lambda n: f"https://example.com/api/timedtext?v={n}") | ||
autogenerated = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
import json | ||
from io import BytesIO | ||
from unittest.mock import sentinel | ||
|
||
import pytest | ||
from h_matchers import Any | ||
from requests import Response | ||
|
||
from tests.factories import TranscriptInfoFactory | ||
from via.services.youtube_transcript import ( | ||
TranscriptInfo, | ||
YouTubeTranscriptService, | ||
factory, | ||
) | ||
|
||
|
||
class TestTranscriptInfo: | ||
@pytest.mark.parametrize( | ||
"transcript_info,expected_id", | ||
[ | ||
( | ||
TranscriptInfo( | ||
"en-us", | ||
"English (United States)", | ||
"https://example.com/transcript", | ||
autogenerated=False, | ||
), | ||
"en-us..RW5nbGlzaCAoVW5pdGVkIFN0YXRlcyk=", | ||
), | ||
( | ||
TranscriptInfo( | ||
"en", | ||
"English", | ||
"https://example.com/transcript", | ||
autogenerated=True, | ||
), | ||
"en.a.RW5nbGlzaA==", | ||
), | ||
], | ||
) | ||
def test_id(self, transcript_info, expected_id): | ||
assert transcript_info.id == expected_id | ||
|
||
|
||
class TestYouTubeTranscriptService: | ||
def test_get_transcript_infos(self, svc, http_service): | ||
# The JSON response body from the YouTube API. | ||
response_json = { | ||
"captions": { | ||
"playerCaptionsTracklistRenderer": { | ||
"captionTracks": [ | ||
{ | ||
"languageCode": "en", | ||
"name": {"simpleText": "English"}, | ||
"baseUrl": "https://example.com/transcript_1", | ||
}, | ||
{ | ||
"languageCode": "en-us", | ||
"name": {"simpleText": "English (United States)"}, | ||
"baseUrl": "https://example.com/transcript_2", | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
response = http_service.post.return_value = Response() | ||
response.raw = BytesIO(json.dumps(response_json).encode("utf-8")) | ||
|
||
transcript_infos = svc.get_transcript_infos("test_video_id") | ||
|
||
caption_tracks = response_json["captions"]["playerCaptionsTracklistRenderer"][ | ||
"captionTracks" | ||
] | ||
assert transcript_infos == [ | ||
Any.instance_of(TranscriptInfo).with_attrs( | ||
{ | ||
"language_code": caption_tracks[0]["languageCode"], | ||
"autogenerated": False, | ||
"name": caption_tracks[0]["name"]["simpleText"], | ||
"url": caption_tracks[0]["baseUrl"], | ||
} | ||
), | ||
Any.instance_of(TranscriptInfo).with_attrs( | ||
{ | ||
"language_code": caption_tracks[1]["languageCode"], | ||
"autogenerated": False, | ||
"name": caption_tracks[1]["name"]["simpleText"], | ||
"url": caption_tracks[1]["baseUrl"], | ||
} | ||
), | ||
] | ||
|
||
@pytest.mark.parametrize( | ||
"transcript_infos,expected_default_transcript_index", | ||
[ | ||
( | ||
[ | ||
TranscriptInfoFactory(language_code="en", name="English"), | ||
TranscriptInfoFactory( | ||
language_code="en-us", name="English (United States)" | ||
), | ||
], | ||
0, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory( | ||
language_code="en-us", name="English (United States)" | ||
), | ||
TranscriptInfoFactory(language_code="en", name="English - DTVCC1"), | ||
], | ||
0, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory(language_code="en", name="English - Foo"), | ||
TranscriptInfoFactory( | ||
language_code="en-us", name="English (United States) - Foo" | ||
), | ||
], | ||
0, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory( | ||
language_code="en-us", name="English (United States) - Foo" | ||
), | ||
TranscriptInfoFactory( | ||
language_code="en", name="English", autogenerated=True | ||
), | ||
], | ||
0, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory( | ||
language_code="en", name="English", autogenerated=True | ||
), | ||
TranscriptInfoFactory( | ||
language_code="en-us", | ||
name="English (United States)", | ||
autogenerated=True, | ||
), | ||
], | ||
0, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory(language_code="fr", name="French"), | ||
TranscriptInfoFactory( | ||
language_code="en", name="English", autogenerated=True | ||
), | ||
], | ||
1, | ||
), | ||
( | ||
[ | ||
TranscriptInfoFactory(language_code="fr", name="French"), | ||
TranscriptInfoFactory(language_code="de", name="Deutsch"), | ||
], | ||
0, | ||
), | ||
], | ||
) | ||
def test_pick_default_transcript( | ||
self, svc, transcript_infos, expected_default_transcript_index | ||
): | ||
assert ( | ||
svc.pick_default_transcript(transcript_infos) | ||
== transcript_infos[expected_default_transcript_index] | ||
) | ||
|
||
def test_get_transcript(self, svc, transcript_info, http_service): | ||
http_service.get.return_value.text = """ | ||
<transcript> | ||
<text start="0.21" dur="1.387">Hey there guys,</text> | ||
<text start="1.597">Lichen' subscribe</text> | ||
<text start="4.327" dur="2.063"> | ||
<font color="#A0AAB4">Buy my merch!</font> | ||
</text> | ||
</transcript> | ||
""" | ||
|
||
transcript = svc.get_transcript(transcript_info) | ||
http_service.get.assert_called_once_with(transcript_info.url) | ||
|
||
assert transcript == [ | ||
{"duration": 1.387, "start": 0.21, "text": "Hey there guys,"}, | ||
{"duration": 0.0, "start": 1.597, "text": "Lichen' subscribe"}, | ||
{"duration": 2.063, "start": 4.327, "text": "Buy my merch!"}, | ||
] | ||
|
||
@pytest.fixture | ||
def svc(self, http_service): | ||
return YouTubeTranscriptService(http_service) | ||
|
||
|
||
class TestFactory: | ||
def test_factory(self, YouTubeTranscriptService, http_service, pyramid_request): | ||
svc = factory(sentinel.context, pyramid_request) | ||
|
||
YouTubeTranscriptService.assert_called_once_with(http_service=http_service) | ||
assert svc == YouTubeTranscriptService.return_value | ||
|
||
@pytest.fixture | ||
def YouTubeTranscriptService(self, patch): | ||
return patch("via.services.youtube_transcript.YouTubeTranscriptService") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.