Skip to content

Commit

Permalink
fix artist lookup
Browse files Browse the repository at this point in the history
through some emby api changes the artist lookup didnt worked no more.
  • Loading branch information
xsteadfastx committed Mar 23, 2017
1 parent c4f71b9 commit e4ed43c
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 33 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Credits
Changelog
=========

v0.2.5
---------------------------------------

- fixed artist lookup

v0.2.4
---------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion mopidy_emby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mopidy import config, ext


__version__ = '0.2.4'
__version__ = '0.2.5'

logger = logging.getLogger(__name__)

Expand Down
11 changes: 1 addition & 10 deletions mopidy_emby/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,8 @@ def lookup(self, uri=None, uris=None):

elif uri.startswith('emby:artist:') and len(parts) == 3:
artist_id = parts[-1]
albums = self.backend.remote.get_directory(artist_id)
tracks = []

for album in albums.get('Items', []):
album_data = self.backend.remote.get_directory(album['Id'])
tracklist = [
self.backend.remote.get_track(i['Id'])
for i in album_data.get('Items', [])
]

tracks.extend(sorted(tracklist, key=lambda k: k.track_no))
tracks = self.backend.remote.lookup_artist(artist_id)

else:
logger.info('Unknown Emby lookup URI: {}'.format(uri))
Expand Down
38 changes: 38 additions & 0 deletions mopidy_emby/remote.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import unicode_literals

import hashlib

import logging

from collections import OrderedDict, defaultdict

from urllib import urlencode
from urllib2 import quote
from urlparse import parse_qs, urljoin, urlsplit, urlunsplit
Expand Down Expand Up @@ -442,6 +445,41 @@ def search(self, query):
albums=albums
)

def lookup_artist(self, artist_id):
"""Lookup all artist tracks and sort them.
:param artist_id: Artist ID
:type artist_id: int
:returns: List of tracks
:rtype: list
"""
url = self.api_url(
(
'/Users/{}/Items?SortOrder=Ascending&ArtistIds={}'
'&Recursive=true&IncludeItemTypes=Audio'
).format(self.user_id, artist_id)
)

items = self.r_get(url)

# sort tracks into album keys
album_dict = defaultdict(list)
for track in items['Items']:
album_dict[track['Album']].append(track)

# order albums in alphabet
album_dict = OrderedDict(sorted(album_dict.items()))

# sort tracks in album dict
tracks = []
for album, track_list in album_dict.items():
track_list.sort(key=lambda k: k['IndexNumber'])

# add tracks to list
tracks.extend(track_list)

return [self.create_track(i) for i in tracks]

@staticmethod
def ticks_to_milliseconds(ticks):
"""Converts Emby track length ticks to milliseconds.
Expand Down
Empty file removed tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def libraryprovider(backend_mock):
}
]
}
backend_mock.remote.lookup_artist.return_value = ['track1', 'track2']

return mopidy_emby.backend.EmbyLibraryProvider(backend_mock)

Expand Down
151 changes: 151 additions & 0 deletions tests/data/lookup_artist0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"Items": [
{
"Album": "Dear You",
"Container": "flac",
"LocationType": "FileSystem",
"ArtistItems": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"ParentLogoItemId": "c35d160230ff74e5cab4d22ea6b37b82",
"ServerId": "1efa5077976bfa92bc71652404f646ec",
"ExtraType": "0",
"Type": "Audio",
"ParentIndexNumber": 1,
"PlayAccess": "Full",
"ImageTags": {
"Primary": "abc26237c6174628bb167bfdcfc1311c"
},
"IsFolder": false,
"ParentLogoImageTag": "331cbd04916aa8e6403a025fe5b495d8",
"AlbumPrimaryImageTag": "cd514782edae76b587113f5c68050076",
"PremiereDate": "1995-09-12T00:00:00.0000000Z",
"ProductionYear": 1995,
"BackdropImageTags": [],
"Name": "Sluttering (May 4th)",
"Artists": [
"Jawbreaker"
],
"MediaType": "Audio",
"AlbumArtist": "Jawbreaker",
"UserData": {
"PlayCount": 0,
"Key": "MusicAlbum-MusicBrainzReleaseGroup-2a4d791d-b328-3212-8bef-edc98c7de3f70001 - 0011 - ",
"PlaybackPositionTicks": 0,
"IsFavorite": false,
"Played": false
},
"IndexNumber": 11,
"AlbumId": "a747de6e603c4b6bf6c410b939f6558b",
"RunTimeTicks": 2541070000,
"AlbumArtists": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"Id": "057801bc10cf08ce96e1e19bf98c407f"
},
{
"Album": "Dear You",
"Container": "flac",
"LocationType": "FileSystem",
"ArtistItems": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"ParentLogoItemId": "c35d160230ff74e5cab4d22ea6b37b82",
"ServerId": "1efa5077976bfa92bc71652404f646ec",
"ExtraType": "0",
"Type": "Audio",
"ParentIndexNumber": 1,
"PlayAccess": "Full",
"ImageTags": {
"Primary": "a10e414b7e6110155cef4a408a7a3e38"
},
"IsFolder": false,
"ParentLogoImageTag": "331cbd04916aa8e6403a025fe5b495d8",
"AlbumPrimaryImageTag": "cd514782edae76b587113f5c68050076",
"PremiereDate": "1995-09-12T00:00:00.0000000Z",
"ProductionYear": 1995,
"BackdropImageTags": [],
"Name": "Bad Scene, Everyone\u2019s Fault",
"Artists": [
"Jawbreaker"
],
"MediaType": "Audio",
"AlbumArtist": "Jawbreaker",
"UserData": {
"PlayCount": 0,
"Key": "MusicAlbum-MusicBrainzReleaseGroup-2a4d791d-b328-3212-8bef-edc98c7de3f70001 - 0010 - ",
"PlaybackPositionTicks": 0,
"IsFavorite": false,
"Played": false
},
"IndexNumber": 10,
"AlbumId": "a747de6e603c4b6bf6c410b939f6558b",
"RunTimeTicks": 1311330000,
"AlbumArtists": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"Id": "0a24ce6c243f2f3a81fa0f99625630b4"
},
{
"Album": "24 Hour Revenge Therapy",
"Container": "flac",
"LocationType": "FileSystem",
"ArtistItems": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"ParentLogoItemId": "c35d160230ff74e5cab4d22ea6b37b82",
"ServerId": "1efa5077976bfa92bc71652404f646ec",
"Type": "Audio",
"ParentIndexNumber": 1,
"PlayAccess": "Full",
"AlbumId": "332a27ea1a1c66925f5419dcccb44bfa",
"IsFolder": false,
"ParentLogoImageTag": "331cbd04916aa8e6403a025fe5b495d8",
"AlbumPrimaryImageTag": "0f4f411bf873b974febc630f11255193",
"ProductionYear": 1994,
"BackdropImageTags": [],
"Name": "The Boat Dreams From The Hill",
"Artists": [
"Jawbreaker"
],
"MediaType": "Audio",
"AlbumArtist": "Jawbreaker",
"UserData": {
"IsFavorite": false,
"Played": true,
"LastPlayedDate": "2016-07-28T12:50:20.2712960Z",
"PlaybackPositionTicks": 0,
"Key": "MusicAlbum-MusicBrainzReleaseGroup-87f60917-1ce9-3e93-92c3-a3e4184e46ca0001 - 0001 - ",
"PlayCount": 3
},
"IndexNumber": 1,
"ImageTags": {
"Primary": "39d33a64f930ef7c3ed6339de6127628"
},
"RunTimeTicks": 1598400000,
"AlbumArtists": [
{
"Name": "Jawbreaker",
"Id": "c35d160230ff74e5cab4d22ea6b37b82"
}
],
"Id": "05321ccb30ff9e43bf8070cd5f70c783"
}
],
"TotalRecordCount": 3
}
15 changes: 1 addition & 14 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,7 @@ def test_browse(uri, expected, libraryprovider):
uri='emby:track:eb6c305bdb1e40d3b46909473c22d906'
)
]),
('emby:artist:123', [
Track(
album=Album(
artists=[
Artist(name='American Football')
],
name='American Football'),
artists=[Artist(name='American Football')],
length=241162,
name='The One With the Tambourine',
track_no=1,
uri='emby:track:eb6c305bdb1e40d3b46909473c22d906'
)
]),
('emby:artist:123', ['track1', 'track2']),
('emby:track', [])
])
def test_lookup_uri(uri, expected, libraryprovider):
Expand Down
54 changes: 54 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,57 @@ def test__get_search_exception(r_get_mock, emby_client):
emby_client._get_search('foo', 'bar')

assert 'Emby search: no itemtype foo' in str(execinfo.value)


@mock.patch('mopidy_emby.backend.EmbyHandler.r_get')
def test_lookup_artist(r_get_mock, emby_client):
with open('tests/data/lookup_artist0.json', 'r') as f:
r_get_mock.return_value = json.load(f)

assert emby_client.lookup_artist(0) == [
Track(
album=Album(
artists=[Artist(name=u'Jawbreaker')],
name=u'24 Hour Revenge Therapy'
),
artists=[
Artist(
name=u'Jawbreaker'
)
],
length=159840,
name=u'The Boat Dreams From The Hill',
track_no=1,
uri='emby:track:05321ccb30ff9e43bf8070cd5f70c783'
),
Track(
album=Album(
artists=[Artist(name=u'Jawbreaker')],
name=u'Dear You'
),
artists=[
Artist(
name=u'Jawbreaker'
)
],
length=131133,
name=u'Bad Scene, Everyone\u2019s Fault',
track_no=10,
uri='emby:track:0a24ce6c243f2f3a81fa0f99625630b4'
),
Track(
album=Album(
artists=[Artist(name=u'Jawbreaker')],
name=u'Dear You'
),
artists=[
Artist(
name=u'Jawbreaker'
)
],
length=254107,
name=u'Sluttering (May 4th)',
track_no=11,
uri='emby:track:057801bc10cf08ce96e1e19bf98c407f'
)
]
17 changes: 9 additions & 8 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
[tox]
envlist = py27, flake8
envlist =
py27
flake8

[testenv]
basepython = python2.7
usedevelop = true
sitepackages = true
deps =
mock
pytest
pytest-cov
pytest-mock
pytest-xdist
commands =
py.test \
--basetemp={envtmpdir} \
--cov=mopidy_emby --cov-report=term-missing \
{posargs}
py.test --cov=mopidy_emby --cov-report=term tests/

[testenv:flake8]
basepython = python2.7
deps =
flake8
flake8-import-order
skip_install = true
commands = flake8
commands =
flake8 mopidy_emby/

0 comments on commit e4ed43c

Please sign in to comment.