Browse Source

Merge pull request 'Add thumbs count' (#1) from ncts/rural-dict:main into main

Reviewed-on: https://codeberg.org/cobra/rural-dict/pulls/1
Skylar "The Cobra" Widulski 1 year ago
parent
commit
88129772a5
3 changed files with 28 additions and 13 deletions
  1. 3 3
      instances.json
  2. 21 9
      main.py
  3. 4 1
      templates/index.html

+ 3 - 3
instances.json

@@ -8,11 +8,11 @@
 	"owner_website": "https://vern.cc"
     },
     {
-	"clearnet": "https://rd.bloatcat.tk",
+	"clearnet": "https://rd.bloat.cat",
 	"tor": null,
 	"i2p": null,
-	"country": "IS",
+	"country": "RO",
 	"owner_name": "bloatcat",
-	"owner_website": "https://bloatcat.tk"
+	"owner_website": "https://bloat.cat"
     }
 ]

+ 21 - 9
main.py

@@ -18,15 +18,27 @@ def scrape(url):
                 return f"REDIRECT {path}"
     ret = []
     soup = BeautifulSoup(data.text, "html.parser")
-    for div in soup.find_all("div"):
-        defid = div.get('data-defid')
-        if defid != None:
-            definition = soup.find(attrs={"data-defid": [defid]})
-            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"]})
-            ret.append([defid, word, meaning, example, contributor])
+
+    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']
+        }
+    except:
+        thumbs_data = {}
+
+    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')
+        ret.append([defid, word, meaning, example, contributor, thumbs_up, thumbs_down])
     pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
     if pages == None:
         pages = ""

+ 4 - 1
templates/index.html

@@ -21,7 +21,7 @@
       <a href="https://git.vern.cc/cobra/rural-dict">Source Code</a>
     </center>
     <br>
-    {% for defid, word, definition, example, author in data[0] %}
+    {% for defid, word, definition, example, author, thumbs_up, thumbs_down in data[0] %}
     
     <div class="{{ defid }}">
       <a href="/define.php?term={{ word }}">
@@ -30,6 +30,9 @@
       <p>{{ definition|safe }}</p>
       <p><i>{{ example|safe }}</i></p>
       <p>{{ author|safe }}</p>
+      {% if thumbs_up and thumbs_down %}
+      <p>{{ thumbs_up|safe }} up, {{ thumbs_down|safe }} down</p>
+      {% endif %}
     </div>
     <br>
     {% endfor %}