Skip to content

Commit 2267378

Browse files
authored
Merge pull request #472 from pkkid/matching_fix
matching_fix
2 parents 7905ff0 + f058668 commit 2267378

File tree

2 files changed

+127
-18
lines changed

2 files changed

+127
-18
lines changed

plexapi/base.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -519,40 +519,49 @@ def matches(self, agent=None, title=None, year=None, language=None):
519519
key = '/library/metadata/%s/matches' % self.ratingKey
520520
params = {'manual': 1}
521521

522-
if any([agent, title, year, language]):
523-
if title is None:
524-
params['title'] = self.title
525-
else:
526-
params['title'] = title
522+
if agent and not any([title, year, language]):
523+
params['language'] = self.section().language
524+
params['agent'] = utils.getAgentIdentifier(self.section(), agent)
525+
else:
526+
if any(x is not None for x in [agent, title, year, language]):
527+
if title is None:
528+
params['title'] = self.title
529+
else:
530+
params['title'] = title
527531

528-
if year is None:
529-
params['year'] = self.year
530-
else:
531-
params['year'] = year
532+
if year is None:
533+
params['year'] = self.year
534+
else:
535+
params['year'] = year
532536

533-
params['language'] = language or self.section().language
537+
params['language'] = language or self.section().language
534538

535-
if agent is None:
536-
params['agent'] = self.section().agent
537-
else:
538-
params['agent'] = utils.getAgentIdentifier(self.section(), agent)
539+
if agent is None:
540+
params['agent'] = self.section().agent
541+
else:
542+
params['agent'] = utils.getAgentIdentifier(self.section(), agent)
539543

540-
key = key + '?' + urlencode(params)
544+
key = key + '?' + urlencode(params)
541545
data = self._server.query(key, method=self._server._session.get)
542-
return self.findItems(data)
546+
return self.findItems(data, initpath=key)
543547

544-
def fixMatch(self, searchResult=None, auto=False):
548+
def fixMatch(self, searchResult=None, auto=False, agent=None):
545549
""" Use match result to update show metadata.
546550
547551
Parameters:
548552
auto (bool): True uses first match from matches
549553
False allows user to provide the match
550554
searchResult (:class:`~plexapi.media.SearchResult`): Search result from
551555
~plexapi.base.matches()
556+
agent (str): Agent name to be used (imdb, thetvdb, themoviedb, etc.)
552557
"""
553558
key = '/library/metadata/%s/match' % self.ratingKey
554559
if auto:
555-
searchResult = self.matches()[0]
560+
autoMatch = self.matches(agent=agent)
561+
if autoMatch:
562+
searchResult = autoMatch[0]
563+
else:
564+
raise NotFound('No matches found using this agent: (%s:%s)' % (agent, autoMatch))
556565
elif not searchResult:
557566
raise NotFound('fixMatch() requires either auto=True or '
558567
'searchResult=:class:`~plexapi.media.SearchResult`.')

tests/test_video.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77
from plexapi.exceptions import BadRequest, NotFound
8+
from plexapi.compat import quote_plus
89

910
from . import conftest as utils
1011

@@ -349,6 +350,105 @@ def test_video_Movie_history(movie):
349350
movie.markUnwatched()
350351

351352

353+
def test_video_Movie_match(movies):
354+
sectionAgent = movies.agent
355+
sectionAgents = [agent.identifier for agent in movies.agents() if agent.shortIdentifier != 'none']
356+
sectionAgents.remove(sectionAgent)
357+
altAgent = sectionAgents[0]
358+
359+
movie = movies.all()[0]
360+
title = movie.title
361+
year = str(movie.year)
362+
titleUrlEncode = quote_plus(title)
363+
364+
def parse_params(key):
365+
params = key.split('?', 1)[1]
366+
params = params.split("&")
367+
return {x.split("=")[0]: x.split("=")[1] for x in params}
368+
369+
results = movie.matches(title="", year="")
370+
if results:
371+
initpath = results[0]._initpath
372+
assert initpath.startswith(movie.key)
373+
params = initpath.split(movie.key)[1]
374+
parsedParams = parse_params(params)
375+
assert parsedParams.get('manual') == '1'
376+
assert parsedParams.get('title') == ""
377+
assert parsedParams.get('year') == ""
378+
assert parsedParams.get('agent') == sectionAgent
379+
else:
380+
assert len(results) == 0
381+
382+
results = movie.matches(title=title, year="", agent=sectionAgent)
383+
if results:
384+
initpath = results[0]._initpath
385+
assert initpath.startswith(movie.key)
386+
params = initpath.split(movie.key)[1]
387+
parsedParams = parse_params(params)
388+
assert parsedParams.get('manual') == '1'
389+
assert parsedParams.get('title') == titleUrlEncode
390+
assert parsedParams.get('year') == ""
391+
assert parsedParams.get('agent') == sectionAgent
392+
else:
393+
assert len(results) == 0
394+
395+
results = movie.matches(title=title, agent=sectionAgent)
396+
if results:
397+
initpath = results[0]._initpath
398+
assert initpath.startswith(movie.key)
399+
params = initpath.split(movie.key)[1]
400+
parsedParams = parse_params(params)
401+
assert parsedParams.get('manual') == '1'
402+
assert parsedParams.get('title') == titleUrlEncode
403+
assert parsedParams.get('year') == year
404+
assert parsedParams.get('agent') == sectionAgent
405+
else:
406+
assert len(results) == 0
407+
408+
results = movie.matches(title="", year="")
409+
if results:
410+
initpath = results[0]._initpath
411+
assert initpath.startswith(movie.key)
412+
params = initpath.split(movie.key)[1]
413+
parsedParams = parse_params(params)
414+
assert parsedParams.get('manual') == '1'
415+
assert parsedParams.get('agent') == sectionAgent
416+
else:
417+
assert len(results) == 0
418+
419+
results = movie.matches(title="", year="", agent=altAgent)
420+
if results:
421+
initpath = results[0]._initpath
422+
assert initpath.startswith(movie.key)
423+
params = initpath.split(movie.key)[1]
424+
parsedParams = parse_params(params)
425+
assert parsedParams.get('manual') == '1'
426+
assert parsedParams.get('agent') == altAgent
427+
else:
428+
assert len(results) == 0
429+
430+
results = movie.matches(agent=altAgent)
431+
if results:
432+
initpath = results[0]._initpath
433+
assert initpath.startswith(movie.key)
434+
params = initpath.split(movie.key)[1]
435+
parsedParams = parse_params(params)
436+
assert parsedParams.get('manual') == '1'
437+
assert parsedParams.get('agent') == altAgent
438+
else:
439+
assert len(results) == 0
440+
441+
results = movie.matches()
442+
if results:
443+
initpath = results[0]._initpath
444+
assert initpath.startswith(movie.key)
445+
params = initpath.split(movie.key)[1]
446+
parsedParams = parse_params(params)
447+
assert parsedParams.get('manual') == '1'
448+
else:
449+
assert len(results) == 0
450+
451+
352452
def test_video_Show(show):
353453
assert show.title == "Game of Thrones"
354454

0 commit comments

Comments
 (0)