|
@@ -8,6 +8,7 @@ import tvdb_v4_official
|
|
|
|
|
|
|
|
# Default config
|
|
# Default config
|
|
|
default_apikey = os.path.expanduser('~/.tvdb_apikey')
|
|
default_apikey = os.path.expanduser('~/.tvdb_apikey')
|
|
|
|
|
+default_lang = 'eng'
|
|
|
|
|
|
|
|
# Local helpers
|
|
# Local helpers
|
|
|
def readConfigLine(file):
|
|
def readConfigLine(file):
|
|
@@ -28,6 +29,7 @@ def readConfigLine(file):
|
|
|
parser = argparse.ArgumentParser('TVDB Renamer')
|
|
parser = argparse.ArgumentParser('TVDB Renamer')
|
|
|
parser.add_argument('--api', default=default_apikey, help='Path to a file containing the TVDB API key')
|
|
parser.add_argument('--api', default=default_apikey, help='Path to a file containing the TVDB API key')
|
|
|
parser.add_argument('--verbose', '-v', action='store_true', help='Display missing and unexpected episodes')
|
|
parser.add_argument('--verbose', '-v', action='store_true', help='Display missing and unexpected episodes')
|
|
|
|
|
+parser.add_argument('--lang', '-l', default=default_lang, help='TVDB language code')
|
|
|
parser.add_argument('path', type=pathlib.Path, help='The directory containing files to be renamed')
|
|
parser.add_argument('path', type=pathlib.Path, help='The directory containing files to be renamed')
|
|
|
cli = parser.parse_args()
|
|
cli = parser.parse_args()
|
|
|
if not os.path.isfile(cli.api):
|
|
if not os.path.isfile(cli.api):
|
|
@@ -36,6 +38,11 @@ if not os.path.isfile(cli.api):
|
|
|
if not os.path.isdir(cli.path):
|
|
if not os.path.isdir(cli.path):
|
|
|
print('Invalid path: {path}'.format(path=cli.path))
|
|
print('Invalid path: {path}'.format(path=cli.path))
|
|
|
exit(1)
|
|
exit(1)
|
|
|
|
|
+if cli.lang == 'None' or cli.lang == '':
|
|
|
|
|
+ cli.lang = None
|
|
|
|
|
+if cli.verbose:
|
|
|
|
|
+ print('Path: {p}'.format(p=cli.path))
|
|
|
|
|
+ print('Language: {l}'.format(l=cli.lang))
|
|
|
|
|
|
|
|
# Move to keep the file paths flat
|
|
# Move to keep the file paths flat
|
|
|
try:
|
|
try:
|
|
@@ -76,26 +83,38 @@ for file in dirfiles:
|
|
|
# Fetch the TVDB episode list
|
|
# Fetch the TVDB episode list
|
|
|
try:
|
|
try:
|
|
|
apikey = readConfigLine(cli.api)
|
|
apikey = readConfigLine(cli.api)
|
|
|
- series = readConfigLine('.tvdb')
|
|
|
|
|
- if apikey is None or series is None:
|
|
|
|
|
|
|
+ series_id = readConfigLine('.tvdb')
|
|
|
|
|
+ if cli.verbose:
|
|
|
|
|
+ print('Series: {id}'.format(id=series_id))
|
|
|
|
|
+ if apikey is None or series_id is None:
|
|
|
print('Invalid TVDB parameters')
|
|
print('Invalid TVDB parameters')
|
|
|
exit(1)
|
|
exit(1)
|
|
|
tvdb = tvdb_v4_official.TVDB(apikey)
|
|
tvdb = tvdb_v4_official.TVDB(apikey)
|
|
|
- data = tvdb.get_series_episodes(series)
|
|
|
|
|
|
|
+ if cli.lang is None:
|
|
|
|
|
+ series = tvdb.get_series(id=series_id)
|
|
|
|
|
+ else:
|
|
|
|
|
+ series = tvdb.get_series_translation(id=series_id, lang=cli.lang)
|
|
|
|
|
+ data = tvdb.get_series_episodes(id=series_id, lang=cli.lang)
|
|
|
del tvdb
|
|
del tvdb
|
|
|
- del series
|
|
|
|
|
|
|
+ del series_id
|
|
|
del apikey
|
|
del apikey
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
- print('Unable to fetch series {id}: {e}'.format(id=series, e=error))
|
|
|
|
|
|
|
+ print(
|
|
|
|
|
+ "Unable to fetch series {id}: {e}\nSeries:\n{s}\nEpisodes:\n{ep}".format(
|
|
|
|
|
+ id=series_id, e=error, s=series, ep=data
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
exit(1)
|
|
exit(1)
|
|
|
|
|
|
|
|
# Parse and clean the data we need
|
|
# Parse and clean the data we need
|
|
|
episodes = []
|
|
episodes = []
|
|
|
try:
|
|
try:
|
|
|
- seriesName = data['series']['name'].strip()
|
|
|
|
|
|
|
+ series_name = series['name'].strip()
|
|
|
|
|
+ if cli.verbose:
|
|
|
|
|
+ print('Series: {name}'.format(name=series_name))
|
|
|
for episode in data['episodes']:
|
|
for episode in data['episodes']:
|
|
|
e = {}
|
|
e = {}
|
|
|
- e['series'] = seriesName
|
|
|
|
|
|
|
+ e['series'] = series_name
|
|
|
e['season'] = int(episode['seasonNumber'])
|
|
e['season'] = int(episode['seasonNumber'])
|
|
|
e['number'] = int(episode['number'])
|
|
e['number'] = int(episode['number'])
|
|
|
e['name'] = episode['name'].strip()
|
|
e['name'] = episode['name'].strip()
|
|
@@ -155,3 +174,6 @@ for e in episodes:
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
print('Unable to rename {s}: {e}'.format(s=src, e=error))
|
|
print('Unable to rename {s}: {e}'.format(s=src, e=error))
|
|
|
continue
|
|
continue
|
|
|
|
|
+ else:
|
|
|
|
|
+ if cli.verbose:
|
|
|
|
|
+ print('Found: {m}'.format(m=e['filename']))
|