Added html parsing to remove returned scripts, added logo

main
Ben Busby 2020-01-23 06:19:17 +00:00
parent b11fc5fe67
commit 4636b0f695
6 changed files with 56 additions and 3 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@ venv/
__pycache__/ __pycache__/
*.pyc *.pyc
*.pem *.pem
*.xml
run.sh run.sh

View File

@ -1,7 +1,9 @@
from app import app from app import app
from bs4 import BeautifulSoup
from flask import request, redirect, Response, render_template from flask import request, redirect, Response, render_template
import os import os
import pycurl import pycurl
import re
from .url import url_parse from .url import url_parse
from io import BytesIO from io import BytesIO
@ -24,7 +26,12 @@ def search():
if 'tbm' in request.args: if 'tbm' in request.args:
tbm = '&tbm=' + request.args.get('tbm') tbm = '&tbm=' + request.args.get('tbm')
start = ''
if 'start' in request.args:
start = '&start=' + request.args.get('start')
user_agent = request.headers.get('User-Agent') user_agent = request.headers.get('User-Agent')
full_query = url_parse(q) + tbm + start
google_ua = DESKTOP_UA google_ua = DESKTOP_UA
if 'Android' in user_agent or 'iPhone' in user_agent: if 'Android' in user_agent or 'iPhone' in user_agent:
@ -32,17 +39,33 @@ def search():
b_obj = BytesIO() b_obj = BytesIO()
crl = pycurl.Curl() crl = pycurl.Curl()
crl.setopt(crl.URL, 'https://www.google.com/search?q=' + url_parse(q) + tbm) crl.setopt(crl.URL, 'https://www.google.com/search?gbv=1&q=' + full_query)
crl.setopt(crl.USERAGENT, google_ua) crl.setopt(crl.USERAGENT, google_ua)
crl.setopt(crl.WRITEDATA, b_obj) crl.setopt(crl.WRITEDATA, b_obj)
crl.perform() crl.perform()
crl.close() crl.close()
get_body = b_obj.getvalue() get_body = b_obj.getvalue().decode('utf-8', 'ignore')
return render_template('search.html', response=get_body.decode("utf-8", 'ignore')) get_body = get_body.replace('data-src', 'src').replace('.001', '1').replace('visibility:hidden', 'visibility:visible').replace('>G<', '>Bl<')
pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE)
get_body = pattern.sub('0000ff', get_body)
soup = BeautifulSoup(get_body, 'html.parser')
try:
for script in soup("script"):
script.decompose()
soup.find('div', id='sfooter').decompose()
except Exception:
pass
return render_template('search.html', response=soup)
@app.route('/url', methods=['GET']) @app.route('/url', methods=['GET'])
def url(): def url():
if 'url' in request.args:
return redirect(request.args.get('url'))
q = request.args.get('q') q = request.args.get('q')
if len(q) > 0 and 'http' in q: if len(q) > 0 and 'http' in q:
return redirect(q) return redirect(q)
@ -50,5 +73,10 @@ def url():
return render_template('error.html') return render_template('error.html')
@app.route('/imgres')
def imgres():
return redirect(request.args.get('imgurl'))
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0') app.run(debug=True, host='0.0.0.0')

View File

@ -5,6 +5,13 @@ body {
font-family: 'Open Sans', sans-serif; font-family: 'Open Sans', sans-serif;
} }
.logo {
width: 80%;
display: block;
margin: auto;
padding-bottom: 10px;
}
.search-container { .search-container {
width: 80%; width: 80%;
position: absolute; position: absolute;

BIN
app/static/img/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

View File

@ -0,0 +1,14 @@
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>[SNK]</ShortName>
<Description>[Search engine full name and summary]</Description>
<InputEncoding>[UTF-8]</InputEncoding>
<Image width="16" height="16" type="image/x-icon">[https://example.com/favicon.ico]</Image>
<Url type="text/html" template="[searchURL]">
<Param name="[key name]" value="{searchTerms}"/>
<!-- other Params if you need them… -->
<Param name="[other key name]" value="[parameter value]"/>
</Url>
<Url type="application/x-suggestions+json" template="[suggestionURL]"/>
<moz:SearchForm>[https://example.com/search]</moz:SearchForm>
</OpenSearchDescription>

View File

@ -1,8 +1,11 @@
<link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="Bloogle Search">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/static/js/controller.js"></script> <script src="/static/js/controller.js"></script>
<link rel="stylesheet" href="/static/css/main.css"> <link rel="stylesheet" href="/static/css/main.css">
<div class="search-container"> <div class="search-container">
<img class="logo" src="/static/img/logo.png">
<div class="search-fields"> <div class="search-fields">
<input type="text" id="search-bar"> <input type="text" id="search-bar">
<button type="submit" id="search-submit">Search</button> <button type="submit" id="search-submit">Search</button>