Commit d9bb3193 authored by Stein Magnus Jodal's avatar Stein Magnus Jodal
Browse files

Merge pull request #50 from jcass77/fix/21_video_url

parents 021e1f58 c5378b38
......@@ -77,6 +77,12 @@ Project resources
Changelog
=========
v2.0.2 (UNRELEASED)
-------------------
- Fix resolving of ``youtube:video`` URIs when looking up tracks. (Fixes: #21,
PR: #50)
v2.0.1 (2015-08-19)
-------------------
......
......@@ -23,6 +23,9 @@ yt_api_endpoint = 'https://www.googleapis.com/youtube/v3/'
yt_key = 'AIzaSyAl1Xq9DwdE_KD4AtPaE4EJl3WZe2zCqg4'
session = requests.Session()
video_uri_prefix = 'youtube:video'
search_uri = 'youtube:search'
def resolve_track(track, stream=False):
logger.debug("Resolving YouTube for track '%s'", track)
......@@ -49,9 +52,8 @@ def resolve_url(url, stream=False):
try:
video = pafy.new(url)
if not stream:
uri = 'youtube:video/%s.%s' % (
safe_url(video.title), video.videoid
)
uri = '%s/%s.%s' % (
video_uri_prefix, safe_url(video.title), video.videoid)
else:
uri = video.getbestaudio()
if not uri: # get video url
......@@ -151,7 +153,7 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
else:
return [item for item in [resolve_url(track)] if item]
else:
return [item for item in [resolve_url(track)] if item]
return [item for item in [resolve_track(track)] if item]
def search(self, query=None, uris=None, exact=False):
# TODO Support exact search
......@@ -166,21 +168,21 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
req = parse_qs(url.query)
if 'list' in req:
return SearchResult(
uri='youtube:search',
uri=search_uri,
tracks=resolve_playlist(req.get('list')[0])
)
else:
logger.info(
"Resolving YouTube for track '%s'", search_query)
return SearchResult(
uri='youtube:search',
uri=search_uri,
tracks=[t for t in [resolve_url(search_query)] if t]
)
else:
search_query = ' '.join(query.values()[0])
logger.info("Searching YouTube for query '%s'", search_query)
return SearchResult(
uri='youtube:search',
uri=search_uri,
tracks=search_youtube(search_query)
)
......
This diff is collapsed.
......@@ -9,6 +9,7 @@ import pytest
import vcr
from mopidy_youtube import backend
from mopidy_youtube.backend import YouTubeLibraryProvider
@pytest.yield_fixture
......@@ -47,7 +48,7 @@ def test_search_yt(pafy_mock_with_video):
@vcr.use_cassette('tests/fixtures/resolve_track.yaml')
def test_resolve_track(pafy_mock_with_video):
video = backend.resolve_track('TU3b1qyEGsE')
video = backend.resolve_track('C0DPdy98e4c')
assert video
......@@ -63,6 +64,31 @@ def test_resolve_track_failed(pafy_mock):
@vcr.use_cassette('tests/fixtures/resolve_track_stream.yaml')
def test_resolve_track_stream(pafy_mock_with_video):
video = backend.resolve_track('TU3b1qyEGsE', stream=True)
video = backend.resolve_track('C0DPdy98e4c', stream=True)
assert video
@vcr.use_cassette('tests/fixtures/resolve_video_track_stream.yaml')
def test_resolve_video_track_stream(pafy_mock_with_video):
video = backend.resolve_track('youtube:video/a title.a video id',
stream=True)
assert video
assert video.uri == "http://example.com/"
@vcr.use_cassette('tests/fixtures/lookup_video_uri.yaml')
def test_lookup_video_uri(caplog):
provider = YouTubeLibraryProvider(mock.PropertyMock())
track = provider.lookup(backend.video_uri_prefix +
'/a title.C0DPdy98e4c')
assert 'Need 11 character video id or the URL of the video.' \
not in caplog.text()
assert track
assert track[0].uri == backend.video_uri_prefix + \
'/TEST VIDEO.C0DPdy98e4c'
......@@ -8,6 +8,7 @@ deps =
mock
pytest
pytest-cov
pytest-capturelog
pytest-xdist
tornado >= 4
vcrpy
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment