2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-05-26 02:29:22 -04:00
|
|
|
# GNU General Public License for more details.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-15 01:53:32 -04:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2014-07-15 14:40:40 -04:00
|
|
|
import os
|
2014-07-08 18:17:34 -04:00
|
|
|
import time
|
2014-05-31 06:35:11 -04:00
|
|
|
import re
|
2014-03-10 01:18:05 -04:00
|
|
|
import datetime
|
|
|
|
import os.path
|
|
|
|
import regexes
|
|
|
|
import sickbeard
|
|
|
|
|
2014-07-24 14:16:59 -04:00
|
|
|
from sickbeard import logger, helpers, scene_numbering, common, exceptions, scene_exceptions, encodingKludge as ek, db
|
2014-04-30 18:07:18 -04:00
|
|
|
from dateutil import parser
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-07-18 14:47:19 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class NameParser(object):
|
2014-07-27 06:59:21 -04:00
|
|
|
ALL_REGEX = 0
|
|
|
|
NORMAL_REGEX = 1
|
|
|
|
SPORTS_REGEX = 2
|
|
|
|
ANIME_REGEX = 3
|
2014-04-28 05:15:29 -04:00
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
def __init__(self, file_name=True, showObj=None, tryIndexers=False, convert=False,
|
2014-06-22 14:33:58 -04:00
|
|
|
naming_pattern=False):
|
2014-05-26 02:29:22 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.file_name = file_name
|
2014-05-31 06:35:11 -04:00
|
|
|
self.showObj = showObj
|
2014-07-24 00:44:11 -04:00
|
|
|
self.tryIndexers = tryIndexers
|
2014-05-31 06:35:11 -04:00
|
|
|
self.convert = convert
|
2014-06-19 21:13:07 -04:00
|
|
|
self.naming_pattern = naming_pattern
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-18 03:03:29 -04:00
|
|
|
if self.showObj and not self.showObj.is_anime and not self.showObj.is_sports:
|
2014-07-27 06:59:21 -04:00
|
|
|
self._compile_regexes(self.NORMAL_REGEX)
|
2014-07-14 22:00:53 -04:00
|
|
|
elif self.showObj and self.showObj.is_anime:
|
2014-07-27 06:59:21 -04:00
|
|
|
self._compile_regexes(self.ANIME_REGEX)
|
2014-07-14 22:00:53 -04:00
|
|
|
elif self.showObj and self.showObj.is_sports:
|
2014-07-27 06:59:21 -04:00
|
|
|
self._compile_regexes(self.SPORTS_REGEX)
|
|
|
|
else:
|
|
|
|
self._compile_regexes(self.ALL_REGEX)
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def clean_series_name(self, series_name):
|
|
|
|
"""Cleans up series name by removing any . and _
|
|
|
|
characters, along with any trailing hyphens.
|
2014-05-28 20:44:00 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
Is basically equivalent to replacing all _ and . with a
|
|
|
|
space, but handles decimal numbers in string, for example:
|
2014-05-28 20:44:00 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
>>> cleanRegexedSeriesName("an.example.1.0.test")
|
|
|
|
'an example 1.0 test'
|
|
|
|
>>> cleanRegexedSeriesName("an_example_1.0_test")
|
|
|
|
'an example 1.0 test'
|
2014-05-28 20:44:00 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
Stolen from dbr's tvnamer
|
|
|
|
"""
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
series_name = re.sub("(\D)\.(?!\s)(\D)", "\\1 \\2", series_name)
|
2014-03-25 01:57:24 -04:00
|
|
|
series_name = re.sub("(\d)\.(\d{4})", "\\1 \\2", series_name) # if it ends in a year then don't keep the dot
|
2014-03-10 01:18:05 -04:00
|
|
|
series_name = re.sub("(\D)\.(?!\s)", "\\1 ", series_name)
|
|
|
|
series_name = re.sub("\.(?!\s)(\D)", " \\1", series_name)
|
|
|
|
series_name = series_name.replace("_", " ")
|
|
|
|
series_name = re.sub("-$", "", series_name)
|
2014-03-25 01:57:24 -04:00
|
|
|
series_name = re.sub("^\[.*\]", "", series_name)
|
2014-03-10 01:18:05 -04:00
|
|
|
return series_name.strip()
|
|
|
|
|
2014-04-28 05:15:29 -04:00
|
|
|
def _compile_regexes(self, regexMode):
|
2014-07-14 22:00:53 -04:00
|
|
|
if regexMode == self.SPORTS_REGEX:
|
2014-05-07 03:50:49 -04:00
|
|
|
logger.log(u"Using SPORTS regexs", logger.DEBUG)
|
2014-05-26 02:29:22 -04:00
|
|
|
uncompiled_regex = [regexes.sports_regexs]
|
|
|
|
elif regexMode == self.ANIME_REGEX:
|
|
|
|
logger.log(u"Using ANIME regexs", logger.DEBUG)
|
2014-07-24 04:27:35 -04:00
|
|
|
uncompiled_regex = [regexes.anime_regexes, regexes.normal_regexes]
|
2014-07-27 06:59:21 -04:00
|
|
|
elif regexMode == self.NORMAL_REGEX:
|
|
|
|
logger.log(u"Using NORMAL regexs", logger.DEBUG)
|
2014-05-26 02:29:22 -04:00
|
|
|
uncompiled_regex = [regexes.normal_regexes]
|
2014-07-27 06:59:21 -04:00
|
|
|
else:
|
|
|
|
logger.log(u"Using ALL regexes", logger.DEBUG)
|
|
|
|
uncompiled_regex = [regexes.normal_regexes, regexes.sports_regexs, regexes.anime_regexes]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
self.compiled_regexes = []
|
2014-05-26 02:29:22 -04:00
|
|
|
for regexItem in uncompiled_regex:
|
2014-07-24 14:16:59 -04:00
|
|
|
for cur_pattern_num, (cur_pattern_name, cur_pattern) in enumerate(regexItem):
|
2014-07-14 22:00:53 -04:00
|
|
|
try:
|
|
|
|
cur_regex = re.compile(cur_pattern, re.VERBOSE | re.IGNORECASE)
|
|
|
|
except re.error, errormsg:
|
|
|
|
logger.log(u"WARNING: Invalid episode_pattern, %s. %s" % (errormsg, cur_pattern))
|
|
|
|
else:
|
2014-07-27 06:59:21 -04:00
|
|
|
self.compiled_regexes.append((cur_pattern_num, cur_pattern_name, cur_regex))
|
2014-06-24 21:57:05 -04:00
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
def _parse_string(self, name):
|
2014-05-28 17:13:29 -04:00
|
|
|
if not name:
|
|
|
|
return
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
matches = []
|
|
|
|
bestResult = None
|
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
for (cur_regex_num, cur_regex_name, cur_regex) in self.compiled_regexes:
|
|
|
|
match = cur_regex.match(name)
|
2014-06-22 14:33:58 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if not match:
|
|
|
|
continue
|
2014-05-17 05:27:17 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
result = ParseResult(name)
|
|
|
|
result.which_regex = [cur_regex_name]
|
|
|
|
result.score = 0 - cur_regex_num
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
named_groups = match.groupdict().keys()
|
2014-07-19 09:09:16 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'series_name' in named_groups:
|
|
|
|
result.series_name = match.group('series_name')
|
|
|
|
if result.series_name:
|
|
|
|
result.series_name = self.clean_series_name(result.series_name)
|
|
|
|
result.score += 1
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'season_num' in named_groups:
|
|
|
|
tmp_season = int(match.group('season_num'))
|
|
|
|
if cur_regex_name == 'bare' and tmp_season in (19, 20):
|
|
|
|
continue
|
|
|
|
result.season_number = tmp_season
|
|
|
|
result.score += 1
|
2014-07-19 08:06:18 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'ep_num' in named_groups:
|
|
|
|
ep_num = self._convert_number(match.group('ep_num'))
|
|
|
|
if 'extra_ep_num' in named_groups and match.group('extra_ep_num'):
|
|
|
|
result.episode_numbers = range(ep_num, self._convert_number(match.group('extra_ep_num')) + 1)
|
|
|
|
result.score += 1
|
|
|
|
else:
|
|
|
|
result.episode_numbers = [ep_num]
|
|
|
|
result.score += 1
|
|
|
|
|
|
|
|
if 'ep_ab_num' in named_groups:
|
|
|
|
ep_ab_num = self._convert_number(match.group('ep_ab_num'))
|
|
|
|
if 'extra_ab_ep_num' in named_groups and match.group('extra_ab_ep_num'):
|
|
|
|
result.ab_episode_numbers = range(ep_ab_num,
|
|
|
|
self._convert_number(match.group('extra_ab_ep_num')) + 1)
|
2014-07-24 14:16:59 -04:00
|
|
|
result.score += 1
|
2014-07-27 06:59:21 -04:00
|
|
|
else:
|
|
|
|
result.ab_episode_numbers = [ep_ab_num]
|
|
|
|
result.score += 1
|
2014-05-31 06:35:11 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'sports_event_id' in named_groups:
|
|
|
|
sports_event_id = match.group('sports_event_id')
|
|
|
|
if sports_event_id:
|
|
|
|
result.sports_event_id = int(match.group('sports_event_id'))
|
2014-07-24 14:16:59 -04:00
|
|
|
result.score += 1
|
2014-05-31 06:35:11 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'sports_event_name' in named_groups:
|
|
|
|
result.sports_event_name = match.group('sports_event_name')
|
|
|
|
if result.sports_event_name:
|
|
|
|
result.sports_event_name = self.clean_series_name(result.sports_event_name)
|
2014-07-24 14:16:59 -04:00
|
|
|
result.score += 1
|
2014-05-31 06:35:11 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'sports_air_date' in named_groups:
|
|
|
|
sports_air_date = match.group('sports_air_date')
|
|
|
|
if result.show and result.show.is_sports:
|
|
|
|
try:
|
|
|
|
result.sports_air_date = parser.parse(sports_air_date, fuzzy=True).date()
|
2014-07-14 22:00:53 -04:00
|
|
|
result.score += 1
|
2014-07-27 06:59:21 -04:00
|
|
|
except:
|
|
|
|
continue
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'air_year' in named_groups and 'air_month' in named_groups and 'air_day' in named_groups:
|
|
|
|
if result.show and result.show.air_by_date:
|
|
|
|
year = int(match.group('air_year'))
|
|
|
|
month = int(match.group('air_month'))
|
|
|
|
day = int(match.group('air_day'))
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
try:
|
|
|
|
dtStr = '%s-%s-%s' % (year, month, day)
|
|
|
|
result.air_date = datetime.datetime.strptime(dtStr, "%Y-%m-%d").date()
|
|
|
|
result.score += 1
|
|
|
|
except:
|
2014-07-24 14:16:59 -04:00
|
|
|
continue
|
2014-05-26 02:29:22 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
if 'extra_info' in named_groups:
|
|
|
|
tmp_extra_info = match.group('extra_info')
|
2014-05-26 02:29:22 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
# Show.S04.Special or Show.S05.Part.2.Extras is almost certainly not every episode in the season
|
|
|
|
if tmp_extra_info and cur_regex_name == 'season_only' and re.search(
|
|
|
|
r'([. _-]|^)(special|extra)s?\w*([. _-]|$)', tmp_extra_info, re.I):
|
|
|
|
continue
|
|
|
|
result.extra_info = tmp_extra_info
|
|
|
|
result.score += 1
|
|
|
|
|
|
|
|
if 'release_group' in named_groups:
|
|
|
|
result.release_group = match.group('release_group')
|
|
|
|
result.score += 1
|
|
|
|
|
2014-07-22 00:53:32 -04:00
|
|
|
if 'version' in named_groups:
|
|
|
|
# assigns version to anime file if detected using anime regex. Non-anime regex receives -1
|
|
|
|
version = match.group('version')
|
|
|
|
if version:
|
|
|
|
result.version = version
|
|
|
|
else:
|
|
|
|
result.version = 1
|
|
|
|
else:
|
|
|
|
result.version = -1
|
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
|
|
|
|
matches.append(result)
|
2014-06-01 19:18:53 -04:00
|
|
|
|
|
|
|
if len(matches):
|
2014-07-14 22:00:53 -04:00
|
|
|
# pick best match with highest score based on placement
|
|
|
|
bestResult = max(sorted(matches, reverse=True, key=lambda x: x.which_regex), key=lambda x: x.score)
|
2014-06-01 19:18:53 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
show = None
|
|
|
|
if not self.naming_pattern:
|
|
|
|
# try and create a show object for this result
|
|
|
|
show = helpers.get_show(bestResult.series_name, self.tryIndexers)
|
|
|
|
|
|
|
|
# confirm passed in show object indexer id matches result show object indexer id
|
|
|
|
if show:
|
|
|
|
if self.showObj and show.indexerid != self.showObj.indexerid:
|
|
|
|
show = None
|
|
|
|
bestResult.show = show
|
|
|
|
elif not show and self.showObj:
|
2014-07-24 14:16:59 -04:00
|
|
|
bestResult.show = self.showObj
|
|
|
|
|
2014-07-24 04:27:35 -04:00
|
|
|
# if this is a naming pattern test or result doesn't have a show object then return best result
|
2014-07-24 14:16:59 -04:00
|
|
|
if not bestResult.show or self.naming_pattern:
|
2014-07-24 04:27:35 -04:00
|
|
|
return bestResult
|
2014-06-07 07:06:21 -04:00
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
# get quality
|
|
|
|
bestResult.quality = common.Quality.nameQuality(name, bestResult.show.is_anime)
|
|
|
|
|
2014-07-24 04:27:35 -04:00
|
|
|
new_episode_numbers = []
|
|
|
|
new_season_numbers = []
|
|
|
|
new_absolute_numbers = []
|
2014-07-15 06:18:34 -04:00
|
|
|
|
2014-07-24 14:16:59 -04:00
|
|
|
# if we have an air-by-date show then get the real season/episode numbers
|
|
|
|
if bestResult.is_air_by_date or bestResult.is_sports:
|
|
|
|
airdate = bestResult.air_date.toordinal() if bestResult.air_date else bestResult.sports_air_date.toordinal()
|
|
|
|
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
sql_result = myDB.select(
|
|
|
|
"SELECT season, episode FROM tv_episodes WHERE showid = ? and indexer = ? and airdate = ?",
|
|
|
|
[bestResult.show.indexerid, bestResult.show.indexer, airdate])
|
|
|
|
|
|
|
|
if sql_result:
|
|
|
|
season_number = int(sql_result[0][0])
|
|
|
|
episode_numbers = [int(sql_result[0][1])]
|
|
|
|
|
|
|
|
for epNo in episode_numbers:
|
|
|
|
s = season_number
|
|
|
|
e = epNo
|
|
|
|
|
|
|
|
if self.convert:
|
|
|
|
(s, e) = scene_numbering.get_indexer_numbering(bestResult.show.indexerid,
|
|
|
|
bestResult.show.indexer,
|
|
|
|
season_number,
|
|
|
|
epNo)
|
|
|
|
new_episode_numbers.append(e)
|
|
|
|
new_season_numbers.append(s)
|
|
|
|
|
|
|
|
elif bestResult.show.is_anime and len(bestResult.ab_episode_numbers):
|
2014-07-24 04:27:35 -04:00
|
|
|
scene_season = scene_exceptions.get_scene_exception_by_name(bestResult.series_name)[1]
|
|
|
|
for epAbsNo in bestResult.ab_episode_numbers:
|
|
|
|
a = epAbsNo
|
2014-07-15 06:11:54 -04:00
|
|
|
|
2014-07-24 04:27:35 -04:00
|
|
|
if self.convert:
|
|
|
|
a = scene_numbering.get_indexer_absolute_numbering(bestResult.show.indexerid,
|
|
|
|
bestResult.show.indexer, epAbsNo,
|
|
|
|
True, scene_season)
|
2014-07-24 14:16:59 -04:00
|
|
|
|
|
|
|
(s, e) = helpers.get_all_episodes_from_absolute_number(bestResult.show, [a])
|
|
|
|
|
|
|
|
new_absolute_numbers.append(a)
|
|
|
|
new_episode_numbers.extend(e)
|
|
|
|
new_season_numbers.append(s)
|
2014-07-24 04:27:35 -04:00
|
|
|
|
|
|
|
elif bestResult.season_number and len(bestResult.episode_numbers):
|
|
|
|
for epNo in bestResult.episode_numbers:
|
|
|
|
s = bestResult.season_number
|
|
|
|
e = epNo
|
|
|
|
|
|
|
|
if self.convert:
|
|
|
|
(s, e) = scene_numbering.get_indexer_numbering(bestResult.show.indexerid,
|
|
|
|
bestResult.show.indexer,
|
|
|
|
bestResult.season_number,
|
|
|
|
epNo)
|
|
|
|
if bestResult.show.is_anime:
|
|
|
|
a = helpers.get_absolute_number_from_season_and_episode(bestResult.show, s, e)
|
|
|
|
if a:
|
|
|
|
new_absolute_numbers.append(a)
|
|
|
|
|
|
|
|
new_episode_numbers.append(e)
|
|
|
|
new_season_numbers.append(s)
|
|
|
|
|
|
|
|
# need to do a quick sanity check heregex. It's possible that we now have episodes
|
|
|
|
# from more than one season (by tvdb numbering), and this is just too much
|
|
|
|
# for sickbeard, so we'd need to flag it.
|
|
|
|
new_season_numbers = list(set(new_season_numbers)) # remove duplicates
|
|
|
|
if len(new_season_numbers) > 1:
|
|
|
|
raise InvalidNameException("Scene numbering results episodes from "
|
|
|
|
"seasons %s, (i.e. more than one) and "
|
|
|
|
"sickrage does not support this. "
|
|
|
|
"Sorry." % (str(new_season_numbers)))
|
|
|
|
|
|
|
|
# I guess it's possible that we'd have duplicate episodes too, so lets
|
|
|
|
# eliminate them
|
|
|
|
new_episode_numbers = list(set(new_episode_numbers))
|
|
|
|
new_episode_numbers.sort()
|
|
|
|
|
|
|
|
# maybe even duplicate absolute numbers so why not do them as well
|
|
|
|
new_absolute_numbers = list(set(new_absolute_numbers))
|
|
|
|
new_absolute_numbers.sort()
|
|
|
|
|
|
|
|
if len(new_absolute_numbers):
|
|
|
|
bestResult.ab_episode_numbers = new_absolute_numbers
|
|
|
|
|
|
|
|
if len(new_season_numbers) and len(new_episode_numbers):
|
|
|
|
bestResult.episode_numbers = new_episode_numbers
|
|
|
|
bestResult.season_number = new_season_numbers[0]
|
|
|
|
|
|
|
|
if self.convert:
|
|
|
|
logger.log(
|
|
|
|
u"Converted parsed result " + bestResult.original_name + " into " + str(bestResult).decode('utf-8',
|
|
|
|
'xmlcharrefreplace'),
|
|
|
|
logger.DEBUG)
|
2014-07-15 06:11:54 -04:00
|
|
|
|
2014-07-20 01:26:28 -04:00
|
|
|
# CPU sleep
|
|
|
|
time.sleep(0.02)
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
return bestResult
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _combine_results(self, first, second, attr):
|
|
|
|
# if the first doesn't exist then return the second or nothing
|
|
|
|
if not first:
|
|
|
|
if not second:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return getattr(second, attr)
|
|
|
|
|
|
|
|
# if the second doesn't exist then return the first
|
|
|
|
if not second:
|
|
|
|
return getattr(first, attr)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
a = getattr(first, attr)
|
|
|
|
b = getattr(second, attr)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# if a is good use it
|
2014-03-20 14:03:22 -04:00
|
|
|
if a != None or (type(a) == list and len(a)):
|
2014-03-10 01:18:05 -04:00
|
|
|
return a
|
|
|
|
# if not use b (if b isn't set it'll just be default)
|
|
|
|
else:
|
|
|
|
return b
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
def _unicodify(self, obj, encoding="utf-8"):
|
2014-03-10 01:18:05 -04:00
|
|
|
if isinstance(obj, basestring):
|
|
|
|
if not isinstance(obj, unicode):
|
2014-07-14 22:00:53 -04:00
|
|
|
obj = unicode(obj, encoding, 'replace')
|
2014-03-10 01:18:05 -04:00
|
|
|
return obj
|
|
|
|
|
2014-05-28 17:13:29 -04:00
|
|
|
def _convert_number(self, org_number):
|
|
|
|
"""
|
|
|
|
Convert org_number into an integer
|
|
|
|
org_number: integer or representation of a number: string or unicode
|
|
|
|
Try force converting to int first, on error try converting from Roman numerals
|
|
|
|
returns integer or 0
|
|
|
|
"""
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
2014-05-28 17:13:29 -04:00
|
|
|
# try forcing to int
|
|
|
|
if org_number:
|
|
|
|
number = int(org_number)
|
|
|
|
else:
|
|
|
|
number = 0
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
2014-05-28 17:13:29 -04:00
|
|
|
# on error try converting from Roman numerals
|
|
|
|
roman_to_int_map = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100),
|
|
|
|
('XC', 90), ('L', 50), ('XL', 40), ('X', 10),
|
|
|
|
('IX', 9), ('V', 5), ('IV', 4), ('I', 1)
|
2014-03-10 01:18:05 -04:00
|
|
|
)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-28 17:13:29 -04:00
|
|
|
roman_numeral = str(org_number).upper()
|
|
|
|
number = 0
|
|
|
|
index = 0
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-28 17:13:29 -04:00
|
|
|
for numeral, integer in roman_to_int_map:
|
|
|
|
while roman_numeral[index:index + len(numeral)] == numeral:
|
|
|
|
number += integer
|
|
|
|
index += len(numeral)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-28 17:13:29 -04:00
|
|
|
return number
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-26 06:42:34 -04:00
|
|
|
def parse(self, name, cache_result=True):
|
2014-03-10 01:18:05 -04:00
|
|
|
name = self._unicodify(name)
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
if self.naming_pattern:
|
|
|
|
cache_result = False
|
|
|
|
|
2014-07-15 18:01:23 -04:00
|
|
|
cached = name_parser_cache.get(name)
|
2014-03-10 01:18:05 -04:00
|
|
|
if cached:
|
|
|
|
return cached
|
|
|
|
|
|
|
|
# break it into parts if there are any (dirname, file name, extension)
|
2014-07-19 18:23:01 -04:00
|
|
|
dir_name, file_name = ek.ek(os.path.split, name)
|
|
|
|
|
|
|
|
if self.file_name:
|
|
|
|
base_file_name = helpers.remove_extension(file_name)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
base_file_name = file_name
|
|
|
|
|
|
|
|
# set up a result to use
|
|
|
|
final_result = ParseResult(name)
|
|
|
|
|
|
|
|
# try parsing the file name
|
|
|
|
file_name_result = self._parse_string(base_file_name)
|
|
|
|
|
2014-06-19 21:13:07 -04:00
|
|
|
# use only the direct parent dir
|
|
|
|
dir_name = os.path.basename(dir_name)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# parse the dirname for extra info if needed
|
|
|
|
dir_name_result = self._parse_string(dir_name)
|
|
|
|
|
|
|
|
# build the ParseResult object
|
|
|
|
final_result.air_date = self._combine_results(file_name_result, dir_name_result, 'air_date')
|
2014-07-14 22:00:53 -04:00
|
|
|
|
|
|
|
# anime absolute numbers
|
2014-05-26 02:29:22 -04:00
|
|
|
final_result.ab_episode_numbers = self._combine_results(file_name_result, dir_name_result, 'ab_episode_numbers')
|
2014-04-30 08:10:13 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
# sports
|
2014-05-01 23:03:44 -04:00
|
|
|
final_result.sports_event_id = self._combine_results(file_name_result, dir_name_result, 'sports_event_id')
|
|
|
|
final_result.sports_event_name = self._combine_results(file_name_result, dir_name_result, 'sports_event_name')
|
2014-07-14 22:00:53 -04:00
|
|
|
final_result.sports_air_date = self._combine_results(file_name_result, dir_name_result, 'sports_air_date')
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
if not final_result.air_date and not final_result.sports_air_date:
|
2014-03-10 01:18:05 -04:00
|
|
|
final_result.season_number = self._combine_results(file_name_result, dir_name_result, 'season_number')
|
|
|
|
final_result.episode_numbers = self._combine_results(file_name_result, dir_name_result, 'episode_numbers')
|
|
|
|
|
|
|
|
# if the dirname has a release group/show name I believe it over the filename
|
|
|
|
final_result.series_name = self._combine_results(dir_name_result, file_name_result, 'series_name')
|
|
|
|
final_result.extra_info = self._combine_results(dir_name_result, file_name_result, 'extra_info')
|
|
|
|
final_result.release_group = self._combine_results(dir_name_result, file_name_result, 'release_group')
|
2014-07-22 00:53:32 -04:00
|
|
|
final_result.version = self._combine_results(dir_name_result, file_name_result, 'version')
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
final_result.which_regex = []
|
|
|
|
if final_result == file_name_result:
|
|
|
|
final_result.which_regex = file_name_result.which_regex
|
|
|
|
elif final_result == dir_name_result:
|
|
|
|
final_result.which_regex = dir_name_result.which_regex
|
|
|
|
else:
|
|
|
|
if file_name_result:
|
|
|
|
final_result.which_regex += file_name_result.which_regex
|
|
|
|
if dir_name_result:
|
|
|
|
final_result.which_regex += dir_name_result.which_regex
|
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
final_result.show = self._combine_results(file_name_result, dir_name_result, 'show')
|
2014-06-07 07:06:21 -04:00
|
|
|
final_result.quality = self._combine_results(file_name_result, dir_name_result, 'quality')
|
2014-05-26 02:29:22 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
if not final_result.show:
|
|
|
|
raise InvalidShowException(
|
|
|
|
"Unable to parse " + name.encode(sickbeard.SYS_ENCODING, 'xmlcharrefreplace'))
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# if there's no useful info in it then raise an exception
|
2014-07-14 22:00:53 -04:00
|
|
|
if final_result.season_number == None and not final_result.episode_numbers and final_result.air_date == None and final_result.sports_air_date == None and not final_result.ab_episode_numbers and not final_result.series_name:
|
2014-03-10 01:18:05 -04:00
|
|
|
raise InvalidNameException("Unable to parse " + name.encode(sickbeard.SYS_ENCODING, 'xmlcharrefreplace'))
|
|
|
|
|
2014-05-26 06:42:34 -04:00
|
|
|
if cache_result:
|
2014-07-15 18:01:23 -04:00
|
|
|
name_parser_cache.add(name, final_result)
|
2014-05-26 06:42:34 -04:00
|
|
|
|
2014-06-16 06:44:33 -04:00
|
|
|
logger.log(u"Parsed " + name + " into " + str(final_result).decode('utf-8', 'xmlcharrefreplace'), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
return final_result
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-30 01:48:02 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class ParseResult(object):
|
|
|
|
def __init__(self,
|
|
|
|
original_name,
|
|
|
|
series_name=None,
|
2014-05-01 23:03:44 -04:00
|
|
|
sports_event_id=None,
|
|
|
|
sports_event_name=None,
|
2014-07-14 22:00:53 -04:00
|
|
|
sports_air_date=None,
|
2014-03-10 01:18:05 -04:00
|
|
|
season_number=None,
|
|
|
|
episode_numbers=None,
|
|
|
|
extra_info=None,
|
|
|
|
release_group=None,
|
2014-04-28 05:15:29 -04:00
|
|
|
air_date=None,
|
2014-05-26 02:29:22 -04:00
|
|
|
ab_episode_numbers=None,
|
2014-05-31 06:35:11 -04:00
|
|
|
show=None,
|
2014-06-07 07:06:21 -04:00
|
|
|
score=None,
|
2014-07-22 00:53:32 -04:00
|
|
|
quality=None,
|
|
|
|
version=None
|
2014-03-25 01:57:24 -04:00
|
|
|
):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
self.original_name = original_name
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.series_name = series_name
|
|
|
|
self.season_number = season_number
|
|
|
|
if not episode_numbers:
|
|
|
|
self.episode_numbers = []
|
|
|
|
else:
|
|
|
|
self.episode_numbers = episode_numbers
|
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
if not ab_episode_numbers:
|
|
|
|
self.ab_episode_numbers = []
|
|
|
|
else:
|
|
|
|
self.ab_episode_numbers = ab_episode_numbers
|
|
|
|
|
2014-06-07 07:06:21 -04:00
|
|
|
if not quality:
|
|
|
|
self.quality = common.Quality.UNKNOWN
|
|
|
|
else:
|
|
|
|
self.quality = quality
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.extra_info = extra_info
|
|
|
|
self.release_group = release_group
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.air_date = air_date
|
2014-04-30 08:10:13 -04:00
|
|
|
|
2014-05-01 23:03:44 -04:00
|
|
|
self.sports_event_id = sports_event_id
|
|
|
|
self.sports_event_name = sports_event_name
|
2014-07-14 22:00:53 -04:00
|
|
|
self.sports_air_date = sports_air_date
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-06-01 01:39:24 -04:00
|
|
|
self.which_regex = []
|
2014-05-26 02:29:22 -04:00
|
|
|
self.show = show
|
2014-05-31 06:35:11 -04:00
|
|
|
self.score = score
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-07-22 00:53:32 -04:00
|
|
|
self.version = version
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not other:
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if self.series_name != other.series_name:
|
|
|
|
return False
|
|
|
|
if self.season_number != other.season_number:
|
|
|
|
return False
|
|
|
|
if self.episode_numbers != other.episode_numbers:
|
|
|
|
return False
|
|
|
|
if self.extra_info != other.extra_info:
|
|
|
|
return False
|
|
|
|
if self.release_group != other.release_group:
|
|
|
|
return False
|
|
|
|
if self.air_date != other.air_date:
|
|
|
|
return False
|
2014-05-01 23:03:44 -04:00
|
|
|
if self.sports_event_id != other.sports_event_id:
|
|
|
|
return False
|
|
|
|
if self.sports_event_name != other.sports_event_name:
|
|
|
|
return False
|
2014-07-14 22:00:53 -04:00
|
|
|
if self.sports_air_date != other.sports_air_date:
|
2014-04-30 18:07:18 -04:00
|
|
|
return False
|
2014-05-26 02:29:22 -04:00
|
|
|
if self.ab_episode_numbers != other.ab_episode_numbers:
|
|
|
|
return False
|
|
|
|
if self.show != other.show:
|
|
|
|
return False
|
2014-05-31 06:35:11 -04:00
|
|
|
if self.score != other.score:
|
|
|
|
return False
|
2014-06-07 07:06:21 -04:00
|
|
|
if self.quality != other.quality:
|
|
|
|
return False
|
2014-07-22 00:53:32 -04:00
|
|
|
if self.version != other.version:
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
def __str__(self):
|
2014-03-20 14:03:22 -04:00
|
|
|
if self.series_name != None:
|
2014-03-10 01:18:05 -04:00
|
|
|
to_return = self.series_name + u' - '
|
|
|
|
else:
|
|
|
|
to_return = u''
|
2014-03-20 14:03:22 -04:00
|
|
|
if self.season_number != None:
|
2014-03-25 01:57:24 -04:00
|
|
|
to_return += 'S' + str(self.season_number)
|
2014-03-10 01:18:05 -04:00
|
|
|
if self.episode_numbers and len(self.episode_numbers):
|
|
|
|
for e in self.episode_numbers:
|
2014-03-25 01:57:24 -04:00
|
|
|
to_return += 'E' + str(e)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
if self.is_air_by_date:
|
2014-03-10 01:18:05 -04:00
|
|
|
to_return += str(self.air_date)
|
2014-07-14 22:00:53 -04:00
|
|
|
if self.is_sports:
|
2014-05-01 23:03:44 -04:00
|
|
|
to_return += str(self.sports_event_name)
|
|
|
|
to_return += str(self.sports_event_id)
|
2014-07-14 22:00:53 -04:00
|
|
|
to_return += str(self.sports_air_date)
|
2014-05-26 02:29:22 -04:00
|
|
|
if self.ab_episode_numbers:
|
2014-07-22 00:53:32 -04:00
|
|
|
to_return += ' [ABS: ' + str(self.ab_episode_numbers) + ']'
|
|
|
|
if self.version:
|
|
|
|
to_return += ' [ANIME VER: ' + str(self.version) + ']'
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if self.release_group:
|
2014-07-21 06:13:17 -04:00
|
|
|
to_return += ' [GROUP: ' + self.release_group + ']'
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
to_return += ' [ABD: ' + str(self.is_air_by_date) + ']'
|
|
|
|
to_return += ' [SPORTS: ' + str(self.is_sports) + ']'
|
2014-05-26 02:29:22 -04:00
|
|
|
to_return += ' [ANIME: ' + str(self.is_anime) + ']'
|
2014-04-28 05:15:29 -04:00
|
|
|
to_return += ' [whichReg: ' + str(self.which_regex) + ']'
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return to_return.encode('utf-8')
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
@property
|
|
|
|
def is_air_by_date(self):
|
2014-04-30 08:10:13 -04:00
|
|
|
if self.season_number == None and len(self.episode_numbers) == 0 and self.air_date:
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
|
|
|
return False
|
2014-05-07 03:50:49 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
@property
|
|
|
|
def is_sports(self):
|
|
|
|
if self.season_number == None and len(self.episode_numbers) == 0 and self.sports_air_date:
|
|
|
|
return True
|
2014-05-26 02:29:22 -04:00
|
|
|
return False
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
@property
|
|
|
|
def is_anime(self):
|
|
|
|
if len(self.ab_episode_numbers):
|
2014-04-28 05:15:29 -04:00
|
|
|
return True
|
|
|
|
return False
|
2014-05-07 03:50:49 -04:00
|
|
|
|
2014-07-18 14:47:19 -04:00
|
|
|
|
2014-07-15 18:01:23 -04:00
|
|
|
class NameParserCache(object):
|
|
|
|
_previous_parsed = {}
|
|
|
|
_cache_size = 100
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-01 18:53:37 -04:00
|
|
|
def add(self, name, parse_result):
|
2014-07-15 18:01:23 -04:00
|
|
|
self._previous_parsed[name] = parse_result
|
|
|
|
_current_cache_size = len(self._previous_parsed)
|
|
|
|
if _current_cache_size > self._cache_size:
|
|
|
|
for i in range(_current_cache_size - self._cache_size):
|
|
|
|
del self._previous_parsed[self._previous_parsed.keys()[0]]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
def get(self, name):
|
2014-07-15 18:01:23 -04:00
|
|
|
if name in self._previous_parsed:
|
2014-07-14 22:00:53 -04:00
|
|
|
logger.log("Using cached parse result for: " + name, logger.DEBUG)
|
2014-07-15 18:01:23 -04:00
|
|
|
return self._previous_parsed[name]
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-15 18:01:23 -04:00
|
|
|
name_parser_cache = NameParserCache()
|
2014-07-06 09:11:04 -04:00
|
|
|
|
2014-07-15 14:40:40 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class InvalidNameException(Exception):
|
2014-07-14 22:00:53 -04:00
|
|
|
"The given release name is not valid"
|
2014-07-06 09:11:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidShowException(Exception):
|
2014-07-14 22:00:53 -04:00
|
|
|
"The given show name is not valid"
|