def send_thread(self): if self.thread is None: self.thread = FilepathSearchThread(self.treeWidget()) self.thread.finished.connect(self.receive_thread) thread_ready = self.thread.wait(10000) if thread_ready: tree = self.treeWidget() self.pbar = QProgressBar(tree) self.pbar.setRange(0, 0) tree.setItemWidget(self, 1, self.pbar) self.thread.filepath = self.filepath self.thread.start()
class FileItem(QTreeWidgetItem): cols = ['Filepath', 'Renamed Filepath'] invalid_char_map = {':': ''} re_filename_episodes = [ re.compile( r'.*[Ss](?P<season>\d{1,2})[\s\-_]*[Ee](?P<episode>\d{1,2}).*', re.I), re.compile( r'.*season\s*(?P<season>\d{1,2})[\s\-_]*episode\s*[Ee](?P<episode>\d{1,2}).*', re.I), ] re_filepath_episodes = [ re.compile( r'.*season\s*(?P<season>\d{1,2})\\(?:season\s*\d{1,2}[\s\-_]*)?(?:episode|ep\.?|e)\s*(?P<episode>\d{1,2}).*', re.I), ] def __init__(self, parent, filepath, mediaman): super(FileItem, self).__init__(parent) self._renamed = False self.mediaman = mediaman self.filepath = filepath self.renamed_filepath = None self.media_info = None self.episode_info = None self.thread = None self.pbar = None self.setText(0, self.filepath) self.send_thread() def send_thread(self): if self.thread is None: self.thread = FilepathSearchThread(self.treeWidget()) self.thread.finished.connect(self.receive_thread) thread_ready = self.thread.wait(10000) if thread_ready: tree = self.treeWidget() self.pbar = QProgressBar(tree) self.pbar.setRange(0, 0) tree.setItemWidget(self, 1, self.pbar) self.thread.filepath = self.filepath self.thread.start() def receive_thread(self): self.media_info = self.thread.result self.pbar.reset() tree = self.treeWidget() tree.removeItemWidget(self, 1) self.pbar.deleteLater() self.refresh() def _replace_invalid_chars(self, text): for k, v in FileItem.invalid_char_map.items(): if k in text: text = text.replace(k, v) return text def refresh(self): moviedir = self.mediaman.moviedir() tvdir = self.mediaman.tvdir() if self.media_info: title = self.media_info.get('title') title = self._replace_invalid_chars(title) year = self.media_info.get('year') filename, ext = os.path.splitext(os.path.basename(self.filepath)) if self.episode_info or self.media_info.get('episodes'): if not self.episode_info: episodes = self.media_info['episodes'] ep_match = None for reg in FileItem.re_filename_episodes: m = reg.search(filename) if m: ep_match = m.groupdict() break if not ep_match: for reg in FileItem.re_filepath_episodes: m = reg.search(self.filepath) if m: ep_match = m.groupdict() if ep_match: season, episode = int(ep_match['season']), int( ep_match['episode']) for episode_info in episodes: if (episode_info['season'], episode_info['episode']) == (season, episode): self.episode_info = episode_info break if self.episode_info: self.renamed_filepath = ur'{tvdir}\{title}\Season{season:02d}\{title}.S{season:02d}E{episode:02d}.{episode_title}{ext}'.format( tvdir=tvdir, title=title, year=year, season=self.episode_info['season'], episode=self.episode_info['episode'], episode_title=self._replace_invalid_chars( self.episode_info['title']), ext=ext, ) else: self.renamed_filepath = ur'{tvdir}\{title}\Season00\{title}.S00E00.XXX{ext}'.format( tvdir=tvdir, title=title, year=year, ext=ext, ) else: self.renamed_filepath = ur'{moviedir}\{title} ({year})\{title}{ext}'.format( moviedir=moviedir, title=title, year=year, ext=ext) self.setText(1, self.renamed_filepath) if os.path.exists(self.renamed_filepath): self.setBackgroundColor( FileItem.cols.index('Renamed Filepath'), QColor(0, 255, 0, 100))
class FileItem(QTreeWidgetItem): cols = ['Filepath', 'Renamed Filepath'] invalid_char_map = {':': ''} re_filename_episodes = [ re.compile(r'.*[Ss](?P<season>\d{1,2})[\s\-_]*[Ee](?P<episode>\d{1,2}).*', re.I), re.compile(r'.*season\s*(?P<season>\d{1,2})[\s\-_]*episode\s*[Ee](?P<episode>\d{1,2}).*', re.I), ] re_filepath_episodes = [ re.compile(r'.*season\s*(?P<season>\d{1,2})\\(?:season\s*\d{1,2}[\s\-_]*)?(?:episode|ep\.?|e)\s*(?P<episode>\d{1,2}).*', re.I), ] def __init__(self, parent, filepath, mediaman): super(FileItem, self).__init__(parent) self._renamed = False self.mediaman = mediaman self.filepath = filepath self.renamed_filepath = None self.media_info = None self.episode_info = None self.thread = None self.pbar = None self.setText(0, self.filepath) self.send_thread() def send_thread(self): if self.thread is None: self.thread = FilepathSearchThread(self.treeWidget()) self.thread.finished.connect(self.receive_thread) thread_ready = self.thread.wait(10000) if thread_ready: tree = self.treeWidget() self.pbar = QProgressBar(tree) self.pbar.setRange(0, 0) tree.setItemWidget(self, 1, self.pbar) self.thread.filepath = self.filepath self.thread.start() def receive_thread(self): self.media_info = self.thread.result self.pbar.reset() tree = self.treeWidget() tree.removeItemWidget(self, 1) self.pbar.deleteLater() self.refresh() def _replace_invalid_chars(self, text): for k, v in FileItem.invalid_char_map.items(): if k in text: text = text.replace(k, v) return text def refresh(self): moviedir = self.mediaman.moviedir() tvdir = self.mediaman.tvdir() if self.media_info: title = self.media_info.get('title') title = self._replace_invalid_chars(title) year = self.media_info.get('year') filename, ext = os.path.splitext(os.path.basename(self.filepath)) if self.episode_info or self.media_info.get('episodes'): if not self.episode_info: episodes = self.media_info['episodes'] ep_match = None for reg in FileItem.re_filename_episodes: m = reg.search(filename) if m: ep_match = m.groupdict() break if not ep_match: for reg in FileItem.re_filepath_episodes: m = reg.search(self.filepath) if m: ep_match = m.groupdict() if ep_match: season, episode = int(ep_match['season']), int(ep_match['episode']) for episode_info in episodes: if (episode_info['season'], episode_info['episode']) == (season, episode): self.episode_info = episode_info break if self.episode_info: self.renamed_filepath = ur'{tvdir}\{title}\Season{season:02d}\{title}.S{season:02d}E{episode:02d}.{episode_title}{ext}'.format( tvdir=tvdir, title=title, year=year, season=self.episode_info['season'] , episode=self.episode_info['episode'], episode_title=self._replace_invalid_chars(self.episode_info['title']), ext=ext, ) else: self.renamed_filepath = ur'{tvdir}\{title}\Season00\{title}.S00E00.XXX{ext}'.format( tvdir=tvdir, title=title, year=year, ext=ext, ) else: self.renamed_filepath = ur'{moviedir}\{title} ({year})\{title}{ext}'.format( moviedir=moviedir, title=title, year=year, ext=ext ) self.setText(1, self.renamed_filepath) if os.path.exists(self.renamed_filepath): self.setBackgroundColor(FileItem.cols.index('Renamed Filepath'), QColor(0, 255, 0, 100))