-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamer.py
58 lines (47 loc) · 1.76 KB
/
renamer.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
#!/usr/bin/python
import fnmatch
import time
import episode
import os
import shutil
import re
VALID_FILE_EXTENSIONS = ("*.avi", "*.mpg", "*.mp4", "*.ogm", "*.mov")
CONFIG_FILE = "renamer.cfg"
def parse_config(s):
"""Parse a config file into a dictionary"""
config_dict = {}
lines = s.split("\n")
for line in lines:
line = line.strip()
if line and line[0] != "#":
values = line.split(" ", 1)
config_dict[values[0]] = values[1].strip()
return config_dict
# Print load message
print "TV Renaming Script Started"
# Read in the config file
f = file(CONFIG_FILE)
try:
config_data = f.read()
finally:
f.close()
config = parse_config(config_data)
# Check files
for root, dirs, files in os.walk(config["path_to_watch"]):
for extension in VALID_FILE_EXTENSIONS:
for filename in fnmatch.filter(files, extension):
old_file_path = os.path.join(root, filename)
ep = episode.Episode()
try:
ep.parse_filename(filename)
ep.get_episode_name()
new_file_path = os.path.join(config["destination_path"], ep.create_file_path(config["output_format"]))
new_file_dir = os.path.dirname(new_file_path)
if not os.path.exists(new_file_dir):
os.makedirs(new_file_dir)
shutil.move(old_file_path, new_file_path)
print "Moved " + old_file_path + " => " + new_file_path
except episode.ParserError:
print "Ignoring unparseable filename (%s)" % filename
except episode.LookupError:
print "Error: Couldn't find episode in tv database"