def can_connect(self) -> bool: url = "https://embed.gog.com" try: SESSION.get(url, timeout=5) except requests.exceptions.ConnectionError: return False return True
def can_connect() -> bool: urls = [ "https://embed.gog.com", "https://auth.gog.com", ] for url in urls: try: SESSION.get(url, timeout=5) except requests.exceptions.ConnectionError: return False return True
def download_operation(self, download, start_point, download_mode): # Download the file resume_header = {'Range': 'bytes={}-'.format(start_point)} download_request = SESSION.get(download.url, headers=resume_header, stream=True, timeout=30) downloaded_size = start_point file_size = int(download_request.headers.get('content-length')) result = True if downloaded_size < file_size: with open(download.save_location, download_mode) as save_file: for chunk in download_request.iter_content( chunk_size=DOWNLOAD_CHUNK_SIZE): # Pause if needed while self.__paused: time.sleep(0.1) save_file.write(chunk) downloaded_size += len(chunk) if self.__cancel: result = False break if file_size > 0: progress = int(downloaded_size / file_size * 100) download.set_progress(progress) save_file.close() else: download.set_progress(100) return result
def __request(self, url: str = None, params: dict = None) -> dict: # Refresh the token if needed if self.active_token_expiration_time < time.time(): print("Refreshing token") refresh_token = Config.get("refresh_token") Config.set("refresh_token", self.__refresh_token(refresh_token)) # Make the request headers = { 'Authorization': "Bearer {}".format(str(self.active_token)), } result = {} try: response = SESSION.get(url, headers=headers, params=params) if self.debug: print("Request: {}".format(url)) print("Return code: {}".format(response.status_code)) print("Response body: {}".format(response.text)) print("") if response.status_code < 300: result = response.json() except requests.exceptions.RequestException as e: print("Encountered exception while making HTTP request.") print("Request: {}".format(url)) print("Exception: {}".format(e)) print("") return result
def __request_gamesdb(game: Game): request_url = "https://gamesdb.gog.com/platforms/gog/external_releases/{}".format(game.id) try: response = SESSION.get(request_url) respones_dict = response.json() except (requests.exceptions.ConnectionError, ValueError): respones_dict = {} return respones_dict
def __get_refresh_token(self, params: dict) -> str: request_url = "https://auth.gog.com/token" response = SESSION.get(request_url, params=params) response_params = response.json() if "access_token" in response_params and "expires_in" in response_params and "refresh_token" in response_params: self.active_token = response_params["access_token"] expires_in = response_params["expires_in"] self.active_token_expiration_time = time.time() + int(expires_in) refresh_token = response_params["refresh_token"] else: refresh_token = "" return refresh_token
def __get_xml_checksum(self, url): result = {} response = SESSION.get(url) if response.status_code == http.HTTPStatus.OK and len( response.text) > 0: response_object = ET.fromstring(response.text) if response_object and response_object.attrib: result = response_object.attrib else: print( "Couldn't read xml data. Response with code {} received with the following content: {}" .format(response.status_code, response.text)) return result
def __is_same_download_as_before(self, download): file_stats = os.stat(download.save_location) # Don't resume for very small files if file_stats.st_size < MINIMUM_RESUME_SIZE: return False # Check if the first part of the file download_request = SESSION.get(download.url, stream=True) size_to_check = DOWNLOAD_CHUNK_SIZE*5 for chunk in download_request.iter_content(chunk_size=size_to_check): with open(download.save_location, "rb") as file: file_content = file.read(size_to_check) return file_content == chunk
def __download_file(self, download): # Make sure the directory exists save_directory = os.path.dirname(download.save_location) if not os.path.isdir(save_directory): os.makedirs(save_directory, mode=0o755) # Fail if the file already exists if os.path.isdir(download.save_location): raise IsADirectoryError("{} is a directory".format(download.save_location)) # Resume the previous download if possible start_point = 0 download_mode = 'wb' if os.path.isfile(download.save_location): if self.__is_same_download_as_before(download): print("Resuming download {}".format(download.save_location)) download_mode = 'ab' start_point = os.stat(download.save_location).st_size else: os.remove(download.save_location) # Download the file resume_header = {'Range': 'bytes={}-'.format(start_point)} download_request = SESSION.get(download.url, headers=resume_header, stream=True) downloaded_size = start_point file_size = int(download_request.headers.get('content-length')) if downloaded_size < file_size: with open(download.save_location, download_mode) as save_file: for chunk in download_request.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE): # Pause if needed while self.__paused: time.sleep(0.1) save_file.write(chunk) downloaded_size += len(chunk) if self.__cancel: self.__cancel = False save_file.close() download.cancel() self.__current_download = None return if file_size > 0: progress = int(downloaded_size / file_size * 100) download.set_progress(progress) save_file.close() if download.number == download.out_of_amount: finish_thread = threading.Thread(target=download.finish) finish_thread.start() if self.__queue.empty(): Config.unset("current_download")
def __refresh_token(self, refresh_token: str) -> str: request_url = "https://auth.gog.com/token" params = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'grant_type': 'refresh_token', 'refresh_token': refresh_token, } response = SESSION.get(request_url, params=params) response_params = response.json() self.active_token = response_params['access_token'] expires_in = response_params["expires_in"] self.active_token_expiration_time = time.time() + int(expires_in) return response_params['refresh_token']
def __get_xml_checksum(url): result = {} try: response = SESSION.get(url) if response.status_code == http.HTTPStatus.OK and len(response.text) > 0: response_object = ET.fromstring(response.text) if response_object and response_object.attrib: result = response_object.attrib else: print("Couldn't read xml data. Response with code {} received with the following content: {}".format( response.status_code, response.text )) except requests.exceptions.RequestException as e: print("Couldn't read xml data. Received RequestException : {}".format(e)) finally: return result
def __request(self, url: str = None, params: dict = None) -> dict: # Refresh the token if needed if self.active_token_expiration_time < time.time(): print("Refreshing token") refresh_token = Config.get("refresh_token") Config.set("refresh_token", self.__refresh_token(refresh_token)) # Make the request headers = { 'Authorization': "Bearer {}".format(str(self.active_token)), } response = SESSION.get(url, headers=headers, params=params) if self.debug: print("Request: {}".format(url)) print("Return code: {}".format(response.status_code)) print("Response body: {}".format(response.text)) print("") return response.json()
def get_file_size(self, url): xml_link = self.__request(url)['checksum'] xml_string = SESSION.get(xml_link).text root = ET.fromstring(xml_string) return root.attrib["total_size"]
def get_download_file_md5(self, url): xml_link = self.__request(url)['checksum'] xml_string = SESSION.get(xml_link).text root = ET.fromstring(xml_string) return root.attrib['md5']