1
0

myanimelist.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = "Re-reading " + data.description
  20. else:
  21. data.description = "Re-watching " + data.description
  22. status, progress, episodes = break_rss_description_string(data.description)
  23. media = utils.Media(id=None,
  24. name=data.title,
  25. url=data.link,
  26. episodes=episodes,
  27. image=image,
  28. type=type)
  29. feed = utils.Feed(service=utils.Service.MAL,
  30. date_publication=datetime.datetime.fromtimestamp(pubDateRaw, globals.timezone),
  31. user=user,
  32. status=status,
  33. description=data.description, # TODO To remove, useless now
  34. media=media,
  35. progress=progress,
  36. score=None,
  37. score_format=None)
  38. return feed
  39. def break_rss_description_string(description : str):
  40. ''' Break a MyAnimeList RSS description from a feed into a Status, the progress and the number of episodes '''
  41. # Description example: "Completed - 12 of 12 episodes"
  42. # Split the description starting from the dash
  43. split_desc = description.rsplit('-', 1)
  44. if (len(split_desc) != 2):
  45. globals.logger.error("Error while trying to break MAL RSS description. No '-' found in '{}'.".format(description))
  46. return None, None, None
  47. status_str = split_desc[0]
  48. episodes_progress_and_count = split_desc[1]
  49. status = utils.MediaStatus.from_str(status_str)
  50. # Split the second part of the string (E.g. "12 of 12 episodes") to get the progress
  51. episodes_progress_and_count_split = episodes_progress_and_count.split('of', 1)
  52. if (len(episodes_progress_and_count_split) != 2):
  53. globals.logger.error("Error while trying to break MAL RSS description. No 'of' found between the progress and the episode count in '{}'.".format(description))
  54. return None, None, None
  55. progress = episodes_progress_and_count_split[0].strip()
  56. episodes_count_str = episodes_progress_and_count_split[1].strip()
  57. # Remove the episodes label from our string
  58. episode_count_split = episodes_count_str.split(' ', 1)
  59. if (len(episode_count_split) != 2):
  60. 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))
  61. return None, None, None
  62. episodes_count = episode_count_split[0]
  63. return status, progress, episodes_count