From 61321556143db7e67ad4d30437407dfadeb8984d Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Wed, 9 Oct 2019 15:01:46 -0300 Subject: [PATCH] retrieving URL file size correctly | improving downloaded file size format --- CHANGELOG.md | 4 +++- README.md | 4 +++- bauh/api/http.py | 16 +++++++++++++--- bauh/gems/appimage/controller.py | 9 ++++++++- bauh/view/core/downloader.py | 5 +---- bauh/view/qt/info.py | 2 +- bauh/view/resources/locale/en | 3 ++- bauh/view/resources/locale/es | 3 ++- bauh/view/resources/locale/pt | 3 ++- 9 files changed, 35 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8803a67..66a320c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - retrieving installed applications information for Ubuntu based distros ### AppImage support -- Search, install, uninstall, downgrade, launch and retrieve the applications history available in [AppImageHub](https://appimage.github.io) +- Search, install, uninstall, downgrade, launch and retrieve the applications history +- Supported sources: [AppImageHub](https://appimage.github.io). +- Applications with no releases published to GitHub are not available - Adds desktop entries ( menu shortcuts ) for the installed applications ( **~/.local/share/applications **) diff --git a/README.md b/README.md index 76a36ebb..c483862e 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,9 @@ will be pre-downloaded faster ( it does **NOT** modify your **pacman** settings - Automatically makes simple package compilation improvements ### AppImage support ( appimage gem ) -- The user is able to search, install, uninstall, downgrade, launch and retrieve the applications history available in [AppImageHub](https://appimage.github.io) +- The user is able to search, install, uninstall, downgrade, launch and retrieve the applications history +- Supported sources: [AppImageHub](https://appimage.github.io). +- Applications with no releases published to GitHub are not available - Adds desktop entries ( menu shortcuts ) for the installed applications ( **~/.local/share/applications **) a) if **MAKEFLAGS** is not set in **/etc/makepkg.conf** and **~/.makepkg.conf** does not exist, diff --git a/bauh/api/http.py b/bauh/api/http.py index 22b15e99..d5b94a37 100644 --- a/bauh/api/http.py +++ b/bauh/api/http.py @@ -4,6 +4,8 @@ import traceback import requests +SIZE_MULTIPLIERS = ((0.001, 'Kb'), (0.000001, 'Mb'), (0.000000001, 'Gb'), (0.000000000001, 'Tb')) + class HttpClient: @@ -43,12 +45,20 @@ class HttpClient: res = self.get(url) return res.json() if res else None - def get_content_length(self, url: str) -> int: + def get_content_length(self, url: str) -> str: """ :param url: :return: """ - res = self.session.head(url) + res = self.session.get(url, allow_redirects=True, stream=True) if res.status_code == 200: - return res.headers['content-length'] + size = int(res.headers.get('Content-Length')) + + if size is not None: + for m in SIZE_MULTIPLIERS: + size_str = str(size * m[0]) + + if len(size_str.split('.')[0]) < 4: + return '{0:.2f}'.format(float(size_str)) + ' ' + m[1] + return str(size) diff --git a/bauh/gems/appimage/controller.py b/bauh/gems/appimage/controller.py index e42ff7fa..fc63238b 100644 --- a/bauh/gems/appimage/controller.py +++ b/bauh/gems/appimage/controller.py @@ -199,7 +199,14 @@ class AppImageManager(SoftwareManager): pass def get_info(self, pkg: AppImage) -> dict: - return pkg.get_data_to_cache() + data = pkg.get_data_to_cache() + + if data.get('url_download'): + size = self.http_client.get_content_length(data['url_download']) + if size: + data['size'] = size + + return data def get_history(self, pkg: AppImage) -> PackageHistory: history = [] diff --git a/bauh/view/core/downloader.py b/bauh/view/core/downloader.py index a95471f0..8ebcac02 100644 --- a/bauh/view/core/downloader.py +++ b/bauh/view/core/downloader.py @@ -84,8 +84,7 @@ class AdaptableFileDownloader(FileDownloader): downloader = 'wget' file_size = self.http_client.get_content_length(file_url) - file_size = int(file_size) / (1024 ** 2) if file_size else None - msg = bold('[{}] ').format(downloader) + self.i18n['downloading'] + ' ' + bold(file_url.split('/')[-1]) + (' ( {0:.2f} Mb )'.format(file_size) if file_size else '') + msg = bold('[{}] ').format(downloader) + self.i18n['downloading'] + ' ' + bold(file_url.split('/')[-1]) + (' ' + file_size if file_size else '') watcher.change_substatus(msg) success = handler.handle(process) except: @@ -101,8 +100,6 @@ class AdaptableFileDownloader(FileDownloader): return success - - def is_multithreaded(self) -> bool: return self.multithread_enabled and self.is_aria2c_available() diff --git a/bauh/view/qt/info.py b/bauh/view/qt/info.py index dea12475..e2042e94 100644 --- a/bauh/view/qt/info.py +++ b/bauh/view/qt/info.py @@ -65,7 +65,7 @@ class InfoDialog(QDialog): text.setStyleSheet("width: 400px") text.setReadOnly(True) - label = QLabel("{}: ".format(i18n.get(i18n_key, i18n.get(attr.lower(), attr))).capitalize()) + label = QLabel(i18n.get(i18n_key, i18n.get(attr.lower(), attr)).capitalize()) label.setStyleSheet("font-weight: bold") self.gbox_info_layout.addWidget(label, idx, 0) diff --git a/bauh/view/resources/locale/en b/bauh/view/resources/locale/en index fe2cdf8e..21a6ac02 100644 --- a/bauh/view/resources/locale/en +++ b/bauh/view/resources/locale/en @@ -118,4 +118,5 @@ manage_window.bt_settings.tooltip=Click here to open extra actions downloading=Downloading console.install_logs.path=Installation logs can be found at {} author=author -source=source \ No newline at end of file +source=source +size=size \ No newline at end of file diff --git a/bauh/view/resources/locale/es b/bauh/view/resources/locale/es index 16382602..23f9ec03 100644 --- a/bauh/view/resources/locale/es +++ b/bauh/view/resources/locale/es @@ -120,4 +120,5 @@ manage_window.bt_settings.tooltip=Haga clic aquí para abrir acciones adicionale downloading=Descargando console.install_logs.path=Los registros de instalación se pueden encontrar en {} author=autor -source=origen \ No newline at end of file +source=origen +size=tamaño \ No newline at end of file diff --git a/bauh/view/resources/locale/pt b/bauh/view/resources/locale/pt index c2b33f29..92cab931 100644 --- a/bauh/view/resources/locale/pt +++ b/bauh/view/resources/locale/pt @@ -120,4 +120,5 @@ manage_window.bt_settings.tooltip=Clique aqui para abrir ações adicionais downloading=Baixando console.install_logs.path=Os registros de instalação podem ser encontrados em {} author=autor -source=fonte \ No newline at end of file +source=fonte +size=tamanho \ No newline at end of file