2 # -*- coding: utf-8 -*-
3 # Module: LibraryExporter
4 # Created on: 13.01.2017
11 """Exports Netflix shows & movies to a local library folder (Not yet ready)"""
13 series_label = 'shows'
14 movies_label = 'movies'
15 db_filename = 'lib.ndb'
18 def __init__ (self, base_url, root_folder, library_settings, log_fn=noop):
19 """Takes the instances & configuration options needed to drive the plugin
26 root_folder : :obj:`str`
29 library_settings : :obj:`str`
30 User data cache location
32 library_db_path : :obj:`str`
33 User data cache location
38 self.base_url = base_url
39 self.base_data_path = root_folder
40 self.enable_custom_library_folder = library_settings['enablelibraryfolder']
41 self.custom_library_folder = library_settings['customlibraryfolder']
43 self.db_filepath = os.path.join(self.base_data_path, self.db_filename)
45 # check for local library folder & set up the paths
46 if self.enable_custom_library_folder != 'true':
47 self.movie_path = os.path.join(self.base_data_path, self.series_label)
48 self.tvshow_path = os.path.join(self.base_data_path, self.movies_label)
50 self.movie_path = os.path.join(self.custom_library_folder, self.movies_label)
51 self.tvshow_path = os.path.join(self.custom_library_folder, self.series_label)
53 self.setup_local_netflix_library(source={
54 self.movies_label: self.movie_path,
55 self.series_label: self.tvshow_path
58 self.db = self._load_local_db(filename=self.db_filepath)
60 def setup_local_netflix_library (self, source):
62 if not os.path.exists(source[label]):
63 os.makedirs(source[label])
65 def write_strm_file(self, path, url):
66 with open(path, 'w+') as f:
70 def _load_local_db (self, filename):
71 # if the db doesn't exist, create it
72 if not os.path.isfile(filename):
73 data = {self.movies_label: {}, self.series_label: {}}
74 self.log('Setup local library DB')
75 self._update_local_db(filename=filename, data=data)
78 with open(filename) as f:
85 def _update_local_db (self, filename, data):
86 if not os.path.isdir(os.path.dirname(filename)):
88 with open(filename, 'w') as f:
93 def movie_exists (self, title, year):
94 movie_meta = '%s (%d)' % (title, year)
95 return movie_meta in self.db[self.movies_label]
97 def show_exists (self, title, year):
98 show_meta = '%s (%d)' % (title, year)
99 return show_meta in self.db[self.series_label]
101 def season_exists (self, title, year, season):
102 if self.show_exists() == False:
104 show_meta = '%s (%d)' % (title, year)
105 show_entry = self.db[self.series_label][show_meta]
106 return season in show_entry['seasons']
108 def episode_exists (self, title, year, season, episode):
109 if self.show_exists() == False:
111 show_meta = '%s (%d)' % (title, year)
112 show_entry = self.db[self.series_label][show_meta]
113 episode_entry = 'S%02dE%02d' % (season, episode)
114 return episode_entry in show_entry['episodes']
116 def add_movie(self, title, year, video_id, pin, build_url):
117 movie_meta = '%s (%d)' % (title, year)
118 dirname = os.path.join(self.movie_path, movie_meta)
119 filename = os.path.join(dirname, movie_meta + '.strm')
120 if os.path.exists(filename):
122 if not os.path.exists(dirname):
124 if self.movie_exists(title=title, year=year) == False:
125 self.db[self.movies_label][movie_meta] = True
126 self._update_local_db(filename=self.db_filepath, db=self.db)
127 self.write_strm_file(path=filename, url=build_url({'action': 'play_video', 'video_id': video_id, 'pin': pin}))
129 def add_show(self, title, year, episodes, build_url):
130 show_meta = '%s (%d)' % (title, year)
131 show_dir = os.path.join(self.tvshow_path, show_meta)
132 if not os.path.exists(show_dir):
133 os.makedirs(show_dir)
134 if self.show_exists(title, year) == False:
135 self.db[self.series_label][show_meta] = {'seasons': [], 'episodes': []}
136 for episode_id in episodes:
137 episode = episodes[episode_id]
138 self._add_episode(show_dir=show_dir, show_meta=show_meta, title=title, year=year, season=episode['season'], episode=episode['idx'], video_id=episode['id'], pin=episode['pin'], build_url=build_url)
139 self._update_local_db(filename=self.db_filepath, db=self.db)
142 def _add_episode(self, title, year, show_dir, show_meta, season, episode, video_id, pin, build_url):
144 episode = int(episode)
147 if self.season_exists(title=title, year=year, season=season) == False:
148 self.db[self.series_label][show_meta]['seasons'].append(season)
151 episode_meta = 'S%02dE%02d' % (season, episode)
152 if self.episode_exists(title=title, year=year, season=season, episode=episode) == False:
153 self.db[self.series_label][show_meta]['episodes'].append(episode_meta)
156 filename = episode_meta + '.strm'
157 filepath = os.path.join(show_dir, filename)
158 if os.path.exists(filepath):
160 self.write_strm_file(path=filepath, url=build_url({'action': 'play_video', 'video_id': video_id, 'pin': pin}))
162 def remove_movie(self, title, year):
163 movie_meta = '%s (%d)' % (title, year)
164 del self.db[self.movies_label][movie_meta]
165 self._update_local_db(filename=self.db_filepath, db=self.db)
166 dirname = os.path.join(self.movie_path, movie_meta)
167 if os.path.exists(dirname):
172 def remove_show(self, title, year):
173 show_meta = '%s (%d)' % (title, year)
174 del self.db[self.series_label][show_meta]
175 self._update_local_db(filename=self.db_filepath, db=self.db)
176 show_dir = os.path.join(self.tvshow_path, show_meta)
177 if os.path.exists(show_dir):
182 def remove_season(self, title, year, season):
186 show_meta = '%s (%d)' % (title, year)
187 for season_entry in self.db[self.series_label][show_meta]['seasons']:
188 if season_entry != season:
189 season_list.append(season_entry)
190 self.db[self.series_label][show_meta]['seasons'] = season_list
191 show_dir = os.path.join(self.tvshow_path, show_meta)
192 if os.path.exists(show_dir):
193 show_files = [f for f in os.listdir(show_dir) if os.path.isfile(os.path.join(show_dir, f))]
194 for filename in show_files:
195 if 'S%02dE' % (season) in filename:
196 os.remove(os.path.join(show_dir, filename))
198 episodes_list.append(filename.replace('.strm', ''))
199 self.db[self.series_label][show_meta]['episodes'] = episodes_list
200 self._update_local_db(filename=self.db_filepath, db=self.db)
203 def remove_episode(self, title, year, season, episode):
205 show_meta = '%s (%d)' % (title, year)
206 episode_meta = 'S%02dE%02d' % (season, episode)
207 show_dir = os.path.join(self.tvshow_path, show_meta)
208 if os.path.exists(os.path.join(show_dir, episode_meta + '.strm')):
209 os.remove(os.path.join(show_dir, episode_meta + '.strm'))
210 for episode_entry in self.db[self.series_label][show_meta]['episodes']:
211 if episode_meta != episode_entry:
212 episodes_list.append(episode_entry)
213 self.db[self.series_label][show_meta]['episodes'] = episodes_list
214 self._update_local_db(filename=self.db_filepath, db=self.db)