/cogs/ServantStats.py

https://bitbucket.org/TheMysteryofDoom/doom-utilityapp · Python · 105 lines · 104 code · 1 blank · 0 comment · 1 complexity · 2c6d084bab84d766fb6d4266333efaee MD5 · raw file

  1. import discord
  2. import re
  3. from discord.ext import commands
  4. try: # check if BeautifulSoup4 is installed
  5. from bs4 import BeautifulSoup
  6. soupAvailable = True
  7. except:
  8. soupAvailable = False
  9. import aiohttp
  10. class ServantStats:
  11. """This is Homura's CirnoRipper Features!"""
  12. def __init__(self, bot):
  13. self.bot = bot
  14. self.searchHelper = {'billy':'Billy_The_Kid',
  15. 'ecchan':'Mysterious_Heroine_X_(Alter)',
  16. 'artoria':'Artoria_Pendragon',
  17. 'artoria alter':'Artoria_Pendragon_(Alter)',
  18. 'artoria_alter':'Artoria_Pendragon_(Alter)'}
  19. @commands.command()
  20. async def servant(self, *, searcharg):
  21. """servant stats displayed in blocks"""
  22. # Call a Search Refiner Here
  23. searcharg.replace(" ", "_")
  24. if searcharg.lower() in self.searchHelper:
  25. searcharg = self.searchHelper[searcharg.lower()]
  26. else:
  27. pass
  28. # ==========================
  29. url = "http://fategrandorder.wikia.com/wiki/"+searcharg
  30. async with aiohttp.get(url) as response:
  31. soup = BeautifulSoup(await response.text(), 'html.parser')
  32. try:
  33. base = soup.find("div", {"class": "ServantInfoStatsWrapper"})
  34. image = soup.find("a", title="Stage 4")
  35. tableset = base.find_all("table", {"class": "closetable"})
  36. # ================
  37. stat = soup.find("p", {"class": "ServantInfoName"}).get_text()
  38. embed = discord.Embed(title="Servant", description=stat, color=0xdb3c33)
  39. embed.set_image(url=image['href'])
  40. # ================
  41. tablework = tableset[1]
  42. td_only = tablework.find_all("td")
  43. # ================
  44. td_test = td_only[0].get_text() #ID
  45. embed.add_field(name="ID", value=td_test[5:], inline=True)
  46. # ================
  47. td_test = td_only[1].get_text() #Cost
  48. stat = td_test[7:]
  49. if int(stat) == 7:
  50. stat = "★★★"
  51. elif int(stat) == 12:
  52. stat = "★★★★"
  53. elif int(stat) == 16:
  54. stat = "★★★★★"
  55. elif int(stat) == 3:
  56. stat = "★"
  57. elif int(stat) == 4:
  58. stat = "★★"
  59. embed.add_field(name="Rarity", value=stat, inline=True)
  60. # ================ Attack
  61. td_test = td_only[2].get_text() #Attack
  62. embed.add_field(name="Attack (Min/Max)", value=td_test[7:], inline=True)
  63. # ================ HP
  64. td_test = td_only[3].get_text() #HP
  65. embed.add_field(name="HP (Min/Max)", value=td_test[5:], inline=True)
  66. # ================ G ATK
  67. stat = soup.find(string=re.compile('Grail HP')).parent.parent.parent.find_previous('span').get_text() #Grail ATK
  68. embed.add_field(name="Grail ATK (Lv. 100)", value=stat, inline=True)
  69. # ================ G HP
  70. stat = soup.find(string=re.compile('Grail HP')).parent.parent.parent.find_next('span').find_next('span').get_text() #Grail HP
  71. embed.add_field(name="Grail HP (Lv. 100)", value=stat, inline=True)
  72. # ================ Voice Actor
  73. stat = soup.find(string=re.compile('Voice Actor')).parent.parent.parent.get_text() #VoiceActor
  74. embed.add_field(name="Voice Actor", value=stat[13:], inline=True)
  75. # ================ Illustrator
  76. stat = soup.find(string=re.compile('Voice Actor')).parent.parent.parent.find_next('td').get_text() #Illustrator
  77. embed.add_field(name="Illustrator", value=stat[14:], inline=True)
  78. # ================ Alignment
  79. stat = soup.find('a',title="Alignments").parent.parent.get_text() #Alignment
  80. embed.add_field(name="Alignment", value=stat[12:], inline=True)
  81. # ================ Gender
  82. stat = soup.find(string=re.compile('Gender')).parent.parent.get_text()
  83. embed.add_field(name="Gender", value=stat[8:], inline=True)
  84. # ================ Traits
  85. stat = soup.find('a',title="Traits").parent.parent.get_text()
  86. embed.add_field(name="Traits", value=stat[10:], inline=True)
  87. # ================
  88. stat = soup.find("p", {"class": "ServantInfoClass"})
  89. image = stat.find_next('img')
  90. embed.set_thumbnail(url=image['data-src'])
  91. # ================
  92. # embed.add_field(name="TestTitle", value="hi2", inline=True)
  93. await self.bot.say(embed=embed)
  94. except Exception as ex:
  95. await self.bot.say(ex)
  96. def setup(bot):
  97. if soupAvailable:
  98. bot.add_cog(ServantStats(bot))
  99. else:
  100. raise RuntimeError("You need to run `pip3 install beautifulsoup4`")