Skip to content

Commit

Permalink
fix: improve series detection
Browse files Browse the repository at this point in the history
  • Loading branch information
3h4x committed Jul 17, 2022
1 parent a6aa7ec commit 8747c47
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ What things you need to run the program:

<p align="center">
Made with ❤️ by <a href="https://github.com/3h4x">3h4x</a></br>
Loosely based on work of <a href="https://github.com/bearlike">bearlike</a>
Loosely based on work of <a href="https://github.com/bearlike/Media-Library-Organiser">bearlike</a>
</p>

![wave](http://cdn.thekrishna.in/img/common/border.png)
14 changes: 9 additions & 5 deletions src/organizer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
import os
import sys
import click

from imdb import IMDb

from rename_movies import rename_movies
from rename_series import rename_series

Expand All @@ -11,22 +12,25 @@
@click.pass_context
def main(ctx):
ctx.ensure_object(dict)
ctx.obj["imdb_client"] = IMDb()


@main.command(help="Rename Movies")
@click.pass_context
@click.option("--path", "-p", help="Path", default=".")
@click.option(
"--default", help="Should default answer for renaming be True", default=True
)
def movies(path, default):
rename_movies(path, default)
def movies(ctx, path, default):
rename_movies(ctx, path, default)


@main.command(help="Rename TV Series")
@click.pass_context
@click.option("--path", "-p", help="Path", default=".")
@click.option("--force", "-f", help="Automatically rename", is_flag=True, default=False)
def series(path, force):
rename_series(path, force)
def series(ctx, path, force):
rename_series(ctx, path, force)


if __name__ == "__main__":
Expand Down
10 changes: 4 additions & 6 deletions src/rename_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import click
import ipdb
import re
from imdb import IMDb
from similarity.damerau import Damerau

damerau = Damerau()
Expand Down Expand Up @@ -32,9 +31,8 @@ def find_url_in_string(file_name: str) -> str:


# Returns a List Movie name and release year using imDB from Old_FileName
def main_imdb(str21):
ia = IMDb()
s_result = ia.search_movie(str21)
def get_imdb_title(imdb_client, name):
s_result = imdb_client.search_movie(name)
movies = []
for movie in s_result:
if movie["kind"] == "movie":
Expand Down Expand Up @@ -87,7 +85,7 @@ def FormatStr(file_new_name):
return rest.strip()


def rename_movies(path, default):
def rename_movies(ctx, path, default):
files = os.listdir(path)
for file in files:
file_new_name = file
Expand All @@ -104,7 +102,7 @@ def rename_movies(path, default):
)
file_new_name = file_new_name[0 : len(file_new_name) - 4]
Final = file_new_name + year_str
movies = main_imdb(file_new_name + year_str)
movies = get_imdb_title(ctx.obj["imdb_client"], file_new_name + year_str)
if not movies:
click.secho(f'No Match Found for "{file_new_name}"', bg="yellow")
click.echo()
Expand Down
66 changes: 29 additions & 37 deletions src/rename_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import subprocess
import click
from imdb import IMDb
from similarity.damerau import Damerau


Expand All @@ -21,14 +20,12 @@ def find_most_apt(name, series):


# Retrieves name from imDB
def main_imdb(str21):
ia = IMDb()
s_result = ia.search_movie(str21)
def get_imdb_title(imdb_client, name, season, episode):
results = imdb_client.search_movie(name)
series = []
for ss in s_result:
if ss["kind"] == "tv series":
str2 = ss["title"]
series.append(str2)
for result in results:
if result["kind"] == "tv series":
series.append(result["title"])
return series


Expand All @@ -47,48 +44,46 @@ def removeIllegal(str):
return str


RE_X = r"(\d)+\s+x\s+(\d+)"
RE_X = re.compile("(\d)+\s+x\s+(\d+)", re.IGNORECASE)
RE_SE = re.compile("SE?(\d+)EP?(\d+)", re.IGNORECASE)
RE_E = re.compile("EP?(\d+)", re.IGNORECASE)


def hasX(inputString):
return bool(re.search(RE_X, inputString, re.IGNORECASE))


RE_SE = r"SE?(\d+)EP?(\d+)"


def hasSE(inputString):
return bool(re.search(RE_SE, inputString, re.IGNORECASE))
RE_XA = re.compile("(\d)+\s+x\s+(\d+).*", re.IGNORECASE)
RE_SEA = re.compile("SE?(\d+)EP?(\d+).*", re.IGNORECASE)
RE_EA = re.compile("EP?(\d+).*", re.IGNORECASE)


def get_season_episode(file_name: str):
if hasSE(file_name):
season, episode = re.search(RE_SE, file_name, re.IGNORECASE).groups()
if re.search(RE_SE, file_name):
season, episode = re.search(RE_SE, file_name).groups()
return AddZero(season), AddZero(episode)
elif hasX(file_name):
season, episode = re.search(RE_X, file_name, re.IGNORECASE).groups()
elif re.search(RE_X, file_name):
season, episode = re.search(RE_X, file_name).groups()
return AddZero(season), AddZero(episode)
elif re.search(RE_E, file_name):
episode = re.search(RE_E, file_name).groups()[0]
return AddZero(1), AddZero(episode)

return "", ""


def sanitize_name(file_name):
file_name = re.sub(RE_X, "", file_name)
file_name = re.sub(RE_SE, "", file_name)
file_name = re.sub(RE_XA, "", file_name)
file_name = re.sub(RE_SEA, "", file_name)
file_name = re.sub(RE_EA, "", file_name)
return file_name.replace("-", " ").replace(".", " ").strip()


def AddZero(inputString):
if int(inputString) < 10:
return str("0" + str(int(inputString)))
return inputString
def AddZero(input):
if int(input) < 10:
return str("0" + str(int(input)))
return input


def rename_series(path, force):
def rename_series(ctx, path, force):
click.echo("Reading Files....")

files = os.listdir(path)
for file in files:
for file in sorted(os.listdir(path)):
_, file = os.path.split(file)
file_name, extension = os.path.splitext(file)

Expand Down Expand Up @@ -118,10 +113,7 @@ def rename_series(path, force):
continue

file_name = sanitize_name(file_name)
import ipdb

ipdb.set_trace()
series = main_imdb(file_name)
series = get_imdb_title(ctx.obj["imdb_client"], file_name, season, episode)
if not series:
click.secho(f'Couldnt find imdb series for "{file_name}"', fg="yellow")
continue
Expand All @@ -145,4 +137,4 @@ def rename_series(path, force):
except FileExistsError:
print(f"Error - File Already Exist: {Final}")

click.echo("All Files Processed...")
click.echo("All Files Processed...")

0 comments on commit 8747c47

Please sign in to comment.