Complete refactoring of opensearch

Refactored opensearch.xml to only exist as a template that is
served by a flask route, which is then populated with the
necessary url root.
main
Ben Busby 2020-04-24 18:45:57 -06:00
parent 02bb5a3426
commit 1f6bfa092e
6 changed files with 19 additions and 43 deletions

View File

@ -77,10 +77,7 @@ To filter by a range of time, append ":past <time>" to the end of your search, w
## Extra Steps
### Set Shoogle as your primary search engine
1. From the main shoogle folder, run `python config/opensearch.py "<your app url>"`
2. Rebuild and release your updated app
- `heroku container:push web` and then `heroku container:release web`
3. Update browser settings
Update browser settings:
- Firefox (Desktop)
- Navigate to your app's url, and click the 3 dot menu in the address bar. At the bottom, there should be an option to "Add Search Engine". Once you've clicked this, open your Firefox Preferences menu, click "Search" in the left menu, and use the available dropdown to select "Shoogle" from the list.
- Firefox (Mobile)

View File

@ -52,6 +52,7 @@ class Filter:
logo['style'] = 'display:flex; justify-content:center; align-items:center; color:#685e79; font-size:18px;'
# Fix search bar length on mobile
if self.mobile:
search_bar = soup.find('header').find('form').find('div')
search_bar['style'] = 'width: 100%;'

View File

@ -2,7 +2,7 @@ from app import app
from app.filter import Filter
from app.request import Request, gen_query
from bs4 import BeautifulSoup
from flask import g, request, redirect, render_template
from flask import g, make_response, request, redirect, render_template
import json
import os
import urllib.parse as urlparse
@ -24,6 +24,18 @@ def index():
return render_template('index.html', bg=bg, ua=g.user_request.modified_user_agent)
@app.route('/opensearch.xml', methods=['GET'])
def opensearch():
url_root = request.url_root
if url_root.endswith('/'):
url_root = url_root[:-1]
template = render_template('opensearch.xml', shoogle_url=url_root)
response = make_response(template)
response.headers['Content-Type'] = 'application/xml'
return response
@app.route('/search', methods=['GET'])
def search():
q = request.args.get('q')

View File

@ -1,13 +0,0 @@
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>Shoogle</ShortName>
<Description>Shoogle: A lightweight, deployable Google search proxy for desktop/mobile that removes Javascript, AMP links, and ads</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="32" height="32" type="image/x-icon">/static/img/favicon/favicon-32x32.png</Image>
<Url type="text/html" template="SHOOGLE_URL/search">
<Param name="q" value="{searchTerms}"/>
</Url>
<Url type="application/x-suggestions+json" template="SHOOGLE_URL/search"/>
<moz:SearchForm>SHOOGLE_URL/search</moz:SearchForm>
</OpenSearchDescription>

View File

@ -17,7 +17,7 @@
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/static/img/favicon/ms-icon-144x144.png">
<script type="text/javascript" src="/static/js/controller.js"></script>
<link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="Shoogle Search">
<link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Shoogle Search">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/main.css">
<title>Shoogle Search</title>

View File

@ -1,21 +0,0 @@
import os
import sys
script_path = os.path.dirname(os.path.realpath(__file__))
template_path = script_path + '/../app/static/opensearch.template'
opensearch_path = script_path + '/../app/static/opensearch.xml'
replace_tag = 'SHOOGLE_URL'
if len(sys.argv) != 2:
print('You must provide the url as an argument for this script.')
print('Example: python opensearch.py "https://my-app-1776.herokuapps.com"')
sys.exit(0)
app_url = sys.argv[1].rstrip('/')
opensearch_template = open(template_path, 'r').read()
with open(opensearch_path, 'w') as opensearch_xml:
opensearch_xml.write(opensearch_template.replace(replace_tag, app_url))
opensearch_xml.close()
print('\nDone - you may now set Shoogle as your primary search engine')