Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Sourcery refactored master branch #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pytube/captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ def download(

filename += f" ({self.code})"

if srt:
filename += ".srt"
else:
filename += ".xml"

filename += '.srt' if srt else '.xml'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Caption.download refactored with the following changes:

  • Replace if statement with if expression

file_path = os.path.join(target_directory(output_path), filename)

with open(file_path, "w", encoding="utf-8") as file_handle:
Expand Down
8 changes: 4 additions & 4 deletions pytube/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ def build_playback_report(youtube: YouTube) -> None:
ts = int(dt.datetime.utcnow().timestamp())
fp = os.path.join(os.getcwd(), f"yt-video-{youtube.video_id}-{ts}.json.gz")

js = youtube.js
watch_html = youtube.watch_html
vid_info = youtube.vid_info

with gzip.open(fp, "wb") as fh:
Comment on lines -161 to 165
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function build_playback_report refactored with the following changes:

  • Move assignments closer to their usage

js = youtube.js
watch_html = youtube.watch_html
vid_info = youtube.vid_info

fh.write(
json.dumps(
{
Expand Down
8 changes: 4 additions & 4 deletions pytube/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,17 @@ def apply_descrambler(stream_data: Dict, key: str) -> None:
{'foo': [{'bar': '1', 'var': 'test'}, {'em': '5', 't': 'url encoded'}]}

"""
otf_type = "FORMAT_STREAM_TYPE_OTF"

if key == "url_encoded_fmt_stream_map" and not stream_data.get(
"url_encoded_fmt_stream_map"
):
"url_encoded_fmt_stream_map"
):
formats = json.loads(stream_data["player_response"])["streamingData"]["formats"]
formats.extend(
json.loads(stream_data["player_response"])["streamingData"][
"adaptiveFormats"
]
)
otf_type = "FORMAT_STREAM_TYPE_OTF"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_descrambler refactored with the following changes:

  • Move assignments closer to their usage

try:
stream_data[key] = [
{
Expand Down
2 changes: 1 addition & 1 deletion pytube/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def safe_filename(s: str, max_length: int = 255) -> str:
A sanitized string.
"""
# Characters in range 0-31 (0x00-0x1F) are not allowed in ntfs filenames.
ntfs_characters = [chr(i) for i in range(0, 31)]
ntfs_characters = [chr(i) for i in range(31)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function safe_filename refactored with the following changes:

  • Replace range(0, x) with range(x)

characters = [
r'"',
r"\#",
Expand Down
5 changes: 1 addition & 4 deletions pytube/itags.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ def get_format_profile(itag: int) -> Dict:
YouTube format identifier code.
"""
itag = int(itag)
if itag in ITAGS:
res, bitrate = ITAGS[itag]
else:
res, bitrate = None, None
res, bitrate = ITAGS[itag] if itag in ITAGS else (None, None)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_format_profile refactored with the following changes:

  • Replace if statement with if expression

return {
"resolution": res,
"abr": bitrate,
Expand Down
7 changes: 2 additions & 5 deletions pytube/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def download(

bytes_remaining = self.filesize
logger.debug(
"downloading (%s total bytes) file to %s", self.filesize, file_path,
'downloading (%s total bytes) file to %s', bytes_remaining, file_path
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Stream.download refactored with the following changes:

  • Use previously assigned local variable

)

with open(file_path, "wb") as fh:
Expand Down Expand Up @@ -265,10 +265,7 @@ def stream_to_buffer(self, buffer: BinaryIO) -> None:
:rtype: io.BytesIO buffer
"""
bytes_remaining = self.filesize
logger.info(
"downloading (%s total bytes) file to buffer", self.filesize,
)

logger.info('downloading (%s total bytes) file to buffer', bytes_remaining)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Stream.stream_to_buffer refactored with the following changes:

  • Use previously assigned local variable

for chunk in request.stream(self.url):
# reduce the (bytes) remainder by the length of the chunk.
bytes_remaining -= len(chunk)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ def test_ffmpeg_process_res_should_download(_ffmpeg_downloader, youtube):
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_res_none_should_not_download(_ffmpeg_downloader, youtube):
# Given
target = "/target"
Comment on lines -371 to -372
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_ffmpeg_process_res_none_should_not_download refactored with the following changes:

  • Move assignments closer to their usage

streams = MagicMock()
youtube.streams = streams
streams.filter.return_value.first.return_value = None
audio_stream = MagicMock()
streams.get_audio_only.return_value = audio_stream
# When
with pytest.raises(SystemExit):
# Given
target = "/target"
cli.ffmpeg_process(youtube, "XYZp", target)
# Then
_ffmpeg_downloader.assert_not_called()
Expand Down Expand Up @@ -405,8 +405,6 @@ def test_ffmpeg_process_audio_none_should_fallback_download(
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_audio_fallback_none_should_exit(_ffmpeg_downloader, youtube):
# Given
target = "/target"
Comment on lines -408 to -409
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_ffmpeg_process_audio_fallback_none_should_exit refactored with the following changes:

  • Move assignments closer to their usage

streams = MagicMock()
youtube.streams = streams
stream = MagicMock()
Expand All @@ -418,6 +416,8 @@ def test_ffmpeg_process_audio_fallback_none_should_exit(_ffmpeg_downloader, yout
streams.get_audio_only.return_value = None
# When
with pytest.raises(SystemExit):
# Given
target = "/target"
cli.ffmpeg_process(youtube, "best", target)
# Then
_ffmpeg_downloader.assert_not_called()
Expand Down