From dd5321ae7ced610a14547991af8f215955acd67b Mon Sep 17 00:00:00 2001 From: jacr13 Date: Wed, 17 Nov 2021 13:08:51 +0100 Subject: [PATCH] add function that updates default content --- app/utils/results.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/app/utils/results.py b/app/utils/results.py index 416d0ea..77bdc02 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -1,4 +1,5 @@ from bs4 import BeautifulSoup, NavigableString +import copy import html import os import urllib.parse as urlparse @@ -222,3 +223,41 @@ def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: # Inserting the element ref_element.insert_before(ip_tag) return html_soup + + +def get_tabs_content(tabs: dict, + full_query: str, + search_type: str, + translation: dict) -> dict: + """Takes the default tabs content and updates it according to the query. + + Args: + tabs: The default content for the tabs + full_query: The original search query + search_type: The current search_type + translation: The translation to get the names of the tabs + + Returns: + dict: contains the name, the href and if the tab is selected or not + """ + tabs = copy.deepcopy(tabs) + for tab_id, tab_content in tabs.items(): + # update name to desired language + if tab_id in translation: + tab_content['name'] = translation[tab_id] + + # update href with query + query = full_query + if '&tbm=' in full_query: + query = full_query.replace(f"&tbm={search_type}", '') + + if tab_content['tbm'] is not None: + query = f"{query}&tbm={tab_content['tbm']}" + + tab_content['href'] = tab_content['href'].format(query=query) + + # update if selected tab (default all tab is selected) + if tab_content['tbm'] == search_type: + tabs['all']['selected'] = False + tab_content['selected'] = True + return tabs