-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfo_to_plexmatch.py
97 lines (79 loc) · 2.42 KB
/
nfo_to_plexmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import sys
from tqdm import tqdm
from models.file import File
from models.movie import Movie
from models.plexmatch import PlexMatch
from models.show import Show
from models.xmlfile import XMLFile
from models.media import Media
def walk_dir(media: Media, path: str, override: bool = False) -> None:
bar = tqdm(unit=" folders scanned")
for root, _, files in os.walk(path):
need_convert = False
f = None
for file in files:
if media.check_nfo(file):
f = File(file, root)
need_convert = True
if not override and ".plexmatch" in file:
need_convert = False
break
if need_convert:
nfoTo_plexmatch(f)
bar.update(1)
def write_match(path: str, plex_match: PlexMatch) -> bool:
with open(f"{path}/.plexmatch", "w", encoding="utf-8") as f:
try:
f.write(plex_match.to_plexmatch())
return True
except Exception:
return False
def nfoTo_plexmatch(nfo_files) -> bool:
xml = XMLFile(f"{nfo_files.path}/{nfo_files.name}")
if not xml.parse():
return False
xml.get_root()
plexmatch = xml.get_plexmatch()
write_match(nfo_files.path, plexmatch)
return True
def help():
print(f"""
Usage:
{sys.argv[0]} <option> <path>
Options:
-h, --help:
Print this help message
-s, --show:
Convert nfo for shows
-m, --movie:
Convert nfo for movies
-o, --override:
Override existing .plexmatch files
""")
if __name__ == "__main__":
path = sys.argv[len(sys.argv) - 1]
if (path.startswith("--")):
print("Not Path specified")
print(help())
exit(1)
is_movie = False
override = False
for i in sys.argv:
if i == "-h" or i == "--help":
help()
exit(0)
if (i == "--show" or i == "-s"):
is_movie = False
if (i == "--movie" or i == "-m"):
is_movie = True
if (i == "--override" or i == "-o"):
override = True
if ("--movie" in sys.argv or "-m" in sys.argv) and ("--show" in sys.argv or "-s" in sys.argv):
print("You can only use one of the following options: --movie, --show")
help()
exit(1)
if is_movie:
walk_dir(Movie(), path, override)
else:
walk_dir(Show(), path, override)