Skip to content

Commit

Permalink
Attempt to make chunk downloading work better
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkwouter committed Jul 9, 2023
1 parent 5bdf18a commit 027a44e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
32 changes: 31 additions & 1 deletion minigalaxy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions minigalaxy/entity/download_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 25 additions & 21 deletions minigalaxy/ui/gametile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 027a44e

Please sign in to comment.