Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Honeypot
mopidy-youtube
Commits
c0dfe8f0
Commit
c0dfe8f0
authored
Jan 25, 2017
by
Gerion Entrup
Browse files
backend: PEP8 and cleanup
parent
619a9b59
Changes
1
Hide whitespace changes
Inline
Side-by-side
mopidy_youtube/backend.py
View file @
c0dfe8f0
...
...
@@ -3,8 +3,6 @@
from
__future__
import
unicode_literals
import
re
import
string
import
unicodedata
from
urlparse
import
parse_qs
,
urlparse
from
mopidy
import
backend
...
...
@@ -21,7 +19,6 @@ yt_api_endpoint = 'https://www.googleapis.com/youtube/v3/'
yt_key
=
'AIzaSyBBnJyPjdmmVemPeeEmcOCND9jZCNHVXnQ'
session
=
requests
.
Session
()
video_uri_prefix
=
'youtube:video'
search_uri
=
'youtube:search'
...
...
@@ -50,27 +47,29 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
def
_get_videos
(
self
,
ids
):
"""Process youtube ids and return tracks"""
logger
.
info
(
"Get videos for '%s'"
,
str
(
ids
))
fancy_ids
=
' '
.
join
([
str
(
x
)
for
x
in
ids
])
logger
.
info
(
"Get videos for ids: %s"
,
fancy_ids
)
query
=
{
'part'
:
'id,snippet,contentDetails'
,
'fields'
:
'items(id,snippet(title,channelTitle,thumbnails(high(url))),contentDetails(duration))'
,
'id'
:
','
.
join
(
ids
),
'id'
:
','
.
join
(
ids
),
'key'
:
yt_key
}
results
=
session
.
get
(
yt_api_endpoint
+
'videos'
,
params
=
query
).
json
()[
'items'
]
results
=
session
.
get
(
yt_api_endpoint
+
'videos'
,
params
=
query
).
json
()[
'items'
]
tracks
=
[]
for
result
in
results
:
track
=
Track
(
artists
=
[
Artist
(
name
=
'YouTube'
)],
name
=
result
[
'snippet'
][
'title'
],
length
=
self
.
_get_duration
(
result
[
'contentDetails'
][
'duration'
]),
album
=
Album
(
name
=
result
[
'id'
],
images
=
[
result
[
'snippet'
][
'thumbnails'
][
'high'
][
'url'
]]
),
uri
=
'yt:'
+
result
[
'id'
]
)
artists
=
[
Artist
(
name
=
'YouTube'
)],
name
=
result
[
'snippet'
][
'title'
],
length
=
self
.
_get_duration
(
result
[
'contentDetails'
][
'duration'
]),
album
=
Album
(
name
=
result
[
'id'
],
images
=
[
result
[
'snippet'
][
'thumbnails'
][
'high'
][
'url'
]]
),
uri
=
'yt:'
+
result
[
'id'
]
)
tracks
.
append
(
track
)
return
tracks
...
...
@@ -78,8 +77,8 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
"""Process youtube playlist and return tracks"""
logger
.
info
(
"Get Playlist for '%s'"
,
str
(
id
))
page
=
''
videoids
=
[]
while
page
is
not
None
and
len
(
videoids
)
<
maxresults
:
video
_
ids
=
[]
while
page
is
not
None
and
len
(
video
_
ids
)
<
maxresults
:
query
=
{
'part'
:
'id,snippet'
,
'fields'
:
'nextPageToken,items(snippet(resourceId(videoId)))'
,
...
...
@@ -88,11 +87,12 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
'key'
:
yt_key
,
'pageToken'
:
page
,
}
results
=
session
.
get
(
yt_api_endpoint
+
'playlistItems'
,
params
=
query
).
json
()[
'items'
]
videoids
+=
[
x
[
'snippet'
][
'resourceId'
][
'videoId'
]
for
x
in
results
]
results
=
session
.
get
(
yt_api_endpoint
+
'playlistItems'
,
params
=
query
).
json
()[
'items'
]
for
res
in
results
:
video_ids
.
append
(
res
[
'snippet'
][
'resourceId'
][
'videoId'
])
page
=
results
[
'nextPageToken'
]
or
None
return
self
.
_get_videos
(
videoids
)
return
self
.
_get_videos
(
video_ids
)
def
_get_search
(
self
,
query
,
maxresults
=
20
):
"""Process youtube search and return tracks"""
...
...
@@ -104,10 +104,10 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
'q'
:
query
,
'key'
:
yt_key
}
results
=
session
.
get
(
yt_api_endpoint
+
'search'
,
params
=
query
).
json
()[
'items'
]
videoids
=
[
x
[
'id'
][
'videoId'
]
for
x
in
results
]
return
self
.
_get_videos
(
videoids
)
results
=
session
.
get
(
yt_api_endpoint
+
'search'
,
params
=
query
).
json
()[
'items'
]
video
_
ids
=
[
x
[
'id'
][
'videoId'
]
for
x
in
results
]
return
self
.
_get_videos
(
video
_
ids
)
def
lookup
(
self
,
uri
):
"""Process youtube playlist or id and return tracks"""
...
...
@@ -129,25 +129,27 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
def
search
(
self
,
query
=
None
,
uris
=
None
,
exact
=
False
):
"""Search for query in youtube and return tracks"""
# TODO Support exact search
logger
.
info
(
"Searching YouTube for query '%s'"
,
query
)
if
not
query
:
logger
.
info
(
"Query could not be handled."
)
return
if
'any'
in
query
:
search_query
=
' '
.
join
(
query
.
values
()[
0
])
logger
.
info
(
"Searching YouTube for query '%s'"
,
search_query
)
return
SearchResult
(
uri
=
search_uri
,
tracks
=
self
.
_get_search
(
search_query
)
)
return
None
class
YouTubePlaybackProvider
(
backend
.
PlaybackProvider
):
def
translate_uri
(
self
,
uri
):
"""Translate youtube video url or id into ressource url"""
logger
.
info
(
"Playing YouTube id: %s"
%
uri
)
logger
.
info
(
"Playing YouTube id: %s"
,
uri
)
for
scheme
in
self
.
backend
.
uri_schemes
:
if
uri
.
startswith
(
scheme
):
uri
=
uri
[
len
(
scheme
)
+
1
:]
...
...
@@ -172,4 +174,3 @@ class YouTubePlaybackProvider(backend.PlaybackProvider):
else
:
trackList
=
[
ytUri
]
return
trackList
[
0
][
'url'
]
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment