1
0

myanimelist.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import re
  2. import urllib
  3. import datetime
  4. from bs4 import BeautifulSoup
  5. import myanimebot.utils as utils
  6. import myanimebot.globals as globals
  7. def get_thumbnail(urlParam):
  8. ''' Returns the MAL media thumbnail from a link '''
  9. url = "/".join((urlParam).split("/")[:5])
  10. websource = urllib.request.urlopen(url)
  11. soup = BeautifulSoup(websource.read(), "html.parser")
  12. image = re.search(r'(?P<url>https?://[^\s]+)', str(soup.find("img", {"itemprop": "image"}))).group("url")
  13. thumbnail = "".join(image.split('"')[:1]).replace('"','')
  14. return thumbnail
  15. def build_feed_from_data(data, user : utils.User, image, pubDateRaw, type : utils.MediaType) -> utils.Feed:
  16. if data is None: return None
  17. if data.description.startswith('-') :
  18. if type == utils.MediaType.MANGA:
  19. data.description = "Rereading " + data.description
  20. else:
  21. data.description = "Rewatching " + data.description
  22. status, progress, episodes = break_rss_description_string(data.description)
  23. media = utils.Media(name=data.title,
  24. url=data.link,
  25. episodes=episodes,
  26. image=image,
  27. type=type)
  28. feed = utils.Feed(service=utils.Service.MAL,
  29. date_publication=datetime.datetime.fromtimestamp(pubDateRaw, globals.timezone),
  30. user=user,
  31. status=status,
  32. description=data.description, # TODO To remove, useless now
  33. media=media,
  34. progress=progress)
  35. return feed
  36. def break_rss_description_string(description : str):
  37. ''' Break a MyAnimeList RSS description from a feed into a Status, the progress and the number of episodes '''
  38. # Description example: "Completed - 12 of 12 episodes"
  39. # Split the description starting from the dash
  40. split_desc = description.rsplit('-', 1)
  41. if (len(split_desc) != 2):
  42. globals.logger.error("Error while trying to break MAL RSS description. No '-' found in '{}'.".format(description))
  43. return None, None, None
  44. status_str = split_desc[0]
  45. episodes_progress_and_count = split_desc[1]
  46. status = utils.MediaStatus.from_str(status_str)
  47. # Split the second part of the string (E.g. "12 of 12 episodes") to get the progress
  48. episodes_progress_and_count_split = episodes_progress_and_count.split('of', 1)
  49. if (len(episodes_progress_and_count_split) != 2):
  50. globals.logger.error("Error while trying to break MAL RSS description. No 'of' found between the progress and the episode count in '{}'.".format(description))
  51. return None, None, None
  52. progress = episodes_progress_and_count_split[0].strip()
  53. episodes_count_str = episodes_progress_and_count_split[1].strip()
  54. # Remove the episodes label from our string
  55. episode_count_split = episodes_count_str.split(' ', 1)
  56. if (len(episode_count_split) != 2):
  57. globals.logger.error("Error while trying to break MAL RSS description. No space found between the episode count and the label episodes in '{}'.".format(description))
  58. return None, None, None
  59. episodes_count = episode_count_split[0]
  60. return status, progress, episodes_count