Significantly reduce redundancy
Signed-off-by: Skylar "The Cobra" Widulski <cobra@vern.cc>
This commit is contained in:
parent
ff32a1a35f
commit
3f99b3b6c2
42
main.py
42
main.py
|
@ -6,11 +6,6 @@ import html
|
||||||
import re
|
import re
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
ERRROR_MESSAGE = 'Uh oh, could not connect to urban dictionary api'
|
|
||||||
DEFINE = "https://urbandictionary.com/define.php?term="
|
|
||||||
AUTHOR = "https://urbandictionary.com/author.php?author="
|
|
||||||
RANDOM = "https://urbandictionary.com/random.php"
|
|
||||||
HOME = "https://urbandictionary.com/"
|
|
||||||
|
|
||||||
def scrape(url, arg=None):
|
def scrape(url, arg=None):
|
||||||
if arg == None:
|
if arg == None:
|
||||||
|
@ -21,6 +16,7 @@ def scrape(url, arg=None):
|
||||||
if data.status_code == 200:
|
if data.status_code == 200:
|
||||||
our_path = re.sub(r".*://.*/", "/", request.url)
|
our_path = re.sub(r".*://.*/", "/", request.url)
|
||||||
path = re.sub(r".*://.*/", "/", data.url)
|
path = re.sub(r".*://.*/", "/", data.url)
|
||||||
|
print(our_path, path)
|
||||||
if our_path != path:
|
if our_path != path:
|
||||||
return f"REDIRECT {path}"
|
return f"REDIRECT {path}"
|
||||||
ret = []
|
ret = []
|
||||||
|
@ -39,47 +35,21 @@ def scrape(url, arg=None):
|
||||||
pages = ""
|
pages = ""
|
||||||
return (ret, pages)
|
return (ret, pages)
|
||||||
else:
|
else:
|
||||||
return f"Couldn't get data from the API\n{data.status_code}"
|
return f"Couldn't get data from Urban Dictionary\n{data.status_code}"
|
||||||
|
|
||||||
def render(data):
|
def render(data):
|
||||||
return render_template('index.html', data=data)
|
return render_template('index.html', data=data)
|
||||||
|
|
||||||
app = Flask(__name__, template_folder="templates", static_folder="static")
|
app = Flask(__name__, template_folder="templates", static_folder="static")
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/', defaults={'path': ''})
|
||||||
def home():
|
@app.route('/<path:path>')
|
||||||
scraped = scrape(HOME + {True: f"?page={request.args.get('page')}", False: ""} [request.args.get('page') != None])
|
def catch_all(path):
|
||||||
|
scraped = scrape(f"https://urbandictionary.com/{re.sub(r'.*://.*/', '/', request.url)}")
|
||||||
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
||||||
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
||||||
return render(scraped)
|
|
||||||
|
|
||||||
@app.route('/define.php')
|
|
||||||
def define():
|
|
||||||
if request.args.get('term') != None:
|
|
||||||
scraped = scrape(DEFINE + request.args.get('term') + {True: f"&page={request.args.get('page')}", False: ""} [request.args.get('page') != None])
|
|
||||||
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
|
||||||
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
|
||||||
else:
|
|
||||||
return redirect("/", 302)
|
|
||||||
return render(scraped)
|
|
||||||
|
|
||||||
@app.route('/author.php')
|
|
||||||
def author():
|
|
||||||
if request.args.get('author') != None:
|
|
||||||
scraped = scrape(AUTHOR + request.args.get('author') + {True: f"&page={request.args.get('page')}", False: ""} [request.args.get('page') != None])
|
|
||||||
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
|
||||||
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
|
||||||
else:
|
|
||||||
return redirect("/", 302)
|
|
||||||
scraped = (scraped[0], str(scraped[1]).replace("»", "»").replace("âº", "›").replace("«", "«").replace("â¹", "‹"))
|
scraped = (scraped[0], str(scraped[1]).replace("»", "»").replace("âº", "›").replace("«", "«").replace("â¹", "‹"))
|
||||||
return render(scraped)
|
return render(scraped)
|
||||||
|
|
||||||
@app.route('/random.php')
|
|
||||||
def random():
|
|
||||||
scraped = scrape(RANDOM)
|
|
||||||
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
|
||||||
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
|
||||||
return render(scraped)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(port=8000)
|
app.run(port=8000)
|
||||||
|
|
Loading…
Reference in New Issue