diff --git a/youtube_dl_gui/downloadmanager.py b/youtube_dl_gui/downloadmanager.py index 05446f1..ef58b61 100644 --- a/youtube_dl_gui/downloadmanager.py +++ b/youtube_dl_gui/downloadmanager.py @@ -295,9 +295,7 @@ class DownloadManager(Thread): WAIT_TIME (float): Time in seconds to sleep. Args: - urls_list (list): Python list that contains multiple dictionaries - with the url to download and the corresponding row(index) in - which the worker should send the download process information. + download_list (DownloadList): List that contains items to download. opt_manager (optionsmanager.OptionsManager): Object responsible for managing the youtubedlg options. @@ -309,11 +307,11 @@ class DownloadManager(Thread): WAIT_TIME = 0.1 - def __init__(self, urls_list, opt_manager, log_manager=None): + def __init__(self, download_list, opt_manager, log_manager=None): super(DownloadManager, self).__init__() self.opt_manager = opt_manager self.log_manager = log_manager - self.urls_list = urls_list + self.download_list = download_list self._time_it_took = 0 self._successful = 0 @@ -322,7 +320,7 @@ class DownloadManager(Thread): # Init the custom workers thread pool log_lock = None if log_manager is None else Lock() wparams = (opt_manager, self._youtubedl_path(), log_manager, log_lock) - self._workers = [Worker(*wparams) for i in xrange(opt_manager.options['workers_number'])] + self._workers = [Worker(*wparams) for _ in xrange(opt_manager.options["workers_number"])] self.start() @@ -342,16 +340,22 @@ class DownloadManager(Thread): self._time_it_took = time.time() while self._running: - for worker in self._workers: - if worker.available() and self.urls_list: - worker.download(self.urls_list.pop(0)) + item = self.download_list.fetch_next() + + if item is not None: + # TODO create a get_worker method? + for worker in self._workers: + if worker.available(): + worker.download(item.url, item.options, item.object_id) + item.stage = "Active" #TODO Add change_stage method to download list + break + else: + if self._jobs_done(): + break time.sleep(self.WAIT_TIME) - if not self.urls_list and self._jobs_done(): - break - - self._talk_to_gui('report_active') + self._talk_to_gui("report_active") #TODO Use wx.Timer for this # Close all the workers for worker in self._workers: @@ -376,14 +380,14 @@ class DownloadManager(Thread): active_items = (workers that work) + (items waiting in the url_list). """ - counter = 0 - for worker in self._workers: - if not worker.available(): - counter += 1 + #counter = 0 + #for worker in self._workers: + #if not worker.available(): + #counter += 1 - counter += len(self.urls_list) + #counter += len(self.download_list) - return counter + return len(self.download_list) def stop_downloads(self): """Stop the download process. Also send 'closing' @@ -398,7 +402,7 @@ class DownloadManager(Thread): self._running = False def add_url(self, url): - """Add given url to the urls_list. + """Add given url to the download_list. Args: url (dict): Python dictionary that contains two keys. @@ -407,7 +411,7 @@ class DownloadManager(Thread): download process. """ - self.urls_list.append(url) + self.download_list.append(url) def send_to_worker(self, data): """Send data to the Workers. @@ -500,6 +504,7 @@ class Worker(Thread): self._options_parser = OptionsParser() self._successful = 0 self._running = True + self._options = None self._wait_for_reply = False @@ -524,19 +529,19 @@ class Worker(Thread): def run(self): while self._running: if self._data['url'] is not None: - options = self._options_parser.parse(self.opt_manager.options) - ret_code = self._downloader.download(self._data['url'], options) + #options = self._options_parser.parse(self.opt_manager.options) + ret_code = self._downloader.download(self._data['url'], self._options) if (ret_code == YoutubeDLDownloader.OK or ret_code == YoutubeDLDownloader.ALREADY): self._successful += 1 # Ask GUI for name updates - self._talk_to_gui('receive', {'source': 'filename', 'dest': 'new_filename'}) + #self._talk_to_gui('receive', {'source': 'filename', 'dest': 'new_filename'}) # Wait until you get a reply - while self._wait_for_reply: - time.sleep(self.WAIT_TIME) + #while self._wait_for_reply: + #time.sleep(self.WAIT_TIME) self._reset() @@ -545,7 +550,7 @@ class Worker(Thread): # Call the destructor function of YoutubeDLDownloader object self._downloader.close() - def download(self, item): + def download(self, url, options, object_id): """Download given item. Args: @@ -555,8 +560,9 @@ class Worker(Thread): download process. """ - self._data['url'] = item['url'] - self._data['index'] = item['index'] + self._data['url'] = url + self._options = options + self._data['index'] = object_id def stop_download(self): """Stop the download process of the worker. """ @@ -631,6 +637,7 @@ class Worker(Thread): temp_dict[key] = data[key] # Build the playlist status if there is an update + # TODO re-implement this on DownloadItem or ListCtrl level? if self._data['playlist_index'] is not None: if 'status' in temp_dict or 'playlist_index' in temp_dict: temp_dict['status'] = '{status} {index}/{size}'.format( diff --git a/youtube_dl_gui/mainframe.py b/youtube_dl_gui/mainframe.py index 4d20ff0..0686d10 100644 --- a/youtube_dl_gui/mainframe.py +++ b/youtube_dl_gui/mainframe.py @@ -365,6 +365,7 @@ class MainFrame(wx.Frame): selected_download_item = self._download_list.get_item(object_id) if selected_download_item.stage == "Completed": + # TODO Check if list has items filename = selected_download_item.get_files()[-1] open_file(filename) else: @@ -424,7 +425,10 @@ class MainFrame(wx.Frame): self._status_list._update_from_item(selected_row, download_item) def _on_start(self, event): - raise Exception("Implement me!") + if self.download_manager is None: + self._start_download() + else: + self.download_manager.stop_downloads() def _on_savepath(self, event): dlg = wx.DirDialog(self, self.CHOOSE_DIRECTORY, self._path_combobox.GetStringSelection(), wx.DD_CHANGE_DIR) @@ -634,7 +638,9 @@ class MainFrame(wx.Frame): def _reset_widgets(self): """Resets GUI widgets after update or download process. """ - pass + self._buttons["start"].SetLabel("Start") + icon = wx.Bitmap(os.path.join(self._pixmaps_path, "cloud_download_32px.png")) + self._buttons["start"].SetBitmap(icon, wx.TOP) #self._download_btn.SetLabel(self.DOWNLOAD_LABEL) #self._download_btn.Enable() #self._update_btn.Enable() @@ -687,11 +693,18 @@ class MainFrame(wx.Frame): """ signal, data = msg.data - if signal == 'send': - self._status_list.write(data) + download_item = self._download_list.get_item(data["index"]) + download_item.update_stats(data) + #TODO Add get index from object_id on download_list instead + row = self._download_list._items_list.index(data["index"]) - if signal == 'receive': - self.download_manager.send_to_worker(self._status_list.get(data)) + self._status_list._update_from_item(row, download_item) + + #if signal == 'send': + #self._status_list.write(data) + + #if signal == 'receive': + #self.download_manager.send_to_worker(self._status_list.get(data)) def _download_manager_handler(self, msg): """downloadmanager.DownloadManager thread handler. @@ -746,22 +759,17 @@ class MainFrame(wx.Frame): return [line for line in self._url_list.GetValue().split('\n') if line] def _start_download(self): - """Handles pre-download tasks & starts the download process. """ - self._status_list.clear() - self._status_list.load_urls(self._get_urls()) - if self._status_list.is_empty(): - self._create_popup(self.PROVIDE_URL_MSG, + self._create_popup("No items to download", self.ERROR_LABEL, wx.OK | wx.ICON_EXCLAMATION) else: - self.download_manager = DownloadManager(self._status_list.get_items(), - self.opt_manager, - self.log_manager) + self.download_manager = DownloadManager(self._download_list, self.opt_manager, self.log_manager) self._status_bar_write(self.DOWNLOAD_STARTED) - #self._download_btn.SetLabel(self.STOP_LABEL) - #self._update_btn.Disable() + self._buttons["start"].SetLabel(self.STOP_LABEL) + icon = wx.Bitmap(os.path.join(self._pixmaps_path, "stop_32px.png")) + self._buttons["start"].SetBitmap(icon, wx.TOP) def _paste_from_clipboard(self): """Paste the content of the clipboard to the self._url_list widget. @@ -993,12 +1001,12 @@ class ListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin): self._list_index += 1 def _update_from_item(self, row, download_item): - print row, download_item.object_id + #print row, download_item.object_id for key in download_item.progress_stats: column = self.columns[key][0] #TODO remove line below - print row, column, download_item.progress_stats[key] + #print row, column, download_item.progress_stats[key] self.SetStringItem(row, column, download_item.progress_stats[key]) def add_url(self, url):