|
@@ -6,11 +6,6 @@ import html
|
|
|
import re
|
|
|
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):
|
|
|
if arg == None:
|
|
@@ -21,6 +16,7 @@ def scrape(url, arg=None):
|
|
|
if data.status_code == 200:
|
|
|
our_path = re.sub(r".*://.*/", "/", request.url)
|
|
|
path = re.sub(r".*://.*/", "/", data.url)
|
|
|
+ print(our_path, path)
|
|
|
if our_path != path:
|
|
|
return f"REDIRECT {path}"
|
|
|
ret = []
|
|
@@ -39,47 +35,21 @@ def scrape(url, arg=None):
|
|
|
pages = ""
|
|
|
return (ret, pages)
|
|
|
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):
|
|
|
return render_template('index.html', data=data)
|
|
|
|
|
|
app = Flask(__name__, template_folder="templates", static_folder="static")
|
|
|
|
|
|
-@app.route('/')
|
|
|
-def home():
|
|
|
- scraped = scrape(HOME + {True: f"?page={request.args.get('page')}", False: ""} [request.args.get('page') != None])
|
|
|
+@app.route('/', defaults={'path': ''})
|
|
|
+@app.route('/<path:path>')
|
|
|
+def catch_all(path):
|
|
|
+ scraped = scrape(f"https://urbandictionary.com/{re.sub(r'.*://.*/', '/', request.url)}")
|
|
|
if type(scraped) == str and scraped.startswith("REDIRECT"):
|
|
|
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("â¹", "‹"))
|
|
|
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__':
|
|
|
app.run(port=8000)
|