From 8309bf14e34e17a871e0f496c27cf431aaba18e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Sat, 18 Feb 2023 12:59:27 +0100 Subject: [PATCH] feat(week): add week property --- docs/properties.md | 4 ++++ guessit/config/options.json | 3 +++ guessit/rules/common/date.py | 5 +++++ guessit/rules/properties/date.py | 17 +++++++++++++++-- guessit/test/rules/episodes.yml | 7 +++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/docs/properties.md b/docs/properties.md index 1142010c..dc3f1286 100644 --- a/docs/properties.md +++ b/docs/properties.md @@ -42,6 +42,10 @@ Main properties Year of movie (or episode). +- **week** + + Week number, from 1 to 52, of episode. + - **release\_group** Name of (non)scene group that released the file. diff --git a/guessit/config/options.json b/guessit/config/options.json index 9adac64c..37b0df21 100644 --- a/guessit/config/options.json +++ b/guessit/config/options.json @@ -839,6 +839,9 @@ "Yahoo": "YHOO", "YouTube Red": "RED", "ZDF": "ZDF" + }, + "date": { + "week_words": ["week"] } } } diff --git a/guessit/rules/common/date.py b/guessit/rules/common/date.py index 1e114569..5850e9d4 100644 --- a/guessit/rules/common/date.py +++ b/guessit/rules/common/date.py @@ -34,6 +34,11 @@ def valid_year(year): return 1920 <= year < 2030 +def valid_week(week): + """Check if number is a valid week""" + return 1 <= week < 53 + + def _is_int(string): """ Check if the input string is an integer diff --git a/guessit/rules/properties/date.py b/guessit/rules/properties/date.py index e50cdfa3..4f322c30 100644 --- a/guessit/rules/properties/date.py +++ b/guessit/rules/properties/date.py @@ -1,13 +1,17 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ -date and year properties +date, week and year properties """ +import re + from rebulk import Rebulk, RemoveMatch, Rule -from ..common.date import search_date, valid_year +from ..common import dash +from ..common.date import search_date, valid_year, valid_week from ..common.pattern import is_disabled from ..common.validators import seps_surround +from ...reutils import build_or_pattern def date(config): # pylint:disable=unused-argument @@ -28,6 +32,15 @@ def date(config): # pylint:disable=unused-argument else '__default__', validator=lambda match: seps_surround(match) and valid_year(match.value)) + rebulk.regex(build_or_pattern(config.get('week_words')) + r"-?(\d{1,2})", + name="week", formatter=int, + children=True, + flags=re.IGNORECASE, abbreviations=[dash], + conflict_solver=lambda match, other: other + if other.name in ('episode', 'season') and len(other.raw) < len(match.raw) + else '__default__', + validator=lambda match: seps_surround(match) and valid_week(match.value)) + def date_functional(string, context): # pylint:disable=inconsistent-return-statements """ Search for date in the string and retrieves match diff --git a/guessit/test/rules/episodes.yml b/guessit/test/rules/episodes.yml index d56a486d..7eff1e72 100644 --- a/guessit/test/rules/episodes.yml +++ b/guessit/test/rules/episodes.yml @@ -330,7 +330,14 @@ season: 2010 episode: 2 +? Show Name - S32-Dummy 45-Ep 6478 +: title: Show Name + episode_title: Dummy 45 + season: 32 + episode: 6478 + ? Show Name - S32-Week 45-Ep 6478 : title: Show Name season: 32 + week: 45 episode: 6478