Merge pull request 'Add thumbs count' (#1) from ncts/rural-dict:main into main
Reviewed-on: https://codeberg.org/cobra/rural-dict/pulls/1
This commit is contained in:
commit
88129772a5
|
@ -8,11 +8,11 @@
|
||||||
"owner_website": "https://vern.cc"
|
"owner_website": "https://vern.cc"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"clearnet": "https://rd.bloatcat.tk",
|
"clearnet": "https://rd.bloat.cat",
|
||||||
"tor": null,
|
"tor": null,
|
||||||
"i2p": null,
|
"i2p": null,
|
||||||
"country": "IS",
|
"country": "RO",
|
||||||
"owner_name": "bloatcat",
|
"owner_name": "bloatcat",
|
||||||
"owner_website": "https://bloatcat.tk"
|
"owner_website": "https://bloat.cat"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
30
main.py
30
main.py
|
@ -18,15 +18,27 @@ def scrape(url):
|
||||||
return f"REDIRECT {path}"
|
return f"REDIRECT {path}"
|
||||||
ret = []
|
ret = []
|
||||||
soup = BeautifulSoup(data.text, "html.parser")
|
soup = BeautifulSoup(data.text, "html.parser")
|
||||||
for div in soup.find_all("div"):
|
|
||||||
defid = div.get('data-defid')
|
defs = [(div, div.get('data-defid')) for div in soup.find_all("div") if div.get('data-defid')]
|
||||||
if defid != None:
|
try:
|
||||||
definition = soup.find(attrs={"data-defid": [defid]})
|
thumbs_data = {
|
||||||
word = definition.select("div div h1 a, div div h2 a")[0].text
|
str(entry['defid']): entry
|
||||||
meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents()
|
for entry
|
||||||
example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents()
|
in requests.get(
|
||||||
contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
|
'https://api.urbandictionary.com/v0/uncacheable?ids=' + ','.join(defid for (_, defid) in defs)
|
||||||
ret.append([defid, word, meaning, example, contributor])
|
).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"]})
|
pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
|
||||||
if pages == None:
|
if pages == None:
|
||||||
pages = ""
|
pages = ""
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<a href="http://git.vern.cc/cobra/rural-dict">Source Code</a>
|
<a href="http://git.vern.cc/cobra/rural-dict">Source Code</a>
|
||||||
</center>
|
</center>
|
||||||
<br>
|
<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 }}">
|
<div class="{{ defid }}">
|
||||||
<a href="/define.php?term={{ word }}">
|
<a href="/define.php?term={{ word }}">
|
||||||
|
@ -30,6 +30,9 @@
|
||||||
<p>{{ definition|safe }}</p>
|
<p>{{ definition|safe }}</p>
|
||||||
<p><i>{{ example|safe }}</i></p>
|
<p><i>{{ example|safe }}</i></p>
|
||||||
<p>{{ author|safe }}</p>
|
<p>{{ author|safe }}</p>
|
||||||
|
{% if thumbs_up and thumbs_down %}
|
||||||
|
<p>{{ thumbs_up|safe }} up, {{ thumbs_down|safe }} down</p>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<br>
|
<br>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
Loading…
Reference in New Issue