From 5fbf3a8217d4ee86428c8499ad65039e5072185a Mon Sep 17 00:00:00 2001 From: Rato Date: Sat, 23 Feb 2019 20:56:37 +0100 Subject: [PATCH] Added suggested_expected method to the API to support apps that uses guessit as a library --- guessit/api.py | 31 +++++++++++++++++++++++++++++++ guessit/test/suggested.json | 21 +++++++++++++++++++++ guessit/test/test_api.py | 11 +++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 guessit/test/suggested.json diff --git a/guessit/api.py b/guessit/api.py index 3bf5aae..8e30634 100644 --- a/guessit/api.py +++ b/guessit/api.py @@ -82,6 +82,19 @@ def properties(options=None): return default_api.properties(options) +def suggested_expected(titles, options=None): + """ + Return a list of suggested titles to be used as `expected_title` based on the list of titles + :param titles: the filename or release name + :type titles: list|set|dict + :param options: + :type options: str|dict + :return: + :rtype: list of str + """ + return default_api.suggested_expected(titles, options) + + class GuessItApi(object): """ An api class that can be configured with custom Rebulk configuration. @@ -228,5 +241,23 @@ class GuessItApi(object): ordered = self.rebulk.customize_properties(ordered) return ordered + def suggested_expected(self, titles, options=None): + """ + Return a list of suggested titles to be used as `expected_title` based on the list of titles + :param titles: the filename or release name + :type titles: list|set|dict + :param options: + :type options: str|dict + :return: + :rtype: list of str + """ + suggested = [] + for title in titles: + guess = self.guessit(title, options) + if len(guess) != 2 or 'title' not in guess: + suggested.append(title) + + return suggested + default_api = GuessItApi() diff --git a/guessit/test/suggested.json b/guessit/test/suggested.json new file mode 100644 index 0000000..dc838ad --- /dev/null +++ b/guessit/test/suggested.json @@ -0,0 +1,21 @@ +{ + "titles": [ + "13 Reasons Why", + "Star Wars: Episode VII - The Force Awakens", + "3%", + "The 100", + "3 Percent", + "This is Us", + "Open Season 2", + "Game of Thrones", + "The X-Files", + "11.22.63" + ], + "suggested": [ + "13 Reasons Why", + "Star Wars: Episode VII - The Force Awakens", + "The 100", + "Open Season 2", + "11.22.63" + ] +} \ No newline at end of file diff --git a/guessit/test/test_api.py b/guessit/test/test_api.py index 7ecad9a..391dbce 100644 --- a/guessit/test/test_api.py +++ b/guessit/test/test_api.py @@ -1,14 +1,14 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name, pointless-string-statement - +import json import os import sys import pytest import six -from ..api import guessit, properties, GuessitException +from ..api import guessit, properties, suggested_expected, GuessitException __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) @@ -74,3 +74,10 @@ def test_exception(): assert "An internal error has occured in guessit" in str(excinfo.value) assert "Guessit Exception Report" in str(excinfo.value) assert "Please report at https://github.com/guessit-io/guessit/issues" in str(excinfo.value) + + +def test_suggested_expected(): + with open(os.path.join(__location__, 'suggested.json'), 'r') as f: + content = json.load(f) + actual = suggested_expected(content['titles']) + assert actual == content['suggested']