This repository was archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplextools.py
More file actions
171 lines (147 loc) · 6.92 KB
/
plextools.py
File metadata and controls
171 lines (147 loc) · 6.92 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#-------------------------------------------------------------------------------
# MPEG-TS Parser
#-------------------------------------------------------------------------------
import os
import logging
import re
import shutil
import platform
import post_proc
class PlexTools:
def __init__(self, plexpath):
self.plexpath = plexpath
def get_season_combinations(self, season):
leading_zero = True
season_title = 'Season'
season_str = []
season_num = int(season)
season_str.append(season_title + str(season_num).zfill(2))
season_str.append(season_title + ' ' + str(season_num).zfill(2))
if (season_num < 10):
season_str.append(season_title + str(season_num).zfill(1))
season_str.append(season_title + ' ' + str(season_num).zfill(1))
logging.debug('Creating season combination strings for season: ' + str(season_str))
return season_str
def check_show_in_plex(self, show):
#check for the show
if not os.path.exists(os.path.join(self.plexpath,show)):
logging.info('Show ' + show + ' does not exist in path ' + self.plexpath)
return False
return True
def check_season_in_plex(self, show, season):
#Season can take form , Season XX, SeasonXX, SeasonX
seasons = self.get_season_combinations(season)
valid_season = ''
season_check = False
for s in seasons:
if os.path.exists(os.path.join(self.plexpath,show,s)):
season_check |= True
valid_season = s
if not season_check:
logging.info('Season ' + season + ' does not exist in show ' + show + ' for path ' + self.plexpath)
return ''
return valid_season
def add_season_to_plex(self, show, season):
#Check plexpath exists
if not os.path.exists(self.plexpath):
logging.info('Plex Path ' + self.plexpath + ' does not exist.')
return False
#now check for the show and add if not existing
if not os.path.exists(os.path.join(self.plexpath,show)):
logging.info('Adding Show ' + show + ' to ' + self.plexpath)
os.makedirs(os.path.join(self.plexpath,show))
#finally check if the Season Path exists
if not os.path.exists(os.path.join(self.plexpath, show, 'Season ' + season)):
logging.info('Adding Season ' + season + ' to ' + show + ' in ' + self.plexpath)
os.makedirs(os.path.join(self.plexpath,show, 'Season ' + season))
def move_episode_to_plex(self, show, season, plexfile, oldfile):
# Check plexpath exists
if not os.path.exists(self.plexpath):
logging.error('Plex Path ' + self.plexpath + ' does not exist.')
return False
# Check the show exists
if not self.check_show_in_plex(show):
logging.debug('Plex does not contain show [' + show + '] - Can\'t move file')
return False
# Check the Season exists in the show
season_str = self.check_season_in_plex(show, season)
if season_str == '':
logging.debug('Plex does not contain season [' + season + '] for show [' + show + '] so can\t move file')
return False
logging.info('Moving [' + oldfile + '] to [' + os.path.join(self.plexpath, show, season_str, plexfile))
shutil.move(oldfile,os.path.join(self.plexpath, show, season_str, plexfile))
return True
def link_episode_to_dvr(self, dvrpath, plexpath,show,season,epnum,eptitle,filename):
if not os.path.exists(os.path.join(plexpath, show, 'Season ' + season)):
logging.debug('Can\'t Link episode - [' + os.path.join(plexpath, show, 'Season ' + season) + '] doesn\'t exist')
return False
# does link already exist
# create relative link if possible
common_path = os.path.commonprefix([dvrpath, plexpath])
rel_plex_path = os.path.relpath(plexpath,dvrpath)
logging.info('Identified common path in files: '+common_path)
logging.info('Updating Plex Path to: '+rel_plex_path)
# link file back to DVR folder
logging.info('Linking [' + os.path.join(rel_plex_path, show, 'Season ' + season, self.fix_filename(show, season, epnum, eptitle)) + '] to [' + filename )
if platform.system() == 'Windows':
logging.warn('Linking is not supported at this time on Windows')
else:
logging.info('Doing the link')
os.symlink(os.path.join(rel_plex_path, show, 'Season ' + season, self.fix_filename(show, season, epnum, eptitle)),filename)
def construct_filename(self, show, season, episodenum, eptitle, extension):
#check season for 'Season'
season_str = re.match('Season\s', season)
if season_str == '':
logging.debug('no season string found')
filename = show + '-' + episodenum + '-' + eptitle + extension
return filename
def get_episodes_in_season(self, show, season):
path = os.path.join(self.plexpath, show, season)
return [os.path.join(path,f) for f in os.listdir(path) \
if (not os.path.islink(os.path.join(path,f))) & os.path.isfile(os.path.join(path,f))]
def find_filename(self, show, season, episodenum):
logging.debug('Checking for existing filename in Plex matching [' + show + '][' + season + '][' + episodenum + ']')
filename = ''
# Check plexpath exists
if not os.path.exists(self.plexpath):
logging.error('Plex Path ' + self.plexpath + ' does not exist.')
return filename
# Check the show exists
if not self.check_show_in_plex(show):
logging.debug('Plex does not contain show [' + show + '] so no file to find')
return filename
# Check the Season exists in the show
season_str = self.check_season_in_plex(show, season)
if season_str == '':
logging.debug('Plex does not contain season [' + season + '] for show [' + show + '] so no file to find')
return filename
# Is there any file matching the episodenumber
files = self.get_episodes_in_season(show, season_str)
logging.debug('Checking ' + str(len(files)))
for f in files:
logging.debug('Checking ' + f + ' for SxxExx match to ' + episodenum)
epnum = re.findall('S\d{2}E\d{2}', f)
if (len(epnum) > 0) and (epnum[0] == episodenum):
logging.debug('Found filename for episode ' + episodenum)
filename = f
return filename
def post_process_file(self, show, season, plexfile, script):
# Check plexpath exists
if not os.path.exists(self.plexpath):
logging.error('Plex Path ' + self.plexpath + ' does not exist.')
return False
# Check the show exists
if not self.check_show_in_plex(show):
logging.debug('Plex does not contain show [' + show + '] - Can\'t move file')
return False
# Check the Season exists in the show
season_str = self.check_season_in_plex(show, season)
if season_str == '':
logging.debug('Plex does not contain season [' + season + '] for show [' + show + '] so can\t move file')
return False
new_plex_filename = plexfile.replace(".mpg",".mkv")
# TODO: Add check for new plex filename - don't want to overwrite an existing file.
postProc = post_proc.PostProcessor(script)
postProc.set_infile(os.path.join(self.plexpath, show, season_str, plexfile))
postProc.set_outfile(os.path.join(self.plexpath, show, season_str, new_plex_filename))
postProc.execute_script()