main.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. from flask import Flask, render_template, request, redirect
  3. import requests
  4. import html
  5. import re
  6. from bs4 import BeautifulSoup
  7. def scrape(url, arg=None):
  8. if arg == None:
  9. data = requests.get(url)
  10. else:
  11. data = requests.get(f"{url}{arg}")
  12. if data.status_code == 200:
  13. our_path = re.sub(r".*://.*/", "/", request.url)
  14. path = re.sub(r".*://.*/", "/", data.url)
  15. print(our_path, path)
  16. if our_path != path:
  17. return f"REDIRECT {path}"
  18. ret = []
  19. soup = BeautifulSoup(data.text, "html.parser")
  20. for div in soup.find_all("div"):
  21. defid = div.get('data-defid')
  22. if defid != None:
  23. definition = soup.find(attrs={"data-defid": [defid]})
  24. word = definition.select("div div h1 a, div div h2 a")[0].text
  25. meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents()
  26. example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents()
  27. contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
  28. ret.append([defid, word, meaning, example, contributor])
  29. pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
  30. if pages == None:
  31. pages = ""
  32. return (ret, pages)
  33. else:
  34. return f"Couldn't get data from Urban Dictionary\n{data.status_code}"
  35. def render(data):
  36. return render_template('index.html', data=data)
  37. app = Flask(__name__, template_folder="templates", static_folder="static")
  38. @app.route('/', defaults={'path': ''})
  39. @app.route('/<path:path>')
  40. def catch_all(path):
  41. scraped = scrape(f"https://urbandictionary.com/{re.sub(r'.*://.*/', '/', request.url)}")
  42. if type(scraped) == str and scraped.startswith("REDIRECT"):
  43. return redirect(scraped.replace("REDIRECT ", ""), 302)
  44. scraped = (scraped[0], str(scraped[1]).replace("»", "»").replace("›", "›").replace("«", "«").replace("‹", "‹"))
  45. return render(scraped)
  46. if __name__ == '__main__':
  47. app.run(port=8000)