def __onClickStartButton(self, event): self.__dm = DownloadManager(self, self.__downloadList, self.__dirText.GetValue()) self.__dm.start() self.__downloading = True self.__prefCombobox.Clear() self.SetStatusText("Downloading...")
def download_data(self): self.parentWidget().parentWidget().switch_to_progressview() self.download_manager = DownloadManager() self.thread = QtCore.QThread() self.download_manager.moveToThread(self.thread) self.thread.started.connect(self.download_manager.start) self.download_manager.hero_loaded.connect( self.parentWidget().parentWidget().progress.update) self.download_manager.job_done.connect(self.thread.quit) self.download_manager.job_done.connect(self.data_loaded) self.thread.start()
def _start_download(self): if self._status_list.is_empty(): self._create_popup(_("No items to download"), self.WARNING_LABEL, QMessageBox.OK) else: self._app_timer.start(100) self.download_manager = DownloadManager(self, self._download_list, self.opt_manager, self.log_manager) self.download_manager.sig_callafter.connect( self._download_manager_handler) self.download_manager.sig_worker_callafter.connect( self._download_worker_handler) self._status_bar_write(self.DOWNLOAD_STARTED) self._buttons["start"].setText(self.STOP_LABEL) self._buttons["start"].setToolTip(self.STOP_LABEL) self._buttons["start"].setIcon(self._bitmaps["stop"])
def __init__(self, process, sess=None): self.sess = sess def _process(meta, status, data): self.cache_tile(meta['tile'], status, data, meta.get('cache', False), meta.get('overwrite', False)) process(meta, status, data) return (True, None) self.dlmgr = DownloadManager(limit=100) self.dlpxr = DownloadProcessor(self.dlmgr, _process) self.started = False
class DownloadService(object): def __init__(self, process, sess=None): self.sess = sess def _process(meta, status, data): self.cache_tile(meta['tile'], status, data, meta.get('cache', False), meta.get('overwrite', False)) process(meta, status, data) return (True, None) self.dlmgr = DownloadManager(limit=100) self.dlpxr = DownloadProcessor(self.dlmgr, _process) self.started = False def add(self, key, url): if not self.started: self.start() self.dlmgr.enqueue((key, url)) def cache_tile(self, t, status, data, cache, overwrite): if cache: if overwrite or not self.sess.query(mt.Tile).get(t.pk()): try: _process_tile(self.sess, t, status, data) except: pass def start(self): self.started = True self.dlmgr.start() self.dlpxr.start() def terminate(self): self.dlmgr.terminate() self.dlpxr.terminate()
def download_data(self): self.parentWidget().parentWidget().switch_to_progressview() self.download_manager = DownloadManager() self.thread = QtCore.QThread() self.download_manager.moveToThread(self.thread) self.thread.started.connect(self.download_manager.start) self.download_manager.hero_loaded.connect(self.parentWidget().parentWidget().progress.update) self.download_manager.job_done.connect(self.thread.quit) self.download_manager.job_done.connect(self.data_loaded) self.thread.start()
def __init__(self, tiles, sess): threading.Thread.__init__(self) self.tiles = tiles self.num_tiles = len(tiles) # error count and last error are displayed in the curses interface self.error_count = 0 self.last_error = None self.dlmgr = DownloadManager(limit=100) def process(key, status, data): return process_tile(sess, key, status, data) self.dlpxr = DownloadProcessor(self.dlmgr, process, self.num_tiles, self.onerror)
class TileDownloader(threading.Thread): """a monitorable thread that downloads and processes tiles""" def __init__(self, tiles, sess): threading.Thread.__init__(self) self.tiles = tiles self.num_tiles = len(tiles) # error count and last error are displayed in the curses interface self.error_count = 0 self.last_error = None self.dlmgr = DownloadManager(limit=100) def process(key, status, data): return process_tile(sess, key, status, data) self.dlpxr = DownloadProcessor(self.dlmgr, process, self.num_tiles, self.onerror) def run(self): self.dlmgr.start() self.dlpxr.start() for t in random_walk(self.tiles): layer, z, x, y = t tile = mt.Tile(layer=layer, z=z, x=x, y=y) self.dlmgr.enqueue((tile, tile.url())) self.dlpxr.join() self.dlmgr.terminate() self.dlmgr.join() def onerror(self, msg): self.error_count += 1 self.last_error = msg def status(self): return (self.dlpxr.count, self.num_tiles, self.error_count)
class Pool(object): PARAMS={'directory':'../downloads','web_path':'downloads'} DOWNLOADERS=[TorrentDownloader()] def __init__(self): self.dm=DownloadManager(self.DOWNLOADERS) self.pool=[] self.lock=threading.Semaphore() def save_state(self,filename): with open(filename,'w') as outfile: json.dump([dl.get_state() for dl in pool],outfile) def load_state(self,filename): with open(filename,'r') as infile: pool_state=json.load(infile) pool=[self.dm.restore_state(s) for s in pool_state] # pool command def get_pool(self): return [dl._describe() for dl in self.pool] # add command def add(self,type,args={}): dl_inst=self.dm.instantiate(self,type,args) self.pool.append(dl_inst) return {'uid':dl_inst.uid} def tell_download(self,uid,cmd,args={}): return self.dm.tell(self.find_dl(uid),cmd,args) def available_downloaders(self): return [dlr.TYPE_STRING for dlr in dm.downloaders] def describe_download(self,uid): return self.find_dl(uid)._describe() def rm(self,uid): return self.find_dl(uid).rm() def pause(self,uid): return self.find_dl(uid).rm() def find_dl(self,uid): uid=int(uid) d=dict([(dl.uid,dl) for dl in self.pool]) if uid not in d: raise Exception("Download identifier does not exist") return d[uid] # Runs a set of commands. Pool is guarenteed to not change during them. def doMultipleCommandsAsync(self,commands): return self.sync(lambda:[self.doCommand(cmd) for cmd in commands]) # Acquires this object's lock and executes the given cmd def sync(self,cmd): try: self.lock.acquire() result=cmd() except Exception: self.lock.release() raise self.lock.release() return result # Removes a download asynchronously def removeMeAsync(self,uid): self.sync(lambda:self.removeMe(uid)) def removeMe(self,uid): self.pool=[obj for obj in self.pool if obj.uid != uid] # Parse and run a command def doCommand(self,line): if not isinstance(line,dict): return errorPacket('Command not a dict.') try: cmd=line['cmd'] # Fails if no cmd given except KeyError: return errorPacket('No command given.') try: args=line['args'] except KeyError: args={} if not isinstance(args,dict): return errorPacket('Argument list not a dict.') try: f=self.commands[cmd] except KeyError: return errorPacket('Bad command.') try: result=f(self,**args) except Exception as e: raise return errorPacket(str(e)) return goodPacket(result) commands={ 'rm':rm, 'pause':pause, 'add':add, 'pool':get_pool, 'tell':tell_download, 'describe':describe_download, }
def __init__(self): self.dm=DownloadManager(self.DOWNLOADERS) self.pool=[] self.lock=threading.Semaphore()
class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, title="YouTube Downloader", size=(FRAME_WIDTH, FRAME_HEIGHT), \ style=wx.DEFAULT_FRAME_STYLE) self.SetMinSize((FRAME_WIDTH, FRAME_HEIGHT)) self.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_CLOSE, self.__onClose) panel = wx.Panel(self) vBox = wx.BoxSizer(wx.VERTICAL) hBoxes = [] for i in range(5): # 5 boxsizer to place attributes properly hBoxes.append(wx.BoxSizer(wx.HORIZONTAL)) sourceLabel = wx.StaticText(panel, label="URLs:") self.__addButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/addButtonIcon.png")), style=wx.NO_BORDER) self.__addButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickAddButton, self.__addButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanAdd, self.__addButton) self.__playlistButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/playlistButtonIcon.png")), style=wx.NO_BORDER) self.__playlistButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickPlaylistButton, self.__playlistButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanAddPlaylist, self.__playlistButton) addBox = wx.BoxSizer(wx.HORIZONTAL) addBox.Add(self.__addButton, flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border=8) addBox.Add(self.__playlistButton, flag=wx.ALIGN_CENTER_VERTICAL) # labelGridSizer includes attributes that place on the top labelGridSizer = wx.GridSizer(cols=3) labelGridSizer.Add(sourceLabel, 0, wx.ALIGN_LEFT) labelGridSizer.Add(wx.StaticText(panel, size=(wx.GetDisplaySize().Width, -1)), 0, wx.EXPAND | wx.ALIGN_CENTER) labelGridSizer.Add(addBox, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT) hBoxes[0].Add(labelGridSizer, flag=wx.EXPAND) vBox.Add(hBoxes[0], flag=wx.ALL, border=10) # text field to input urls self.__sourceText = wx.TextCtrl(panel, size=(-1, wx.GetDisplaySize().Height), style=wx.TE_MULTILINE) hBoxes[1].Add(self.__sourceText, proportion=1) vBox.Add(hBoxes[1], proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=10) # a button to change download directory dirBox = wx.BoxSizer(wx.HORIZONTAL) self.__changeDirButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/changeDirButtonIcon.png")), style=wx.NO_BORDER) self.__changeDirButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickChangeDirButton, self.__changeDirButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanChangeDir, self.__changeDirButton) dirBox.Add(self.__changeDirButton, flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border=8) defaultDir = "" # set default download directory if os.path.exists(CONFIGS): # if the user has already set default directory, read it conf = JsonUtil(CONFIGS).read() defaultDir = conf['dir'] if not os.path.exists(defaultDir): # if saved default directory is corrupt, remove it and let user reset it os.remove(CONFIGS) os.execl(sys.executable, sys.executable, *sys.argv) # restart this program else: # otherwise, make the user set default directory dialog = wx.DirDialog(None) if dialog.ShowModal() == wx.ID_OK: defaultDir = dialog.GetPath() if os.name == "nt": # setting directory for Windows if not defaultDir.endswith("\\"): defaultDir += "\\" else: # for Linux or macOS if not defaultDir.endswith("/"): defaultDir += "/" conf = { 'dir': defaultDir } JsonUtil(CONFIGS).write(conf) else: # if the user click cancel, program should be exited self.Destroy() dialog.Destroy() # this text shows currently selected download directory self.__dirText = wx.TextCtrl(panel, value=defaultDir, size=(300, -1), style=wx.TE_READONLY) dirBox.Add(self.__dirText) # a meaningless icon optBox = wx.BoxSizer(wx.HORIZONTAL) prefIcon = wx.StaticBitmap(panel, -1, wx.Bitmap(resource_path("images/changePrefIcon.png"))) prefIcon.SetBackgroundColour(BACKGROUND_COLOR) optBox.Add(prefIcon, flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border=8) # a combobox which includes all available stream options that are available on selected video self.__prefCombobox = wx.ComboBox(panel, size=(200, -1), style=wx.CB_DROPDOWN | wx.TE_READONLY) self.Bind(wx.EVT_COMBOBOX, self.__onSelectOption, self.__prefCombobox) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanChangeOption, self.__prefCombobox) optBox.Add(self.__prefCombobox) # optionGridSizer includes attributes which place on the center optionGridSizer = wx.GridSizer(cols=3) optionGridSizer.Add(dirBox, 0, wx.ALIGN_LEFT) optionGridSizer.Add(wx.StaticText(panel, size=(wx.GetDisplaySize().Width, -1)), 0, wx.EXPAND | wx.ALIGN_CENTER) optionGridSizer.Add(optBox, 0, wx.ALIGN_RIGHT) hBoxes[2].Add(optionGridSizer, flag=wx.EXPAND) vBox.Add(hBoxes[2], flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=10) # a tabled list which includes download list self.__addedList = wx.ListCtrl(panel, style=wx.LC_REPORT | wx.BORDER_DOUBLE) cols = [ "제목", "저자", "길이", "옵션", "크기", "속도", "진행률", "남은 시간", "" ] # an empty column not to spoil UI when resizing columnWidths = [ 230, 80, 70, 180, 70, 85, 60, 70, wx.GetDisplaySize().Width ] for i in range(len(cols)): self.__addedList.InsertColumn(i, cols[i], wx.TEXT_ALIGNMENT_RIGHT if i == 0 else wx.TEXT_ALIGNMENT_CENTER) self.__addedList.SetColumnWidth(i, columnWidths[i]) self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.__onSelectItem, self.__addedList) hBoxes[3].Add(self.__addedList, flag=wx.EXPAND | wx.LEFT | wx.RIGHT) vBox.Add(hBoxes[3], flag=wx.LEFT | wx.RIGHT, border=10) vBox.Add((-1, 10)) # add 6 buttons (start, pause, skip, stop, info, remove) self.__startButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/startButtonIcon.png")), style=wx.NO_BORDER) self.__startButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickStartButton, self.__startButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanStart, self.__startButton) hBoxes[4].Add(self.__startButton, flag=wx.RIGHT, border=12) self.__pauseButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/pauseButtonIcon.png")), style=wx.NO_BORDER) self.__pauseButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickPauseButton, self.__pauseButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanPause, self.__pauseButton) hBoxes[4].Add(self.__pauseButton, flag=wx.RIGHT, border=12) self.__skipButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/skipButtonIcon.png")), style=wx.NO_BORDER) self.__skipButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickSkipButton, self.__skipButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanSkip, self.__skipButton) hBoxes[4].Add(self.__skipButton, flag=wx.RIGHT, border=12) self.__stopButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/stopButtonIcon.png")), style=wx.NO_BORDER) self.__stopButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickStopButton, self.__stopButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanStop, self.__stopButton) hBoxes[4].Add(self.__stopButton, flag=wx.RIGHT, border=12) self.__infoButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/infoButtonIcon.png")), style=wx.NO_BORDER) self.__infoButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickInfoButton, self.__infoButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanShowInfo, self.__infoButton) hBoxes[4].Add(self.__infoButton, flag=wx.RIGHT, border=12) self.__removeButton = wx.BitmapButton(panel, -1, wx.Bitmap(resource_path("images/removeButtonIcon.png")), style=wx.NO_BORDER) self.__removeButton.SetBackgroundColour(BACKGROUND_COLOR) self.Bind(wx.EVT_BUTTON, self.__onClickRemoveButton, self.__removeButton) self.Bind(wx.EVT_UPDATE_UI, self.__onCheckCanRemove, self.__removeButton) hBoxes[4].Add(self.__removeButton) vBox.Add(hBoxes[4], flag=wx.ALIGN_RIGHT | wx.RIGHT, border=10) vBox.Add((-1, 10)) panel.SetSizer(vBox) # status bar to show events self.CreateStatusBar() self.GetStatusBar().SetBackgroundColour(BACKGROUND_COLOR) self.SetStatusText("") self.__downloadList = [] self.__downloading = False self.__plm = PlaylistManager() self.__am = None # AddManager for adding urls self.__dm = None # DownloadManager for downloading videos self._lock = threading.Lock() self.Center() self.Show() PlaylistDialog(self, self.__plm).autoAddPlaylist() # stop all threads before force close def __onClose(self, event): if self.__am and self.__am.is_alive(): self.__am.stop() if self.__dm and self.__dm.is_alive(): self.__dm.pause() self.Destroy() # UI updater for AddButton def __onCheckCanAdd(self, event): event.Enable(not self.__downloading and self.__sourceText.GetValue() != "" and \ (True if self.__am is None else not self.__am.is_alive())) # UI updater for PlaylistButton def __onCheckCanAddPlaylist(self, event): pass # PlaylistButton can always be clickable # UI updater for ChangeDirButton def __onCheckCanChangeDir(self, event): event.Enable(not self.__downloading) # UI updater for PrefCombobox def __onCheckCanChangeOption(self, event): event.Enable(not self.__downloading and self.__addedList.GetSelectedItemCount() == 1) # UI updater for StartButton def __onCheckCanStart(self, event): event.Enable(not self.__downloading and len(self.__downloadList) > 0 and \ self.__am is not None and not self.__am.is_alive()) # UI updater for PauseButton def __onCheckCanPause(self, event): event.Enable(self.__downloading) # UI updater for SkipButton def __onCheckCanSkip(self, event): event.Enable(self.__downloading) # UI updater for StopButton def __onCheckCanStop(self, event): event.Enable(self.__downloading and self.__addedList.GetSelectedItemCount() == 1 and \ self.__dm is not None and self.__dm.is_alive() and \ self.__dm.isDownloading(self.__addedList.GetFocusedItem())) # UI updater for InfoButton def __onCheckCanShowInfo(self, event): event.Enable(self.__addedList.GetSelectedItemCount() > 0) # UI updater for RemoveButton def __onCheckCanRemove(self, event): event.Enable(not self.__downloading and self.__addedList.GetSelectedItemCount() > 0 and \ self.__am is not None and not self.__am.is_alive()) # event handler for AddButton def __onClickAddButton(self, event): urls = [] for i in range(self.__sourceText.GetNumberOfLines()): if self.__sourceText.GetLineText(i) != "": # skip blank urls.append(self.__sourceText.GetLineText(i)) self.__am = AddManager(self, self.__plm, urls, self._lock) self.__am.start() self.__sourceText.Clear() # event handler for PlaylistButton def __onClickPlaylistButton(self, event): PlaylistDialog(self, self.__plm).show(self.GetPosition().x + self.GetSize().Width, \ self.GetPosition().y) # event handler for StartButton def __onClickStartButton(self, event): self.__dm = DownloadManager(self, self.__plm, self.__downloadList, self.__dirText.GetValue(), self._lock) self.__dm.start() self.__downloading = True self.__prefCombobox.Clear() self.SetStatusText("Downloading...") # event handler for PauseButton def __onClickPauseButton(self, event): self.__dm.pause() self.SetStatusText("Paused.") self.__downloading = False # event handler for SkipButton def __onClickSkipButton(self, event): self.__dm.skip() # event handler for StopButton def __onClickStopButton(self, event): self.__dm.stop(self.__addedList.GetFirstSelected()) # event handler for InfoButton def __onClickInfoButton(self, event): selectedItem = self.__addedList.GetFirstSelected() x = 0 # x-coordinate to move info dialog if multiple items are selected y = 0 # y-coordinate to move info dialog if multiple items are selected while True: # do-while loop to show information dialogs for multiple selected items VideoInfoDialog(self.__downloadList[selectedItem].video, x, y).show() selectedItem = self.__addedList.GetNextSelected(selectedItem) x += 30 y += 30 if selectedItem < 0: break # event handler for RemoveButton def __onClickRemoveButton(self, event): selectedItem = self.__addedList.GetFirstSelected() removeList = [] while selectedItem >= 0: # get every index to remove removeList.append((selectedItem, self.__downloadList[selectedItem])) selectedItem = self.__addedList.GetNextSelected(selectedItem) removeList.sort(reverse=True) # sort remove list reversely to remove safely by starting from latest index for itemTuple in removeList: self.SetStatusText(itemTuple[1].video.title + " has been removed.") self.__downloadList.remove(itemTuple[1]) self.__addedList.DeleteItem(itemTuple[0]) # event handler for ChangeDirButton def __onClickChangeDirButton(self, event): dialog = wx.DirDialog(None, defaultPath=self.__dirText.GetValue()) if dialog.ShowModal() == wx.ID_OK: defaultDir = dialog.GetPath() if os.name == "nt": # setting directory for Windows self.__dirText.SetValue(defaultDir + "\\" \ if not defaultDir.endswith("\\") else defaultDir) else: # for Linux or macOS self.__dirText.SetValue(defaultDir + "/" \ if not defaultDir.endswith("/") else defaultDir) conf = JsonUtil(CONFIGS).read() conf['dir'] = self.__dirText.GetValue() JsonUtil(CONFIGS).write(conf) dialog.Destroy() # event handler for selecting an item -> update PrefCombobox list def __onSelectItem(self, event): self.__prefCombobox.Clear() if not self.__downloading and self.__addedList.GetSelectedItemCount() == 1: selectedItem = self.__downloadList[self.__addedList.GetFocusedItem()] self.__prefCombobox.AppendItems(selectedItem.options) self.__prefCombobox.SetValue(selectedItem.selectedExt) # event handler for selecting an option in PrefCombobox -> update selected item's selected extension def __onSelectOption(self, event): selectedItem = self.__addedList.GetFocusedItem() self.__downloadList[selectedItem].setSelectedExt(self.__prefCombobox.GetStringSelection()) self.__addedList.SetItem(selectedItem, 3, self.__downloadList[selectedItem].selectedExt) self.__addedList.SetItem(selectedItem, 4, self.__downloadList[selectedItem].filesize) # add item to download list def addToDownloadList(self, item): # if video is already in download list, skip it if item.video.title not in [ d.video.title for d in self.__downloadList ]: self.__downloadList.append(item) num_items = self.__addedList.GetItemCount() self.__addedList.InsertItem(num_items, item.video.title) self.__addedList.SetItem(num_items, 1, item.video.author) self.__addedList.SetItem(num_items, 2, item.video.duration) self.__addedList.SetItem(num_items, 3, item.selectedExt) self.__addedList.SetItem(num_items, 4, item.filesize) self.SetStatusText(item.video.title + " has been added.") else: self.SetStatusText(item.video.title + " is already in download list.") # add playlist to download list def addPlaylist(self, playlist): self.__am = AddManager(self, self.__plm, playlist, self._lock) self.__am.start() # update status of downloading item def updateStatus(self, item, rate, progress, eta): selectedItem = self.__downloadList.index(item) self.__addedList.SetItem(selectedItem, 5, rate) self.__addedList.SetItem(selectedItem, 6, progress) self.__addedList.SetItem(selectedItem, 7, eta) # remove item from list when downloaded def removeFinishedItem(self, item): self.__addedList.DeleteItem(self.__downloadList.index(item)) self.__downloadList.remove(item) # set finished when all videos are downloaded def setFinished(self): self.SetStatusText("Finished") self.__downloading = False # check it's possible to add playlist def isAddable(self): return not self.__downloading and (True if self.__am is None else not self.__am.is_alive())
class HeroPoolView(QtGui.QWidget): heroes_per_row=16 def __init__(self, parent=None): super(HeroPoolView, self).__init__(parent) def load_data_locally(self): try: self.heroes = DataManager().load_hero_data() except IOError: info = QtGui.QMessageBox('Datafile not found', 'I couldn\'t load the data file from within the current directory, do you want me to download it from the website?', QtGui.QMessageBox.Information, QtGui.QMessageBox.Ok, QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton, self) if info.exec_() == QtGui.QMessageBox.Ok: self.download_data() else: self.buttons = [] layout = QtGui.QGridLayout() layout.addWidget(self.create_searchfield(), 0, 13, 1, 3) for i, hero in enumerate(self.heroes): pb = HeroButton(hero, self) pb.left_click.connect(self.parentWidget().parentWidget().hero_selected) pb.right_click.connect(self.parentWidget().parentWidget().show_hero_info) layout.addWidget(pb, 1+i/HeroPoolView.heroes_per_row, i%HeroPoolView.heroes_per_row) self.buttons.append(pb) self.setLayout(layout) def create_searchfield(self): self.search = QtGui.QLineEdit(self) self.search.textChanged.connect(self.highlight_heroes) return self.search def highlight_heroes(self, text): for hero_button in self.buttons: if text in hero_button.hero.name and hero_button.hero.available: hero_button.setEnabled(True) else: hero_button.setEnabled(False) def download_data(self): self.parentWidget().parentWidget().switch_to_progressview() self.download_manager = DownloadManager() self.thread = QtCore.QThread() self.download_manager.moveToThread(self.thread) self.thread.started.connect(self.download_manager.start) self.download_manager.hero_loaded.connect(self.parentWidget().parentWidget().progress.update) self.download_manager.job_done.connect(self.thread.quit) self.download_manager.job_done.connect(self.data_loaded) self.thread.start() def data_loaded(self): heroes = self.download_manager.heroes DataManager().save_hero_data(heroes) self.load_data_locally() def get_hero_by_name(self, hero_name): return next((hero for hero in self.heroes if hero.name == hero_name), None) def hero_selected(self, hero): hero_button = next(hb for hb in self.buttons if hb.hero.name == hero.name) hero_button.setEnabled(False) hero_button.hero.available = False self.search.setText('') def hero_back_to_pool(self, hero): hero_button = next(hb for hb in self.buttons if hb.hero.name == hero.name) hero_button.setEnabled(True) hero_button.hero.available = True self.search.setText('') def get_available_heroes(self): return [hb.hero for hb in self.buttons if hb.hero.available] def disconnect_all(self): for hero_button in self.buttons: hero_button.left_click.disconnect(self.parentWidget().parentWidget().hero_selected)
class HeroPoolView(QtGui.QWidget): heroes_per_row = 16 def __init__(self, parent=None): super(HeroPoolView, self).__init__(parent) def load_data_locally(self): try: self.heroes = DataManager().load_hero_data() except IOError: info = QtGui.QMessageBox( 'Datafile not found', 'I couldn\'t load the data file from within the current directory, do you want me to download it from the website?', QtGui.QMessageBox.Information, QtGui.QMessageBox.Ok, QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton, self) if info.exec_() == QtGui.QMessageBox.Ok: self.download_data() else: self.buttons = [] layout = QtGui.QGridLayout() layout.addWidget(self.create_searchfield(), 0, 13, 1, 3) for i, hero in enumerate(self.heroes): pb = HeroButton(hero, self) pb.left_click.connect( self.parentWidget().parentWidget().hero_selected) pb.right_click.connect( self.parentWidget().parentWidget().show_hero_info) layout.addWidget(pb, 1 + i / HeroPoolView.heroes_per_row, i % HeroPoolView.heroes_per_row) self.buttons.append(pb) self.setLayout(layout) def create_searchfield(self): self.search = QtGui.QLineEdit(self) self.search.textChanged.connect(self.highlight_heroes) return self.search def highlight_heroes(self, text): for hero_button in self.buttons: if text in hero_button.hero.name and hero_button.hero.available: hero_button.setEnabled(True) else: hero_button.setEnabled(False) def download_data(self): self.parentWidget().parentWidget().switch_to_progressview() self.download_manager = DownloadManager() self.thread = QtCore.QThread() self.download_manager.moveToThread(self.thread) self.thread.started.connect(self.download_manager.start) self.download_manager.hero_loaded.connect( self.parentWidget().parentWidget().progress.update) self.download_manager.job_done.connect(self.thread.quit) self.download_manager.job_done.connect(self.data_loaded) self.thread.start() def data_loaded(self): heroes = self.download_manager.heroes DataManager().save_hero_data(heroes) self.load_data_locally() def get_hero_by_name(self, hero_name): return next((hero for hero in self.heroes if hero.name == hero_name), None) def hero_selected(self, hero): hero_button = next(hb for hb in self.buttons if hb.hero.name == hero.name) hero_button.setEnabled(False) hero_button.hero.available = False self.search.setText('') def hero_back_to_pool(self, hero): hero_button = next(hb for hb in self.buttons if hb.hero.name == hero.name) hero_button.setEnabled(True) hero_button.hero.available = True self.search.setText('') def get_available_heroes(self): return [hb.hero for hb in self.buttons if hb.hero.available] def disconnect_all(self): for hero_button in self.buttons: hero_button.left_click.disconnect( self.parentWidget().parentWidget().hero_selected)
class MainWindow(QMainWindow): #LABEL ADD_LABEL = _("Add") DOWNLOAD_LIST_LABEL = _("Download list") DELETE_LABEL = _("Delete") DELETEALL_LABEL = _("Clear") #PLAY_LABEL = _("Play") UP_LABEL = _("Up") DOWN_LABEL = _("Down") #RELOAD_LABEL = _("Reload") #PAUSE_LABEL = _("Pause") START_LABEL = _("Start") STOP_LABEL = _("Stop") INFO_LABEL = _("Info") ABOUT_LABEL = _("About") QUESTION_LABEL = _("Question") WARNING_LABEL = _("Warning") PROVIDE_URL_MSG = _("You need to provide at least one URL") QUIT_MSG = _("Are You sure to quit?") VIDEO_LABEL = _("Filename") EXTENSION_LABEL = _("Extension") SIZE_LABEL = _("Size") PERCENT_LABEL = _("Percent") ETA_LABEL = _("ETA") SPEED_LABEL = _("Speed") STATUS_LABEL = _("Status") URL_REPORT_MSG = _( "Total Progress: {0:.1f}% | Queued ({1}) Paused ({2}) Active ({3}) Completed ({4}) Error ({5})" ) CLOSING_MSG = _("Stopping downloads") DOWNLOAD_STARTED = _("Downloads started") UPDATING_MSG = _("Downloading latest youtube-dl. Please wait...") UPDATE_ERR_MSG = _("Youtube-dl download failed [{0}]") UPDATE_SUCC_MSG = _("Successfully downloaded youtube-dl") ################################# # STATUSLIST_COLUMNS # # Dictionary which contains the columns for the wxListCtrl widget. # Each key represents a column and holds informations about itself. # Structure informations: # column_key: (column_number, column_label, minimum_width, is_resizable) # STATUSLIST_COLUMNS = { 'filename': (0, VIDEO_LABEL, 150, True), 'extension': (1, EXTENSION_LABEL, 60, False), 'filesize': (2, SIZE_LABEL, 80, False), 'percent': (3, PERCENT_LABEL, 65, False), 'eta': (4, ETA_LABEL, 45, False), 'speed': (5, SPEED_LABEL, 90, False), 'status': (6, STATUS_LABEL, 160, False) } def __init__(self): super(MainWindow, self).__init__() if getattr(sys, 'frozen', False): # we are running in a |PyInstaller| bundle basedir = sys._MEIPASS else: basedir = os.path.dirname(__file__) loadUi(os.path.join(basedir, "qtyoutube-dl.ui"), self) self.setWindowIcon(QIcon(":qtyoutube-dl")) #load setting #self.cfg = QSettings("coolshou.idv", "qtyoutube-dl") #self._savepath = "" #self.loadSettings() ################################### cfgpath = os.path.join( QStandardPaths.writableLocation( QStandardPaths.ApplicationsLocation), "qtyoutube-dl") if not os.path.exists(cfgpath): os.mkdir(cfgpath) self.opt_manager = opt_manager(cfgpath) self.log_manager = LogManager(cfgpath, self.opt_manager.options['log_time']) self.download_manager = None self.update_thread = None self.app_icon = None #REFACTOR Get and set on __init__.py self._download_list = DownloadList() self._status_list = ListCtrl(self.STATUSLIST_COLUMNS) self._status_list.rowsInserted.connect(self._update_btns) #self._status_list.selectionChanged.connect(self._on_selectionChanged) self.tv_status.setModel(self._status_list) self.tv_status.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeToContents) self.tv_status.clicked.connect(self._on_clicked) #self._status_selection = self.tv_status.selectionModel(); #self._status_selection.selectionChanged.connect(self._on_selectionChanged) #self.tv_status.activated.connect(self._on_activated) # Set up youtube-dl options parser self._options_parser = OptionsParser() # Set the Timer self._app_timer = QTimer(self) self._app_timer.timeout.connect(self._on_timer) #upadte ui self.setSettings() #self._url_list = [] ############################################ #button #self.le_savepath.textChanged.connect(self.updateSavePath) self.path_combobox.currentTextChanged.connect(self.updateSavePath) ################################### # set bitmap bitmap_data = (("add", ":/add"), ("down", ":/down"), ("up", ":/up"), ("copy", ":/copy"), ("delete", ":/delete"), ("start", ":/get"), ("stop", ":/stop"), ("pause", ":/pause"), ("resume", ":/resume"), ("qtyoutube-dl", ":/qtyoutube-dl"), ("setting", ":/setting"), ("trash", ":/trash"), ("info", ":/info")) # ("pause", ":/pause"), # ("resume", ":/resume") self._bitmaps = {} for item in bitmap_data: target, name = item #self._bitmaps[target] = wx.Bitmap(os.path.join(self._pixmaps_path, name)) self._bitmaps[target] = QIcon(name) # Dictionary to store all the buttons # Set the data for all the wx.Button items # name, label, size, event_handler buttons_data = (("delete", self.DELETE_LABEL, (-1, -1), self._on_delete, self.pb_del), ("clear", self.DELETEALL_LABEL, (-1, -1), self._on_delAll, self.pb_delAll), ("up", self.UP_LABEL, (-1, -1), self._on_arrow_up, self.pb_up), ("down", self.DOWN_LABEL, (-1, -1), self._on_arrow_down, self.pb_down), ("start", self.START_LABEL, (-1, -1), self._on_start, self.pb_Start), ("savepath", "...", (35, -1), self._on_savepath, self.pb_savepath), ("add", self.ADD_LABEL, (-1, -1), self._on_add, self.pb_add)) #("play", self.PLAY_LABEL, (-1, -1), self._on_play, QPushButton), #("reload", self.RELOAD_LABEL, (-1, -1), self._on_reload, QPushButton), #("pause", self.PAUSE_LABEL, (-1, -1), self._on_pause, QPushButton), self._buttons = {} for item in buttons_data: name, label, size, evt_handler, parent = item #button = parent(self._panel, size=size) button = parent if parent == QPushButton: button.setText(label) # elif parent == wx.BitmapButton: # button.setToolTip(wx.ToolTip(label)) # if name in self._bitmaps: # #button.SetBitmap(self._bitmaps[name], wx.TOP) # button.SetBitmap(self._bitmaps[name], wx.TOP) if evt_handler is not None: #button.Bind(wx.EVT_BUTTON, evt_handler) button.clicked.connect(evt_handler) self._buttons[name] = button self._path_combobox = self.path_combobox #test data #self.te_urls.append("https://www.youtube.com/watch?v=a9V0nl_ezLw") self._initAction() self.show() @pyqtSlot(QModelIndex, int, int) def _update_btns(self, idx, first, last): #print("%s : %s , %s" %(idx, first, last)) iSum = self._status_list.rowCount() if iSum >= 1: #print("iSum:%s" % iSum) self.pb_Start.setEnabled(True) else: self.pb_Start.setEnabled(False) def _update_pause_button(self, event): selected_rows = self._status_list.get_all_selected() label = _("Pause") bitmap = self._bitmaps["pause"] for row in selected_rows: object_id = self._status_list.GetItemData(row) download_item = self._download_list.get_item(object_id) if download_item.stage == "Paused": # If we find one or more items in Paused # state set the button functionality to resume label = _("Resume") bitmap = self._bitmaps["resume"] break self._buttons["pause"].setText(label) self._buttons["pause"].setToolTip(label) self._buttons["pause"].setIcon(bitmap) def _initAction(self): #Action menu #view-log #option-setting self.actionupdate.triggered.connect(self._on_update) self.actionAbout.triggered.connect(self._on_about) pass def _get_urls(self): """Returns urls list. """ return [ line for line in self.te_urls.toPlainText().split('\n') if line ] def _create_popup(self, icon, text, title, style): msg = QMessageBox(icon, title, text, style) msg.setWindowIcon(QIcon(":qtyoutube-dl")) return msg.exec() def _on_add(self): urls = self._get_urls() if not urls: self._create_popup(QMessageBox.Warning, self.PROVIDE_URL_MSG, self.WARNING_LABEL, QMessageBox.Ok) else: #self._url_list.Clear() options = self._options_parser.parse(self.opt_manager.options) for url in urls: #make sure it is http or https if "http://" in url or "https://" in url: download_item = DownloadItem(url, options) download_item.path = self.opt_manager.options["save_path"] if not self._download_list.has_item( download_item.object_id): self._status_list.bind_item(download_item) self._download_list.insert(download_item) #clear line def _on_arrow_up(self, event): index = self._status_list.get_next_selected() if index != -1: while index >= 0: object_id = self._status_list.GetItemData(index) download_item = self._download_list.get_item(object_id) new_index = index - 1 if new_index < 0: new_index = 0 if not self._status_list.IsSelected(new_index): self._download_list.move_up(object_id) self._status_list.move_item_up(index) self._status_list._update_from_item( new_index, download_item) index = self._status_list.get_next_selected(index) def _on_arrow_down(self, event): index = self._status_list.get_next_selected(reverse=True) if index != -1: while index >= 0: object_id = self._status_list.GetItemData(index) download_item = self._download_list.get_item(object_id) new_index = index + 1 if new_index >= self._status_list.GetItemCount(): new_index = self._status_list.GetItemCount() - 1 if not self._status_list.IsSelected(new_index): self._download_list.move_down(object_id) self._status_list.move_item_down(index) self._status_list._update_from_item( new_index, download_item) index = self._status_list.get_next_selected(index, True) def _on_delAll(self): self._status_list.Clear() self._download_list.clear() def _on_delete(self): #get select idx index = self._status_list.get_next_selected() if index == -1: dlg = ButtonsChoiceDialog( self, [_("Remove all"), _("Remove completed")], _("No items selected. Please pick an action"), _("Delete")) ret_code = dlg.ShowModal() dlg.Destroy() #REFACTOR Maybe add this functionality directly to DownloadList? if ret_code == 1: for ditem in self._download_list.get_items(): if ditem.stage != "Active": self._status_list.remove_row( self._download_list.index(ditem.object_id)) self._download_list.remove(ditem.object_id) if ret_code == 2: for ditem in self._download_list.get_items(): if ditem.stage == "Completed": self._status_list.remove_row( self._download_list.index(ditem.object_id)) self._download_list.remove(ditem.object_id) else: if self.opt_manager.options["confirm_deletion"]: self._create_popup( QMessageBox.Question, _("Are you sure you want to remove selected items?"), _("Delete"), QMessageBox.Yes | QMessageBox.No) #dlg = wx.MessageDialog(self, _("Are you sure you want to remove selected items?"), _("Delete"), wx.YES_NO | wx.ICON_QUESTION) #result = dlg.ShowModal() == wx.ID_YES result = dlg.ShowModal() == QMessageBox.Yes dlg.Destroy() else: result = True if result: while index >= 0: object_id = self._status_list.GetItemData(index) selected_download_item = self._download_list.get_item( object_id) if selected_download_item.stage == "Active": #self._create_popup(_("Item is active, cannot remove"), self.WARNING_LABEL, wx.OK | wx.ICON_EXCLAMATION) self._create_popup(QMessageBox.Information, _("Item is active, cannot remove"), self.WARNING_LABEL, QMessageBox.OK) else: #if selected_download_item.stage == "Completed": #dlg = wx.MessageDialog(self, "Do you want to remove the files associated with this item?", "Remove files", wx.YES_NO | wx.ICON_QUESTION) #result = dlg.ShowModal() == wx.ID_YES #dlg.Destroy() #if result: #for cur_file in selected_download_item.get_files(): #remove_file(cur_file) self._status_list.remove_row(index) self._download_list.remove(object_id) index -= 1 index = self._status_list.get_next_selected(index) #self._update_pause_button(None) @pyqtSlot(QModelIndex) def _on_clicked(self, mIdx): print("select: %s, %s" % (mIdx, mIdx.row())) if mIdx.row() >= 0: self.pb_del.setEnabled(True) else: self.pb_del.setEnabled(False) self.pb_up.setEnabled(False) self.pb_down.setEnabled(False) @pyqtSlot(QModelIndex) def _on_activated(self, mIdx): print("select: %s" % mIdx) @pyqtSlot(QItemSelection, QItemSelection) def _on_selectionChanged(self, selected, deselected): print("selected: %s" % selected) def _on_savepath(self): folder = str( QFileDialog.getExistingDirectory(self, "Select save Directory")) if folder: #self._savepath = folder self.le_savepath.setText(folder) def _on_start(self, event): if self.download_manager is None: if self.update_thread is not None and self.update_thread.is_alive( ): self._create_popup( QMessageBox.Information, _("Update in progress. Please wait for the update to complete" ), self.WARNING_LABEL, QMessageBox.OK) else: self._start_download() else: self.download_manager.stop_downloads() def _start_download(self): if self._status_list.is_empty(): self._create_popup(_("No items to download"), self.WARNING_LABEL, QMessageBox.OK) else: self._app_timer.start(100) self.download_manager = DownloadManager(self, self._download_list, self.opt_manager, self.log_manager) self.download_manager.sig_callafter.connect( self._download_manager_handler) self.download_manager.sig_worker_callafter.connect( self._download_worker_handler) self._status_bar_write(self.DOWNLOAD_STARTED) self._buttons["start"].setText(self.STOP_LABEL) self._buttons["start"].setToolTip(self.STOP_LABEL) self._buttons["start"].setIcon(self._bitmaps["stop"]) def _on_timer(self): total_percentage = 0.0 queued = paused = active = completed = error = 0 for item in self._download_list.get_items(): if item.stage == "Queued": queued += 1 if item.stage == "Paused": paused += 1 if item.stage == "Active": active += 1 total_percentage += float( item.progress_stats["percent"].split('%')[0]) if item.stage == "Completed": completed += 1 if item.stage == "Error": error += 1 # REFACTOR Store percentage as float in the DownloadItem? # REFACTOR DownloadList keep track for each item stage? items_count = active + completed + error + queued total_percentage += completed * 100.0 + error * 100.0 if items_count: total_percentage /= items_count msg = self.URL_REPORT_MSG.format(total_percentage, queued, paused, active, completed, error) if self.update_thread is None: # Don't overwrite the update messages self._status_bar_write(msg) def _status_bar_write(self, msg): """Display msg in the status bar. """ self.statusbar.showMessage(msg) def _download_manager_worker_handler(self, signal, data): print("dl_work: %s: %s" % (signal, data)) print("_download_list: %s" % self._download_list.get_items()) def _download_worker_handler(self, data): """downloadmanager.Worker thread handler. Handles messages from the Worker thread. Args: See downloadmanager.Worker _talk_to_gui() method. """ #signal, data = msg.data #signal = sig download_item = self._download_list.get_item(data["index"]) download_item.update_stats(data) row = self._download_list.index(data["index"]) self._status_list._update_from_item(row, download_item) def _download_manager_handler(self, data): """downloadmanager.DownloadManager thread handler. Handles messages from the DownloadManager thread. Args: See downloadmanager.DownloadManager _talk_to_gui() method. """ #data = msg.data if data == 'finished': self._print_stats() self._reset_widgets() self.download_manager = None self._app_timer.stop() self._after_download() elif data == 'closed': self._status_bar_write(self.CLOSED_MSG) self._reset_widgets() self.download_manager = None self._app_timer.stop() elif data == 'closing': self._status_bar_write(self.CLOSING_MSG) elif data == 'report_active': pass #NOTE Remove from here and downloadmanager #since now we have the wx.Timer to check progress def _on_update(self, event): """Event handler of the self._update_btn widget. This method is used when the update button is pressed to start the update process. Note: Currently there is not way to stop the update process. """ if self.opt_manager.options["disable_update"]: self._create_popup( _("Updates are disabled for your system. Please use the system's package manager to update youtube-dl." ), self.INFO_LABEL, QMessageBox.OK) else: self._update_youtubedl() def _update_youtubedl(self): """Update youtube-dl binary to the latest version. """ if self.download_manager is not None and self.download_manager.is_alive( ): self._create_popup(self.DOWNLOAD_ACTIVE, self.WARNING_LABEL, QMessageBox.OK) elif self.update_thread is not None and self.update_thread.is_alive(): self._create_popup(self.UPDATE_ACTIVE, self.INFO_LABEL, QMessageBox.OK) else: self.update_thread = UpdateThread( self.opt_manager.options['youtubedl_path']) self.update_thread.sig_callafter.connect(self._update_handler) def _update_handler(self, sig, msg): """updatemanager.UpdateThread thread handler. Handles messages from the UpdateThread thread. Args: See updatemanager.UpdateThread _talk_to_gui() method. """ #data = msg.data if sig == 'download': self._status_bar_write("%s: %s" % (self.UPDATING_MSG, msg)) elif sig == 'error': self._status_bar_write(self.UPDATE_ERR_MSG.format(msg)) elif sig == 'correct': self._status_bar_write(self.UPDATE_SUCC_MSG) else: self._reset_widgets() self.update_thread = None def _reset_widgets(self): """Resets GUI widgets after update or download process. """ self._buttons["start"].setText(_("Start")) self._buttons["start"].setToolTip(_("Start")) self._buttons["start"].setIcon(self._bitmaps["start"]) def _on_about(self, event): #msg = "Name: %s\n" % __appname__ msg = "Version: %s\n" % __version__ msg += "Web: %s\n" % __projecturl__ msg += " %s\n" % __descriptionfull__ QMessageBox.about(self, "about - %s" % __appname__, msg) def updateSavePath(self, text): #TODO: make sure path of "text" exist self._savepath = text def loadSettings(self): '''load setting from file''' ''' self.cfg.beginGroup("main") #save path, self._savepath = self.cfg.value("savepath", QStandardPaths.writableLocation(QStandardPaths.DownloadLocation)) if not self._savepath: self._savepath = QStandardPaths.writableLocation(QStandardPaths.DownloadLocation) self._size = self.cfg.value("size", self.size()) self._pos = self.cfg.value("pos", self.pos()) #TODO: download list, resume? self.cfg.endGroup() ''' def setSettings(self): '''update setting to GUI''' self.resize( QSize(self.opt_manager.options["size"][0], self.opt_manager.options["size"][1])) self.move( QPoint(self.opt_manager.options["pos"][0], self.opt_manager.options["pos"][0])) #self.le_savepath.setText(self.opt_manager.options["save_path"]) self.path_combobox.addItem(self.opt_manager.options["save_path"]) def saveSettings(self): '''save setting to file''' self.opt_manager.options["size"] = "(%s,%s)" % (self.width(), self.height()) #self.opt_manager.options["pos"] #print("self.size(): %s" % self.opt_manager.options["size"]) self.opt_manager.save_to_file() ''' self.cfg.beginGroup("main") #save path self.cfg.setValue("savepath", self._savepath) self.cfg.setValue("size", self.size()) self.cfg.setValue("pos", self.pos()) #TODO: download list self.cfg.endGroup() ''' def closeEvent(self, event): # self.saveSettings() if 0: close = self._create_popup(QMessageBox.Question, self.QUIT_MSG, self.QUESTION_LABEL, QMessageBox.Yes | QMessageBox.Cancel) if close == QMessageBox.Yes: event.accept() else: event.ignore() else: event.accept()
def main(argv): # --------------------- Handler --------------------- # try: opts, args = getopt.getopt(argv, "hd:g:t:o:", [ "help", "test", "date=", "grid=", "target=", "only=", "overwrite" ]) except getopt.GetoptError: print('Invalid arguments. Add "-h" or "--help" for help.') sys.exit(1) date, grid, only, target, overwrite = None, None, None, None, False for opt, arg in opts: if opt in ("-h", "--help"): print('Usage: ./main.py [OPTIONS...]\n\nHelp options:\n\ -h, --help Show help\n\ --test Test that all packages and system variables work properly\n\ -d, --date Set date for all imagery capture (format: yyyy-mm-dd / yyyymmdd)\n\ -g, --grid Set center grid for sentinel imagery capture (format: east-decimal,north-decimal)\n\ -t, --target Set path to target directory (format: path/to/target)\n\ -o, --only Only create specified layer (format: layername)\n\ --overwrite Overwrite layers in geoserver') sys.exit() elif opt == ("--test"): print( "Success! All packages and sytem variables seems to work properly" ) sys.exit() elif opt in ("-d", "--date"): if '-' in arg: date = datetime.strptime(arg, '%Y-%m-%d').date() else: date = datetime.strptime(arg, '%Y%m%d').date() elif opt in ("-g", "--grid"): grid = arg elif opt in ("-t", "--target"): target = arg elif opt in ("-o", "--only"): only = arg elif opt in ("--overwrite"): overwrite = True if not opts: print('WARNING: No arguments provided. Using default values') # ------------------- Handler end ------------------- # d = DownloadManager(date=date, grid=grid, target=target) # Making download instance functions = { 's2c': d.getS2, 'terramos': d.getTerra, 's1c': d.getS1, 's1mos': d.getS1Mos, 'seaice': d.getSeaIce, 'icedrift': d.getIceDrift } # ------------------ Getting/processing data ------------------ # outfiles = [] if only: outfiles.append(functions[only](d.OUTDIR + only + '.tif')) else: for k, v in functions.iteritems(): try: funcs.printLayerStatus(str(k)) outfiles.append(v(d.OUTDIR + k + '.tif')) except Exception as e: print("Could not generate " + k + " layer") print(traceback.print_exc()) finally: print("") print('Created files: ' + str(outfiles) + '\n') if all(file == False for file in outfiles): print("No images found") exit(1) # ---------------- Getting/processing data end ---------------- # # --------------------- Upload to Geoserver -------------------- # cat = Catalog("http://localhost:8080/geoserver/rest/", "admin", os.environ["GEOSERVER_SECRET_KEY"]) print(cat) # BUG: ws is sometime cite instead of d.date (fixed?) ws = cat.get_workspace(str(d.DATE)) if ws is None: ws = cat.create_workspace(str(d.DATE), str(d.DATE) + 'uri') for layerdata in outfiles: if layerdata: layername = layerdata.split('/')[-1].split('.')[0] print('Adding ' + layername + ' to geoserver...') cat.create_coveragestore(name=layername, data=layerdata, workspace=ws, overwrite=overwrite) print('Sensor data for ' + str(d.DATE) + ' has been successfully uploaded!')