Skip to content

Commit

Permalink
Merge pull request #539 from ritiek/patch-audiostream-urls
Browse files Browse the repository at this point in the history
Patch bug in Pafy when fetching audiostreams with latest youtube-dl
  • Loading branch information
ritiek authored Apr 28, 2019
2 parents d61309b + dfd48f7 commit 951ae02
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
-

### Fixed
-
- Patch bug in Pafy when fetching audiostreams with latest youtube-dl ([@ritiek](https://github.com/ritiek)) (#539)

### Changed
- Removed duplicate debug log entry from `internals.trim_song` ([@ritiek](https://github.com/ritiek)) (#519)
Expand Down
22 changes: 18 additions & 4 deletions spotdl/patcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pafy import backend_youtube_dl
import pafy

from spotdl import internals
Expand All @@ -20,12 +21,25 @@ def _getbestthumb(self):
if self._content_available(url):
return url

def _process_streams(self):
for format_index in range(len(self._ydl_info['formats'])):
try:
self._ydl_info['formats'][format_index]['url'] = self._ydl_info['formats'][format_index]['fragment_base_url']
except KeyError:
pass
return backend_youtube_dl.YtdlPafy._old_process_streams(self)

@classmethod
def _content_available(cls, url):
return internals.content_available(url)

def patch_pafy():
pafy.backend_shared.BasePafy._bestthumb = None
pafy.backend_shared.BasePafy._content_available = _content_available
pafy.backend_shared.BasePafy.getbestthumb = _getbestthumb

class PatchPafy:
def patch_getbestthumb(self):
pafy.backend_shared.BasePafy._bestthumb = None
pafy.backend_shared.BasePafy._content_available = _content_available
pafy.backend_shared.BasePafy.getbestthumb = _getbestthumb

def patch_process_streams(self):
backend_youtube_dl.YtdlPafy._old_process_streams = backend_youtube_dl.YtdlPafy._process_streams
backend_youtube_dl.YtdlPafy._process_streams = _process_streams
4 changes: 3 additions & 1 deletion spotdl/youtube_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
# More info: https://github.com/mps-youtube/pafy/pull/211
if pafy.__version__ <= "0.5.4":
from spotdl import patcher
patcher.patch_pafy()
pafy_patcher = patcher.PatchPafy()
pafy_patcher.patch_getbestthumb()
pafy_patcher.patch_process_streams()


def set_api_key():
Expand Down
1 change: 1 addition & 0 deletions test/test_download_with_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def test_correct_container_for_m4a(self, filename_fixture, monkeypatch):


class TestAvconv:
@pytest.mark.skip(reason="avconv is no longer provided with FFmpeg")
def test_convert_from_m4a_to_mp3(self, filename_fixture, monkeypatch):
monkeypatch.setattr("os.remove", lambda x: None)
expect_command = "avconv -loglevel 0 -i {0}.m4a -ab 192k {0}.mp3 -y".format(
Expand Down
3 changes: 2 additions & 1 deletion test/test_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import pytest

patcher.patch_pafy()
pafy_patcher = patcher.PatchPafy()
pafy_patcher.patch_getbestthumb()

class TestPafyContentAvailable:
pass
Expand Down

0 comments on commit 951ae02

Please sign in to comment.