Added run script, updated to use config json file for general location, general restyling

main
Ben Busby 2020-04-04 19:30:53 -06:00
parent b6901d4604
commit 9fbaa1d6cf
6 changed files with 61 additions and 30 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
venv/ venv/
.idea/
__pycache__/ __pycache__/
*.pyc *.pyc
*.pem *.pem
*.xml *.xml
run.sh config.json

View File

@ -1,40 +1,43 @@
from app import app from app import app
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from flask import request, redirect, Response, render_template from flask import request, redirect, Response, render_template
import json
import os import os
import pycurl import pycurl
import rhyme
import re import re
from .url import url_parse
import urllib.parse as urlparse import urllib.parse as urlparse
from urllib.parse import parse_qs from urllib.parse import parse_qs
from io import BytesIO from io import BytesIO
MOBILE_UA = os.environ.get('MOZ') + '/5.0 (Android 4.20; Mobile; rv:54.0) Gecko/54.0 ' + os.environ.get('FF') + '/54.0' # Get Mozilla Firefox rhyme (important) and form a new user agent
DESKTOP_UA = os.environ.get('MOZ') + '/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Mobile ' + os.environ.get('FF') + '/59.0' mozilla = rhyme.get_rhyme('Mo') + 'zilla'
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
SEARCH_URL = 'https://www.google.com/search?gbv=1&q=' SEARCH_URL = 'https://www.google.com/search?gbv=1&q='
# Optional nojs tag - opens links in a contained window with all js removed
# (can be useful for achieving nojs on mobile)
nojs = int(os.environ.get('NOJS')) nojs = int(os.environ.get('NOJS'))
config = json.load(open('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 return MOBILE_UA if ('Android' in user_agent or 'iPhone' in user_agent) else DESKTOP_UA
def send_request(url, ua): def send_request(curl_url, ua):
request_header = [] request_header = []
# Update as an optional param
# Todo: this doesn't seem to work
ip = '64.22.92.48'
request_header.append('CLIENT-IP: ' + ip)
request_header.append('X-FORWARDED-FOR: ' + ip)
b_obj = BytesIO() b_obj = BytesIO()
crl = pycurl.Curl() crl = pycurl.Curl()
crl.setopt(crl.URL, url) crl.setopt(crl.URL, curl_url)
crl.setopt(crl.USERAGENT, ua) crl.setopt(crl.USERAGENT, ua)
crl.setopt(crl.HTTPHEADER, request_header)
crl.setopt(crl.WRITEDATA, b_obj) crl.setopt(crl.WRITEDATA, b_obj)
crl.perform() crl.perform()
crl.close() crl.close()
@ -52,34 +55,44 @@ def search():
q = request.args.get('q') q = request.args.get('q')
if q is None or len(q) <= 0: if q is None or len(q) <= 0:
return render_template('error.html') return render_template('error.html')
q = urlparse.quote(q)
# Pass along type of results (news, images, books, etc)
tbm = '' tbm = ''
if 'tbm' in request.args: if 'tbm' in request.args:
tbm = '&tbm=' + request.args.get('tbm') tbm = '&tbm=' + request.args.get('tbm')
# Get results page start value (10 per page, ie page 2 start val = 20)
start = '' start = ''
if 'start' in request.args: if 'start' in request.args:
start = '&start=' + request.args.get('start') start = '&start=' + request.args.get('start')
# Change to a config setting # Grab city from config, if available
near = '&near=boulder' near = ''
if 'near' in request.args: if 'near' in config:
near = '&near=' + request.args.get('near') near = '&near=' + config['near']
user_agent = request.headers.get('User-Agent') user_agent = request.headers.get('User-Agent')
full_query = url_parse(q) + tbm + start + near full_query = q + tbm + start + near
# Aesthetic only re-skinning
get_body = send_request(SEARCH_URL + full_query, get_ua(user_agent)) get_body = send_request(SEARCH_URL + full_query, get_ua(user_agent))
get_body = get_body.replace('>G<', '>Sh<') get_body = get_body.replace('>G<', '>Sh<')
pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE) pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE)
get_body = pattern.sub('0000ff', get_body) get_body = pattern.sub('685e79', get_body)
soup = BeautifulSoup(get_body, 'html.parser') soup = BeautifulSoup(get_body, 'html.parser')
# Remove all ads (TODO: Ad specific div class may change over time, look into a more generic method)
ad_divs = soup.find('div', {'id': 'main'}).findAll('div', {'class': 'ZINbbc'}, recursive=False) ad_divs = soup.find('div', {'id': 'main'}).findAll('div', {'class': 'ZINbbc'}, recursive=False)
for div in ad_divs: for div in ad_divs:
div.decompose() div.decompose()
# Remove unnecessary button(s)
for button in soup.find_all('button'):
button.decompose()
# Replace hrefs with only the intended destination (no "utm" type tags)
for a in soup.find_all('a', href=True): for a in soup.find_all('a', href=True):
href = a['href'] href = a['href']
if 'url?q=' in href: if 'url?q=' in href:
@ -87,8 +100,10 @@ def search():
href = parse_qs(href.query)['q'][0] href = parse_qs(href.query)['q'][0]
if nojs: if nojs:
a['href'] = '/window?location=' + href a['href'] = '/window?location=' + href
#else: # else: # Automatically go to reader mode in ff? Not sure if possible
# a['href'] = 'about:reader?url=' + href # a['href'] = 'about:reader?url=' + href
# Ensure no extra scripts passed through
try: try:
for script in soup("script"): for script in soup("script"):
script.decompose() script.decompose()

View File

@ -29,26 +29,26 @@ body {
#search-bar { #search-bar {
width: 100%; width: 100%;
border: 3px solid #0000FF; border: 3px solid #685e79;
padding: 5px; padding: 5px;
height: 40px; height: 40px;
border-radius: 10px; border-radius: 10px;
outline: none; outline: none;
font-size: 24px; font-size: 24px;
color: #0000FF; color: #685e79;
border-radius: 10px 10px 0 0; border-radius: 10px 10px 0 0;
max-width: 600px; max-width: 600px;
} }
#search-bar:focus{ #search-bar:focus{
color: #0000FF; color: #685e79;
} }
#search-submit { #search-submit {
width: 100%; width: 100%;
height: 40px; height: 40px;
border: 1px solid #0000FF; border: 1px solid #685e79;
background: #0000FF; background: #685e79;
text-align: center; text-align: center;
color: #fff; color: #fff;
border-radius: 10px; border-radius: 10px;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -1,4 +0,0 @@
def url_parse(str):
return str.replace(' ', '+')

19
run.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Set NOJS mode to false if unavailable
if [[ -z "$NOJS" ]]; then
export NOJS=0
fi
# Create config json if it doesn't exist
if [[ -f $SCRIPT_DIR/config.json ]]; then
echo "{}" > $SCRIPT_DIR/config.json
fi
pkill flask
# TODO: Set up the following for running over https
#--cert=./app/cert.pem --key=./app/key.pem
$SCRIPT_DIR/venv/bin/flask run