def get_user_info(self, finish_fn=None) -> str: username = Config.get("username") userid = Config.get("user_id") avatar = None if userid is not None: user_dir = os.path.join(CACHE_DIR, "user/{}".format(userid)) if os.path.exists(user_dir) == False: os.makedirs(user_dir) avatar = os.path.join(user_dir, "avatar_menu_user_av_small.jpg") if not (os.path.isfile(avatar) and os.path.exists(avatar)): avatar = None if (avatar is not None and finish_fn is not None): finish_fn() if not username or not avatar: url = "https://embed.gog.com/userData.json" response = self.__request(url) username = response["username"] userid = response["userId"] Config.set("user_id", userid) Config.set("username", username) user_dir = os.path.join(CACHE_DIR, "user/{}".format(userid)) if os.path.exists(user_dir) == False: os.makedirs(user_dir) avatar = os.path.join(user_dir, "avatar_menu_user_av_small.jpg") download = Download(response["avatar"] + "_menu_user_av_small.jpg", avatar) download.register_finish_function(finish_fn) DownloadManager.download_now(download) return username
def __install(self, game: Game = None): GLib.idle_add(self.__update_to_state, game.state.INSTALLING, game) game.install_dir = game.get_install_dir() try: if os.path.exists(game.keep_path): install_game(game, game.keep_path, main_window=self) else: install_game(game, game.download_path, main_window=self) except (FileNotFoundError, BadZipFile): GLib.idle_add(self.__update_to_state, game.state.DOWNLOADABLE, game) return GLib.idle_add(self.__update_to_state, game.state.INSTALLED, game) GLib.idle_add(self.__reload_state, game) # make user to add the game to the side bar # check if DLCs should also be installed if game.type == "game" and Config.get("install_dlcs"): # first ensure we know about game dlcs self.library.update_dlcs_for_game(game) if len(game.dlcs) == 0: return # now grab DLCs that can be installed downloads = [] for dlc in game.dlcs: try: download_info = self.api.get_download_info( dlc, game.platform, True, dlc.get_installers()) except Exception: # could not find a valid target, ignore it continue # set dlc information now, otherwise this will break later dlc.platform = game.platform dlc.language = game.language # add download # Start the download for all files for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format( dlc.download_path, key) else: download_path = dlc.download_path download = Download(url=self.api.get_real_download_link( file_info["downlink"]), title=dlc.name, associated_object=dlc, save_location=download_path, number=key + 1, file_size=download_info["total_size"], out_of_amount=len( download_info['files'])) download.register_finish_function(self.__dlc_finish_func, [game, dlc]) download.register_progress_function( self.set_progress, game) download.register_cancel_function(self.__cancel_download, game) downloads.append(download) DownloadManager.download(downloads)
def load_icon(self): if self.__set_image(): return True if not self.game.sidebar_icon_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}".format(self.game.sidebar_icon_url) icon = os.path.join(self.game.cache_dir, "{}_sbicon.png".format(self.game.id)) download = Download(image_url, icon) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def load_thumbnail(self): if self.__set_image(): return True if not self.game.image_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}_196.jpg".format(self.game.image_url) thumbnail = os.path.join(THUMBNAIL_DIR, "{}_196.jpg".format(self.game.id)) download = Download(image_url, thumbnail) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def __download_update(self, game: Game = None) -> None: Config.set("current_download", game.id) GLib.idle_add(self.__update_to_state, game.state.UPDATE_QUEUED, game) download_info = self.api.get_download_info(game) # Start the download for all files game.downloads = [] download_path = game.update_path finish_func = self.__update for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format(self.update_path, key) download = Download(url=self.api.get_real_download_link( file_info["downlink"]), save_location=download_path, finish_func=finish_func, finish_func_args=game, progress_func=self.set_progress, progress_func_args=game, cancel_func=self.__cancel_update, cancel_func_args=game, number=key + 1, out_of_amount=len(download_info['files'])) game.downloads.append(download) DownloadManager.download(game.downloads)
def load_icon(self): if self.__set_image(): return True if not self.achievement.image_url_unlocked or not self.achievement.achievement_id: return False # Download the thumbnail image_url = self.achievement.image_url_unlocked if self.achievement.date_unlocked is not None else self.achievement.image_url_locked icon = os.path.join( self.game.cache_dir, "achievement_{}_{}.jpg".format( self.achievement.achievement_id, "unlocked" if self.achievement.date_unlocked is not None else "locked")) download = Download(image_url, icon) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def test2_finish(self): mock_finish_function = MagicMock() mock_finish_function.side_effect = FileNotFoundError( Mock(status="Connection Error")) mock_cancel_function = MagicMock() download = Download("test_url", "test_save_location") download.register_finish_function(mock_finish_function) download.register_cancel_function(mock_cancel_function) download.finish() exp = 2 obs = len(mock_cancel_function.mock_calls) self.assertEqual(exp, obs)
def __install_file(self, widget, link: str, ftype: str = None, product_id: int = None, fsize: int = 0, treeiter: Gtk.TreeIter = None): if (ftype == _("Installer")): # just follow the default path self.parent.download_game(self.game) elif ftype == "DLC " + _("Installer"): dlc = None if product_id is not None: for i in self.game.dlcs: if i.id == product_id: dlc = i break if dlc is not None: dlc.platform = self.downloadstree.get_model().get_value( treeiter, 10) dlc.language = DOWNLOAD_LANGUAGES_TO_GOG_CODE[ self.downloadstree.get_model().get_value(treeiter, 2)] dlc.available_version = self.downloadstree.get_model( ).get_value(treeiter, 5) download = Download(url=self.api.get_real_download_link(link), save_location=dlc.download_path, title=dlc.name, associated_object=dlc, file_size=fsize) else: # fallback to game as source of generic information download = Download(url=self.api.get_real_download_link(link), save_location=self.game.download_path, title=self.game.name, associated_object=self.game, file_size=fsize) download.register_finish_function(self.__finish_download, [treeiter, dlc]) download.register_state_function(self.__state_func, treeiter) DownloadManager.download(download) print("Downloading: \"{}\"".format(link))
def __add_video(self, vidx, video_url, thumbnail_url): if self.__set_video(vidx, video_url): return True # Download the thumbnail img = os.path.join(self.game.cache_dir, "video_{}.jpg".format(vidx)) download = Download(thumbnail_url, img) download.register_finish_function(self.__set_video, (vidx, video_url)) DownloadManager.download_now(download) return True
def __add_screenshot(self, image_id, image_url): if self.__set_screenshot(image_id): return True # Download the thumbnail img = os.path.join(self.game.cache_dir, "screenshot_{}_ggvgt_2x.jpg".format(image_id)) download = Download(image_url, img) download.register_finish_function(self.__set_screenshot, image_id) DownloadManager.download_now(download) return True
def test_cancel(self): mock_cancel_function = MagicMock() download = Download("test_url", "test_save_location") download.register_cancel_function(mock_cancel_function) download.cancel() exp = 2 obs = len(mock_cancel_function.mock_calls) self.assertEqual(exp, obs)
def __load_background_image(self): if self.__set_background_image(): return True if not self.game.background_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}".format(self.game.background_url) img = os.path.join(self.game.cache_dir, "{}_background.jpg".format(self.game.id)) download = Download(image_url, img) download.register_finish_function(self.__set_background_image) DownloadManager.download_now(download) return True
def test1_set_progress(self): mock_progress_function = MagicMock() download = Download("test_url", "test_save_location") download.register_progress_function(mock_progress_function) download.set_progress(50) kall = mock_progress_function.mock_calls[-1] name, args, kwargs = kall exp = 50 obs = args[0] self.assertEqual(exp, obs)
def __download_file(self, widget, link: str, fname: str = None): dialog: Gtk.FileChooserDialog = Gtk.FileChooserDialog( _("Download"), self.parent, action=Gtk.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT)) file_name = link[link.rfind("/") + 1:] if fname is None else fname dialog.set_current_name(file_name) response = dialog.run() if response == Gtk.ResponseType.ACCEPT: download = Download(url=self.api.get_real_download_link(link), save_location=dialog.get_filename()) DownloadManager.download(download) print("Downloading: \"{}\" to \"{}\"".format( link, dialog.get_filename())) dialog.destroy()
def __init__(self, parent, download:Download, api): Gtk.Frame.__init__(self) self.parent = parent self.download = download self.api = api self.thumbnail_set = False self.image.set_sensitive(True) self.title_label.set_text(self.download.title) self.__game = None if download.get_progress() >= 0: self.details_label.set_text("{} / {} ({}%)".format(self.__sizeof_fmt(download.get_downloaded()),self.__sizeof_fmt(download.file_size),download.get_progress())) elif download.file_size > 0: self.details_label.set_text("{} / {}".format(self.__sizeof_fmt(download.get_downloaded()),self.__sizeof_fmt(download.file_size))) self.load_icon() # set button icon self.set_state(download.state()) # register functions to track download download.register_progress_function(self.set_progress) download.register_state_function(self.set_state)
def __download_file(self, game: Game, operating_system=None) -> None: Config.set("current_download", game.id) GLib.idle_add(self.__update_to_state, game.state.QUEUED, game) current_os = platform.system() if current_os == "Linux": current_os = "linux" elif current_os == "Windows": current_os = "windows" elif current_os == "Darwin": current_os = "mac" # pick current os if none was passed if operating_system is None: operating_system = current_os if game.platform is None: game.platform = operating_system download_info = self.api.get_download_info( game, operating_system=operating_system) # Start the download for all files game.downloads = [] download_path = game.download_path finish_func = self.__install for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format(self.download_path, key) download = Download(url=self.api.get_real_download_link( file_info["downlink"]), title=download_info["name"], associated_object=game, save_location=download_path, number=key + 1, file_size=download_info["total_size"], out_of_amount=len(download_info['files'])) download.register_finish_function(finish_func, game) download.register_progress_function(self.set_progress, game) download.register_cancel_function(self.__cancel_download, game) game.downloads.append(download) DownloadManager.download(game.downloads)
def __download_listener_func(self, download: Download = None): if download is None or download.priority() < 0: return # create a new download row row = DownloadRow(self.downloads_list, download, self.api) GLib.idle_add(self.downloads_list.prepend, row)