Project

General

Profile

Working (I think) renaming post processing script for Ple... ยป rename.py

Ryan Harter, 2014-04-03 17:27

 
1
"""Script to rename a tv episode file based on information from theTVDB.org"""
2
import os, sys, shutil
3
from pymediainfo import MediaInfo
4
from pprint import pprint
5

    
6
def main():
7
	"""Main entry point for the script."""
8
	file = sys.argv[1]
9
	out_path = sys.argv[2]
10

    
11
	title = get_show_title(file)
12
	print "Title: " + title
13
	
14
	season_num = get_season_number(file)
15
	print "Season: " + str(season_num)
16
	
17
	episode_num = get_episode_number(file)
18
	print "Episode: " + str(episode_num)
19

    
20
	name = build_file_name(title, season_num, episode_num)
21
	print "Renaming to: " + name
22

    
23
	path, ext = os.path.splitext(file)
24
	new_path = os.path.join(out_path, title)
25
	os.makedirs(new_path)
26

    
27
	new_path = os.path.join(new_path, name + ext)
28

    
29
	print "Moving file[" + file + "] to: " + new_path
30
	shutil.move(file, new_path)
31

    
32

    
33
def get_show_title(file):
34
	"""Return the show name from the file metadata"""
35
	media_info = MediaInfo.parse(file)
36
	for track in media_info.tracks:
37
		if track.track_type == 'General':
38
			return track.title
39

    
40
def get_season_number(file):
41
	"""Get the season number from the SYNOPSIS field in the file metadata"""
42
	media_info = MediaInfo.parse(file)
43
	for track in media_info.tracks:
44
		if track.track_type == 'General':
45
			synopsis = track.synopsis
46

    
47
			"""We assume that there won't be more than 99 episodes 
48
			in a season here, so just trim the last two characters
49
			and what remains must be our season number.  There has
50
			to be a smarter way."""
51
			season_num = synopsis[:-2]
52
			return int(season_num)
53

    
54
def get_episode_number(file):
55
	"""Return the episode number from the SYNOPSIS field in the file metadata"""
56
	media_info = MediaInfo.parse(file)
57
	for track in media_info.tracks:
58
		if track.track_type == 'General':
59
			synopsis = track.synopsis
60

    
61
			"""We assume that there won't be more than 99 episodes 
62
			in a season here, so just trim the last two characters
63
			and what remains must be our season number.  There has
64
			to be a smarter way."""
65
			episode_num = synopsis[-2:]
66
			return int(episode_num)
67

    
68
def build_file_name(title, season_num, episode_num):
69
	"""Constructs a file name based on the provided information, excluding extension"""
70
	return title + "-S" + str('%02d' % season_num) + "E" + str('%02d' % episode_num)
71

    
72

    
73
if __name__ == '__main__':
74
	sys.exit(main())
    (1-1/1)