From 6e7eef165e16c34d3208f306edde12ad8be0faae Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 21 Jan 2020 13:26:49 -0700 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ README.md | 1 + app/__init__.py | 6 ++++++ app/routes.py | 41 +++++++++++++++++++++++++++++++++++++ app/static/js/controller.js | 15 ++++++++++++++ app/templates/error.html | 1 + app/templates/index.html | 5 +++++ app/templates/search.html | 3 +++ app/url.py | 4 ++++ requirements.txt | 7 +++++++ server.py | 1 + 11 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app/__init__.py create mode 100644 app/routes.py create mode 100644 app/static/js/controller.js create mode 100644 app/templates/error.html create mode 100644 app/templates/index.html create mode 100644 app/templates/search.html create mode 100644 app/url.py create mode 100644 requirements.txt create mode 100644 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0ee3b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv/ +__pycache__/ +*.pyc diff --git a/README.md b/README.md new file mode 100644 index 0000000..d1c013e --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# No AMP diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..6f344fd --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,6 @@ +from flask import Flask +import os + +app = Flask(__name__, static_folder=os.path.dirname(os.path.abspath(__file__)) + '/static') + +from app import routes diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 0000000..610bda9 --- /dev/null +++ b/app/routes.py @@ -0,0 +1,41 @@ +from app import app +from flask import request, redirect, Response, render_template +import os +import pycurl +from .url import url_parse +from io import BytesIO + + +@app.route('/', methods=['GET']) +def index(): + return render_template('index.html') + + +@app.route('/search', methods=['GET']) +def search(): + q = request.args.get('q') + if q is None or len(q) <= 0: + return render_template('error.html') + + b_obj = BytesIO() + crl = pycurl.Curl() + crl.setopt(crl.URL, 'https://www.google.com/search?q=' + url_parse(q)) + crl.setopt(crl.USERAGENT, 'Brozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 LizzieMcGuirefox/59.0') + crl.setopt(crl.WRITEDATA, b_obj) + crl.perform() + crl.close() + get_body = b_obj.getvalue() + return render_template('search.html', response=get_body.decode("utf-8", 'ignore')) + + +@app.route('/url', methods=['GET']) +def url(): + q = request.args.get('q') + if len(q) > 0 and 'http' in q: + return redirect(q) + else: + return render_template('error.html') + + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') diff --git a/app/static/js/controller.js b/app/static/js/controller.js new file mode 100644 index 0000000..1e06935 --- /dev/null +++ b/app/static/js/controller.js @@ -0,0 +1,15 @@ +document.addEventListener("DOMContentLoaded", function() { + const searchBar = document.getElementById("search-bar"); + const searchBtn = document.getElementById("search-submit"); + + searchBar.addEventListener("keyup", function(event) { + if (event.keyCode === 13) { + event.preventDefault(); + searchBtn.click(); + } + }); + + searchBtn.onclick = function() { + window.location.href = '/search?q=' + encodeURI(searchBar.value); + } +}); diff --git a/app/templates/error.html b/app/templates/error.html new file mode 100644 index 0000000..82b0b1a --- /dev/null +++ b/app/templates/error.html @@ -0,0 +1 @@ +error parsing url diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..1ade0b1 --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1,5 @@ + + + + + diff --git a/app/templates/search.html b/app/templates/search.html new file mode 100644 index 0000000..2974faa --- /dev/null +++ b/app/templates/search.html @@ -0,0 +1,3 @@ +

Search

+ +{{ response|safe }} diff --git a/app/url.py b/app/url.py new file mode 100644 index 0000000..e896dcf --- /dev/null +++ b/app/url.py @@ -0,0 +1,4 @@ + + +def url_parse(str): + return str.replace(' ', '+') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9cdedc3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +Click==7.0 +Flask==1.1.1 +itsdangerous==1.1.0 +Jinja2==2.10.3 +MarkupSafe==1.1.1 +pycurl==7.43.0.4 +Werkzeug==0.16.0 diff --git a/server.py b/server.py new file mode 100644 index 0000000..d099b92 --- /dev/null +++ b/server.py @@ -0,0 +1 @@ +from app import app