utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import urllib.request
  2. import re
  3. from bs4 import BeautifulSoup
  4. # Get thumbnail from an URL
  5. def getThumbnail(urlParam):
  6. url = "/".join((urlParam).split("/")[:5])
  7. websource = urllib.request.urlopen(url)
  8. soup = BeautifulSoup(websource.read(), "html.parser")
  9. image = re.search("(?P<url>https?://[^\s]+)", str(soup.find("img", {"itemprop": "image"}))).group("url")
  10. thumbnail = "".join(image.split('"')[:1]).replace('"','')
  11. return thumbnail
  12. # Replace multiple substrings from a string
  13. def replace_all(text, dic):
  14. for i, j in dic.items():
  15. text = text.replace(i, j)
  16. return text
  17. # Escape special characters
  18. def filter_name(name):
  19. dic = {
  20. "♥": "\♥",
  21. "♀": "\♀",
  22. "♂": "\♂",
  23. "♪": "\♪",
  24. "☆": "\☆"
  25. }
  26. return replace_all(name, dic)
  27. # Check if the show's name ends with a show type and truncate it
  28. def truncate_end_show(show):
  29. SHOW_TYPES = (
  30. '- TV',
  31. '- Movie',
  32. '- Special',
  33. '- OVA',
  34. '- ONA',
  35. '- Manga',
  36. '- Manhua',
  37. '- Manhwa',
  38. '- Novel',
  39. '- One-Shot',
  40. '- Doujinshi',
  41. '- Music',
  42. '- OEL',
  43. '- Unknown'
  44. )
  45. if show.endswith(SHOW_TYPES):
  46. return show[:show.rindex('-') - 1]
  47. return show