main.py 1.9 KB

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