Minor refactor of filter class, updated tests, fixed html/css, added ua to config

main
Ben Busby 2020-04-16 10:01:02 -06:00
parent b5351c8502
commit 024552f2df
6 changed files with 51 additions and 37 deletions

View File

@ -6,18 +6,13 @@ from urllib.parse import parse_qs
class Filter: class Filter:
def __init__(self, mobile=False, config=None): def __init__(self, mobile=False, config=None):
self.mobile = False
self.dark_mode = False
self.nojs = False
self.near_city = None
if config is None: if config is None:
config = {} config = {}
near_city = config['near'] if 'near' in config else None self.near_city = config['near'] if 'near' in config else None
dark_mode = config['dark_mode'] if 'dark_mode' in config else False self.dark_mode = config['dark_mode'] if 'dark_mode' in config else False
nojs = config['nojs'] if 'nojs' in config else False self.nojs = config['nojs'] if 'nojs' in config else False
mobile = mobile self.mobile = mobile
def reskin(self, page): def reskin(self, page):
# Aesthetic only re-skinning # Aesthetic only re-skinning

View File

@ -11,12 +11,8 @@ import urllib.parse as urlparse
app.config['APP_ROOT'] = os.getenv('APP_ROOT', os.path.dirname(os.path.abspath(__file__))) app.config['APP_ROOT'] = os.getenv('APP_ROOT', os.path.dirname(os.path.abspath(__file__)))
app.config['STATIC_FOLDER'] = os.getenv('STATIC_FOLDER', os.path.join(app.config['APP_ROOT'], 'static')) app.config['STATIC_FOLDER'] = os.getenv('STATIC_FOLDER', os.path.join(app.config['APP_ROOT'], 'static'))
# Get Mozilla Firefox rhyme (important) and form a new user agent MOBILE_UA = '{}/5.0 (Android 0; Mobile; rv:54.0) Gecko/54.0 {}/59.0'
mozilla = rhyme.get_rhyme('Mo') + 'zilla' DESKTOP_UA = '{}/5.0 (X11; {} x86_64; rv:75.0) Gecko/20100101 {}/75.0'
firefox = rhyme.get_rhyme('Fire') + 'fox'
MOBILE_UA = mozilla + '/5.0 (Android 4.20; Mobile; rv:54.0) Gecko/54.0 ' + firefox + '/59.0'
DESKTOP_UA = mozilla + '/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Mobile ' + firefox + '/59.0'
# Base search url # Base search url
SEARCH_URL = 'https://www.google.com/search?gbv=1&q=' SEARCH_URL = 'https://www.google.com/search?gbv=1&q='
@ -25,7 +21,16 @@ user_config = json.load(open(app.config['STATIC_FOLDER'] + '/config.json'))
def get_ua(user_agent): def get_ua(user_agent):
return MOBILE_UA if ('Android' in user_agent or 'iPhone' in user_agent) else DESKTOP_UA is_mobile = 'Android' in user_agent or 'iPhone' in user_agent
mozilla = rhyme.get_rhyme('Mo') + rhyme.get_rhyme('zilla')
firefox = rhyme.get_rhyme('Fire') + rhyme.get_rhyme('fox')
linux = rhyme.get_rhyme('Lin') + 'ux'
if is_mobile:
return MOBILE_UA.format(mozilla, firefox)
else:
return DESKTOP_UA.format(mozilla, linux, firefox)
def send_request(curl_url, ua): def send_request(curl_url, ua):
@ -47,7 +52,7 @@ def send_request(curl_url, ua):
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
bg = '#000' if 'dark' in user_config and user_config['dark'] else '#fff' bg = '#000' if 'dark' in user_config and user_config['dark'] else '#fff'
return render_template('index.html', bg=bg) return render_template('index.html', bg=bg, ua=get_ua(request.headers.get('User-Agent')))
@app.route('/search', methods=['GET']) @app.route('/search', methods=['GET'])

View File

@ -63,7 +63,12 @@ body {
padding: 5px; padding: 5px;
} }
button::-moz-focus-inner {
border: 0;
}
.collapsible { .collapsible {
outline: 0;
background-color: rgba(0,0,0,0); background-color: rgba(0,0,0,0);
color: #685e79; color: #685e79;
cursor: pointer; cursor: pointer;
@ -107,3 +112,9 @@ body {
.open { .open {
padding-bottom: 20px; padding-bottom: 20px;
} }
.ua-span {
color: white;
-webkit-box-decoration-break: clone;
box-decoration-break: clone;
}

View File

@ -33,6 +33,10 @@
<button id="config-collapsible" class="collapsible">Configuration</button> <button id="config-collapsible" class="collapsible">Configuration</button>
<div class="content"> <div class="content">
<div class="config-fields"> <div class="config-fields">
<div class="config-div">
<!-- TODO: Add option to regenerate user agent? -->
<span class="ua-span">User Agent: {{ ua }}</span>
</div>
<div class="config-div"> <div class="config-div">
<label for="config-near">Near: </label> <label for="config-near">Near: </label>
<input type="text" name="config-near" id="config-near" placeholder="City Name"> <input type="text" name="config-near" id="config-near" placeholder="City Name">
@ -50,6 +54,7 @@
</div> </div>
</div> </div>
</div> </div>
</div>
</body> </body>
</html> </html>

View File

@ -1,9 +1,7 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from app.filter import Filter from app.filter import Filter
import json
from datetime import datetime from datetime import datetime
from dateutil.parser import * from dateutil.parser import *
from test.conftest import client
def get_search_results(data): def get_search_results(data):

View File

@ -1,10 +1,10 @@
import json import json
from test.conftest import client import random
demo_config = { demo_config = {
'near': 'Seattle', 'near': random.choice(['Seattle', 'New York', 'San Francisco']),
'dark_mode': 0, 'dark_mode': random.getrandbits(1),
'nojs': 0 'nojs': random.getrandbits(1)
} }