From afa370532b9566508ed865ac179f49210174aaf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Fri, 6 Mar 2020 14:57:28 -0300 Subject: [PATCH] [fix][web] some installed apps were not being displayed as installed in the search results --- CHANGELOG.md | 1 + bauh/gems/web/controller.py | 19 +++++++++++-------- bauh/gems/web/model.py | 11 +++++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f96a2b8a..763fc148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Web - not able to inject javascript fixes ( WhatsApp Web not working) [#74](https://github.com/vinifmor/bauh/issues/74) - not informing StartupWMClass on generated desktop entries ( prevents Gnome to link the Favorite shortcut with the app instance [#76](https://github.com/vinifmor/bauh/issues/76) ) + - some installed apps were not being displayed as installed in the search results ### Improvements - AUR diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index 1aaa4a03..c57dec43 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -14,7 +14,7 @@ from typing import List, Type, Set, Tuple import requests import yaml from colorama import Fore -from requests import exceptions +from requests import exceptions, Response from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.controller import SoftwareManager, SearchResult @@ -156,17 +156,18 @@ class WebApplicationManager(SoftwareManager): def serialize_to_disk(self, pkg: SoftwarePackage, icon_bytes: bytes, only_icon: bool): super(WebApplicationManager, self).serialize_to_disk(pkg=pkg, icon_bytes=None, only_icon=False) - def _map_url(self, url: str) -> Tuple["BeautifulSoup", requests.Response]: + def _request_url(self, url: str) -> Response: headers = {'Accept-language': self._get_lang_header(), 'User-Agent': UA_CHROME} try: - url_res = self.http_client.get(url, headers=headers, ignore_ssl=True, single_call=True, session=False) - - if url_res: - return BeautifulSoup(url_res.text, 'lxml', parse_only=SoupStrainer('head')), url_res + return self.http_client.get(url, headers=headers, ignore_ssl=True, single_call=True, session=False, allow_redirects=True) except exceptions.ConnectionError as e: self.logger.warning("Could not get {}: {}".format(url, e.__class__.__name__)) + def _map_url(self, url: str) -> Tuple["BeautifulSoup", requests.Response]: + url_res = self._request_url(url) + return BeautifulSoup(url_res.text, 'lxml', parse_only=SoupStrainer('head')), url_res + def search(self, words: str, disk_loader: DiskCacheLoader, limit: int = -1, is_url: bool = False) -> SearchResult: local_config = {} thread_config = Thread(target=self._fill_config_async, args=(local_config,)) @@ -200,7 +201,7 @@ class WebApplicationManager(SoftwareManager): desc = self._get_app_description(final_url, soup) icon_url = self._get_app_icon_url(final_url, soup) - app = WebApplication(url=final_url, name=name, description=desc, icon_url=icon_url) + app = WebApplication(url=final_url, source_url=url, name=name, description=desc, icon_url=icon_url) if self.env_settings.get('electron') and self.env_settings['electron'].get('version'): app.version = self.env_settings['electron']['version'] @@ -256,7 +257,9 @@ class WebApplicationManager(SoftwareManager): # checking if any of the installed matches is one of the matched suggestions for sug in matched_suggestions: - found = [i for i in installed_matches if i.url == sug.get('url')] + sug_url = sug['url'][0:-1] if sug['url'].endswith('/') else sug['url'] + + found = [i for i in installed_matches if sug_url in {i.url, i.get_source_url()}] if found: res.installed.extend(found) diff --git a/bauh/gems/web/model.py b/bauh/gems/web/model.py index 59bd1a19..a2f27657 100644 --- a/bauh/gems/web/model.py +++ b/bauh/gems/web/model.py @@ -13,11 +13,12 @@ class WebApplication(SoftwarePackage): def __init__(self, id: str = None, url: str = None, name: str = None, description: str = None, icon_url: str = None, installation_dir: str = None, desktop_entry: str = None, installed: bool = False, version: str = None, categories: List[str] = None, custom_icon: str = None, preset_options: List[str] = None, save_icon: bool = True, - options_set: List[str] = None, package_name: str = None): + options_set: List[str] = None, package_name: str = None, source_url: str = None): super(WebApplication, self).__init__(id=id if id else url, name=name, description=description, icon_url=icon_url, installed=installed, version=version, categories=categories) self.url = url + self.source_url = source_url if source_url else url self.installation_dir = installation_dir self.desktop_entry = desktop_entry self.set_custom_icon(custom_icon) @@ -26,6 +27,12 @@ class WebApplication(SoftwarePackage): self.options_set = options_set self.package_name = package_name + def get_source_url(self): + if self.source_url: + return self.source_url + + return self.url + def set_version(self, version: str): self.version = str(version) if version else None self.latest_version = version @@ -39,7 +46,7 @@ class WebApplication(SoftwarePackage): @staticmethod def _get_cached_attrs() -> tuple: return 'id', 'name', 'version', 'url', 'description', 'icon_url', 'installation_dir', \ - 'desktop_entry', 'categories', 'custom_icon', 'options_set', 'save_icon', 'package_name' + 'desktop_entry', 'categories', 'custom_icon', 'options_set', 'save_icon', 'package_name', 'source_url' def can_be_downgraded(self): return False