Updated Currency Conversion Feature

This commit is contained in:
Vansh Comar 2021-11-18 12:26:36 +05:30
parent e06ff85579
commit d9b6b7c7b8
7 changed files with 140 additions and 3 deletions

View File

@ -15,7 +15,8 @@ from app.models.endpoint import Endpoint
from app.request import Request, TorError
from app.utils.bangs import resolve_bang
from app.utils.misc import read_config_bool, get_client_ip
from app.utils.results import add_ip_card
from app.utils.results import add_ip_card, check_currency
from app.utils.results import add_currency_b
from app.utils.results import bold_search_terms
from app.utils.search import *
from app.utils.session import generate_user_key, valid_user_session
@ -322,6 +323,12 @@ def search():
html_soup = bsoup(str(response), 'html.parser')
response = add_ip_card(html_soup, get_client_ip(request))
# Feature to display currency_card
conversion = check_currency(str(response))
if conversion:
html_soup = bsoup(str(response), 'html.parser')
response = add_currency_b(html_soup, conversion)
return render_template(
'display.html',
newest_version=newest_version,

View File

@ -187,6 +187,10 @@ path {
color: var(--whoogle-dark-text) !important;
}
.ip-text-div, .update_available {
.ip-text-div, .update_available, .cb_label, .cb {
color: var(--whoogle-dark-secondary-text) !important;
}
.cb:focus {
color: var(--whoogle-dark-contrast-text) !important;
}

View File

@ -12,3 +12,30 @@
height: 40px;
width: 50px;
}
.ZINbbc.xpd.O9g5cc.uUPGi input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.cb {
width: 40%;
overflow: hidden;
text-align: left;
line-height: 28px;
background: transparent;
border-radius: 6px;
border: 1px solid #5f6368;
font-size: 14px !important;
height: 36px;
padding: 0 0 0 12px;
margin: 10px 10px 10px 0;
}
.conversion_box {
margin-top: 15px;
}
.ZINbbc.xpd.O9g5cc.uUPGi input:focus-visible {
outline: 0;
}

View File

@ -175,6 +175,10 @@ path {
border-bottom: 0px;
}
.ip-text-div, .update_available {
.ip-text-div, .update_available, .cb_label, .cb {
color: var(--whoogle-secondary-text) !important;
}
.cb:focus {
color: var(--whoogle-text) !important;
}

11
app/static/js/currency.js Normal file
View File

@ -0,0 +1,11 @@
function Convert(n1, n2, conversion_factor) {
// id's for currency input boxes
id1 = "cb" + n1;
id2 = "cb" + n2;
// getting the value of the input box that just got filled
var input_box = document.getElementById(id1).value
// updating the other input box after conversion
document.getElementById(id2).value = ((input_box * conversion_factor).toFixed(2));
}

View File

@ -37,4 +37,5 @@
<script src="{{ cb_url('autocomplete.js') }}"></script>
<script src="{{ cb_url('utils.js') }}"></script>
<script src="{{ cb_url('keyboard.js') }}"></script>
<script src="{{ cb_url('currency.js') }}"></script>
</html>

View File

@ -223,3 +223,86 @@ def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup:
# Inserting the element
ref_element.insert_before(ip_tag)
return html_soup
def check_currency(response: str):
"""Check whether the results have currency conversion
Args:
response: Search query Result
Returns:
list
"""
soup = BeautifulSoup(response, 'html.parser')
a = soup.find(class_='ZINbbc xpd O9g5cc uUPGi')
tag = a.find('a')
if tag and tag['href'] == 'https://g.co/gfd':
currency1 = soup.find(class_="xUrNXd UMOHqf")
currency2 = soup.select_one('.kCrYT'
' .BNeawe.iBp4i.AP7Wnd .BNeawe.iBp4i.AP7Wnd')
currency1 = currency1.text.rstrip('=').split(' ', 1)
currency2 = currency2.text.split(' ', 1)
return [float(currency1[0]), currency1[1], float(currency2[0]), currency2[1]]
return None
def add_currency_b(soup: BeautifulSoup, conversion_details: list) -> BeautifulSoup:
"""Adds the conversion input boxes to the search result
Args:
soup: The parsed search result containing currency conversion
conversion_details: floating point number to be used for conversion
Returns:
BeautifulSoup
"""
# Element before which the code will be changed(This is the 'disclaimer' link)
element1 = soup.select_one('[class="nXE3Ob"]')
# Creating the conversion factor
conversion_factor = conversion_details[0]/conversion_details[2]
# Creating a new div for the input boxes
conversion_box = soup.new_tag('div')
conversion_box['class'] = 'conversion_box'
# Currency to be converted from
input_box1 = soup.new_tag('input')
input_box1['id'] = 'cb1'
input_box1['type'] = 'number'
input_box1['class'] = 'cb Hg3mWc'
input_box1['value'] = conversion_details[0]
input_box1['oninput'] = f'Convert(1, 2, {1/conversion_factor})'
label_box1 = soup.new_tag('label')
label_box1['for'] = 'cb1'
label_box1['class'] = 'cb_label'
label_box1.append(conversion_details[1])
br = soup.new_tag('br')
# Currency to be converted to
input_box2 = soup.new_tag('input')
input_box2['id'] = 'cb2'
input_box2['type'] = 'number'
input_box2['class'] = 'cb Hg3mWc'
input_box2['value'] = conversion_details[2]
input_box2['oninput'] = f'Convert(2, 1, {conversion_factor})'
label_box2 = soup.new_tag('label')
label_box2['for'] = 'cb2'
label_box2['class'] = 'cb_label'
label_box2.append(conversion_details[3])
conversion_box.append(input_box1)
conversion_box.append(label_box1)
conversion_box.append(br)
conversion_box.append(input_box2)
conversion_box.append(label_box2)
element1.insert_before(conversion_box)
return soup