|
@@ -1,15 +1,13 @@
|
|
|
-
|
|
|
+import re
|
|
|
+from urllib.parse import quote, unquote
|
|
|
|
|
|
-from flask import Flask, render_template, request, redirect
|
|
|
import requests
|
|
|
-import html
|
|
|
-import re
|
|
|
from bs4 import BeautifulSoup
|
|
|
-from urllib.parse import quote, unquote
|
|
|
+from flask import Flask, redirect, render_template, request
|
|
|
|
|
|
|
|
|
def scrape(url):
|
|
|
- data = requests.get(url)
|
|
|
+ data = requests.get(url, timeout=10)
|
|
|
|
|
|
our_path = re.sub(r".*://.*/", "/", request.url)
|
|
|
path = re.sub(r".*://.*/", "/", data.url)
|
|
@@ -27,10 +25,11 @@ def scrape(url):
|
|
|
str(entry["defid"]): entry
|
|
|
for entry in requests.get(
|
|
|
"https://api.urbandictionary.com/v0/uncacheable?ids="
|
|
|
- + ",".join(defid for (_, defid) in defs)
|
|
|
+ + ",".join(defid for (_, defid) in defs),
|
|
|
+ timeout=10,
|
|
|
).json()["thumbs"]
|
|
|
}
|
|
|
- except:
|
|
|
+ except (KeyError, TypeError):
|
|
|
thumbs_data = {}
|
|
|
|
|
|
for definition, defid in defs:
|
|
@@ -44,9 +43,9 @@ def scrape(url):
|
|
|
thumbs_down = thumbs_data.get(defid, {}).get("down")
|
|
|
ret.append([defid, word, meaning, example, contributor, thumbs_up, thumbs_down])
|
|
|
pages = soup.find(attrs={"class": ["pagination text-xl text-center"]})
|
|
|
- if pages == None:
|
|
|
+ if pages is None:
|
|
|
pages = ""
|
|
|
- return (ret, pages)
|
|
|
+ return ret, pages
|
|
|
|
|
|
|
|
|
app = Flask(__name__, template_folder="templates", static_folder="static")
|
|
@@ -56,7 +55,7 @@ app = Flask(__name__, template_folder="templates", static_folder="static")
|
|
|
@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"):
|
|
|
+ if isinstance(scraped, str) and scraped.startswith("REDIRECT"):
|
|
|
return redirect(scraped.replace("REDIRECT ", ""), 302)
|
|
|
return render_template("index.html", data=scraped, term=request.args.get("term"))
|
|
|
|
|
@@ -64,4 +63,4 @@ def catch_all(path):
|
|
|
if __name__ == "__main__":
|
|
|
from waitress import serve
|
|
|
|
|
|
- serve(app, host="0.0.0.0", port=8080)
|
|
|
+ serve(app, host="0.0.0.0", port=8080)
|