Browse Source

style: format code with Ruff

Zubarev Grigoriy 7 months ago
parent
commit
cd542af102
3 changed files with 68 additions and 24 deletions
  1. 13 0
      .editorconfig
  2. 25 2
      .gitignore
  3. 30 22
      src/rural_dict/__main__.py

+ 13 - 0
.editorconfig

@@ -0,0 +1,13 @@
+root = true
+
+[**]
+charset = utf-8
+end_of_line = lf
+indent_size = 4
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+max_line_length = 99
+
+[{**.yml,**.yaml}]
+indent_size = 2

+ 25 - 2
.gitignore

@@ -1,2 +1,25 @@
-__pycache__
-*.pyc
+# Project
+.idea/
+.vscode/
+.venv/
+.tests/
+.env
+venv/
+
+# Cache
+__pycache__/
+*.py[cod]
+.cache/
+.ruff_cache/
+.mypy_cache/
+.pytest_cache/
+.coverage/
+
+# Build
+env/
+build/
+_build/
+dist/
+site/
+*.egg-info/
+*.egg

+ 30 - 22
src/rural_dict/__main__.py

@@ -7,53 +7,61 @@ import re
 from bs4 import BeautifulSoup
 from urllib.parse import quote, unquote
 
+
 def scrape(url):
     data = requests.get(url)
-    
+
     our_path = re.sub(r".*://.*/", "/", request.url)
     path = re.sub(r".*://.*/", "/", data.url)
-    if our_path != path and \
-            quote(unquote(re.sub("[?&=]", "", our_path))) != re.sub("[?&=]", "", path):
-                # this is bad ^
-                return f"REDIRECT {path}"
+    if our_path != path and quote(unquote(re.sub("[?&=]", "", our_path))) != re.sub(
+        "[?&=]", "", path
+    ):
+        # this is bad ^
+        return f"REDIRECT {path}"
     ret = []
     soup = BeautifulSoup(data.text, "html.parser")
 
-    defs = [(div, div.get('data-defid')) for div in soup.find_all("div") if div.get('data-defid')]
+    defs = [(div, div.get("data-defid")) for div in soup.find_all("div") if div.get("data-defid")]
     try:
         thumbs_data = {
-            str(entry['defid']): entry
-            for entry
-            in requests.get(
-                'https://api.urbandictionary.com/v0/uncacheable?ids=' + ','.join(defid for (_, defid) in defs)
-            ).json()['thumbs']
+            str(entry["defid"]): entry
+            for entry in requests.get(
+                "https://api.urbandictionary.com/v0/uncacheable?ids="
+                + ",".join(defid for (_, defid) in defs)
+            ).json()["thumbs"]
         }
     except:
         thumbs_data = {}
 
-    for (definition, defid) in defs:
+    for definition, defid in defs:
         word = definition.select("div div h1 a, div div h2 a")[0].text
-        meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents()
-        example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents()
-        contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
-        thumbs_up = thumbs_data.get(defid, {}).get('up')
-        thumbs_down = thumbs_data.get(defid, {}).get('down')
+        meaning = definition.find(attrs={"class": ["break-words meaning mb-4"]}).decode_contents()
+        example = definition.find(
+            attrs={"class": ["break-words example italic mb-4"]}
+        ).decode_contents()
+        contributor = definition.find(attrs={"class": ["contributor font-bold"]})
+        thumbs_up = thumbs_data.get(defid, {}).get("up")
+        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"]})
+    pages = soup.find(attrs={"class": ["pagination text-xl text-center"]})
     if pages == None:
         pages = ""
     return (ret, pages)
 
+
 app = Flask(__name__, template_folder="templates", static_folder="static")
 
-@app.route('/', defaults={'path': ''})
-@app.route('/<path:path>')
+
+@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_template('index.html', data=scraped, term=request.args.get("term"))
+    return render_template("index.html", data=scraped, term=request.args.get("term"))
+
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     from waitress import serve
+
     serve(app, host="0.0.0.0", port=8080)