diff --git a/minigalaxy/api.py b/minigalaxy/api.py index c3bdde7e..ef49332e 100644 --- a/minigalaxy/api.py +++ b/minigalaxy/api.py @@ -2,6 +2,7 @@ import logging import os import time +from typing import List from urllib.parse import urlencode import requests import xml.etree.ElementTree as ET @@ -144,7 +145,7 @@ def get_info(self, game: Game) -> dict: return response # This returns a unique download url and a link to the checksum of the download - def get_download_info(self, game: Game, operating_system="linux", dlc_installers="") -> dict: + def get_download_info(self, game: Game, operating_system="linux", dlc_installers="") -> GameDownloadInfo: if dlc_installers: installers = dlc_installers else: @@ -194,6 +195,35 @@ def get_download_file_info(self, url): except requests.exceptions.RequestException as e: raise XmlException("Couldn't read xml data. Received RequestException") from e + def get_download_urls(self, game:Game, operating_system="linux", dlc_installers="") -> List[str]: + urls = [] + if dlc_installers: + installers = dlc_installers + else: + response = self.__request("https://api.gog.com/products/{}?locale=en-US&expand=downloads".format(str(game.id))) + installers = response["downloads"]["installers"] + + possible_downloads = [] + for installer in installers: + if installer["os"] == operating_system: + possible_downloads.append(installer) + if not possible_downloads: + if operating_system == "linux": + return self.get_download_urls(game, "windows") + else: + raise NoDownloadLinkFound("Error: {} with id {} couldn't be installed".format(game.name, game.id)) + + for installer in possible_downloads: + if installer['language'] == self.config.lang: + for download_file in installer["files"]: + urls.append(download_file["downlink"]) + break + if installer['language'] == "en": + for download_file in installer["files"]: + urls.append(download_file["downlink"]) + + return urls + def get_user_info(self) -> str: username = self.config.username if not username: diff --git a/minigalaxy/entity/download_chunk.py b/minigalaxy/entity/download_chunk.py index d7177b58..fb074b44 100644 --- a/minigalaxy/entity/download_chunk.py +++ b/minigalaxy/entity/download_chunk.py @@ -8,3 +8,6 @@ class DownloadChunk: to_byte: int method: str checksum: str + + def get_size(self) -> int: + return self.to_byte - self.from_byte diff --git a/minigalaxy/ui/gametile.py b/minigalaxy/ui/gametile.py index 5a254505..785418b2 100644 --- a/minigalaxy/ui/gametile.py +++ b/minigalaxy/ui/gametile.py @@ -8,6 +8,7 @@ from minigalaxy.config import Config from minigalaxy.entity.game_download_info import GameDownloadInfo +from minigalaxy.entity.game_downloader import GameDownloader from minigalaxy.entity.xml_exception import XmlException from minigalaxy.game import Game from minigalaxy.translation import _ @@ -237,14 +238,16 @@ def get_download_info(self, platform="linux"): return result, download_info def __download_game(self) -> None: - finish_func = self.__install_game - cancel_to_state = self.state.DOWNLOADABLE - result, download_info = self.get_download_info() - if result: - result = self.__download(download_info, DownloadType.GAME, finish_func, - cancel_to_state) - if not result: - GLib.idle_add(self.update_to_state, cancel_to_state) + # finish_func = self.__install_game + # cancel_to_state = self.state.DOWNLOADABLE + # result, download_info = self.get_download_info() + # if result: + # result = self.__download(download_info, DownloadType.GAME, finish_func, + # cancel_to_state) + # if not result: + # GLib.idle_add(self.update_to_state, cancel_to_state) + self.gameDownloader = GameDownloader(api=self.api, config=self.config) + self.gameDownloader.download_game(game=self.game) def __download(self, game_download_info: GameDownloadInfo, download_type: DownloadType, finish_func: Callable, cancel_to_state: Callable): # noqa: C901 GLib.idle_add(self.update_to_state, self.state.QUEUED) @@ -343,18 +346,19 @@ def __download_update(self) -> None: GLib.idle_add(self.update_to_state, cancel_to_state) def __check_for_update_dlc(self): - if self.game.is_installed() and self.game.id and not self.offline: - game_info = self.api.get_info(self.game) - if self.game.get_info("check_for_updates") == "": - self.game.set_info("check_for_updates", True) - if self.game.get_info("check_for_updates"): - game_version = self.api.get_version(self.game, gameinfo=game_info) - update_available = self.game.is_update_available(game_version) - if update_available: - GLib.idle_add(self.update_to_state, self.state.UPDATABLE) - self.__check_for_dlc(game_info) - if self.offline: - GLib.idle_add(self.menu_button_dlc.hide) + # if self.game.is_installed() and self.game.id and not self.offline: + # game_info = self.api.get_info(self.game) + # if self.game.get_info("check_for_updates") == "": + # self.game.set_info("check_for_updates", True) + # if self.game.get_info("check_for_updates"): + # game_version = self.api.get_version(self.game, gameinfo=game_info) + # update_available = self.game.is_update_available(game_version) + # if update_available: + # GLib.idle_add(self.update_to_state, self.state.UPDATABLE) + # self.__check_for_dlc(game_info) + # if self.offline: + # GLib.idle_add(self.menu_button_dlc.hide) + pass def __update(self, save_location): install_success = self.__install(save_location, update=True) @@ -420,7 +424,7 @@ def update_gtk_box_for_dlc(self, dlc_id, icon, title, installer): dlc_box.show_all() self.get_async_image_dlc_icon(dlc_id, image, icon, title) download_info = self.api.get_download_info(self.game, dlc_installers=installer) - if self.game.is_update_available(version_from_api=download_info["version"], dlc_title=title): + if self.game.is_update_available(version_from_api=download_info.version, dlc_title=title): icon_name = "emblem-synchronizing" self.dlc_dict[title][0].set_sensitive(True) elif self.game.is_installed(dlc_title=title):