You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.7KB

  1. from discord.ext.commands import bot
  2. from lxml import html
  3. import random
  4. import requests
  5. from bs4 import BeautifulSoup
  6. from Roxbot import checks
  7. # Warning, this cog sucks so much but hopefully it works and doesn't break the bot too much. Just lazily edited old code and bodged it into this one.
  8. # There is redundant code here that if removed would make it easier. But it might be handy in the future and isn't that bad.
  9. class Imgur():
  10. """Class for all interactions with Imgur"""
  11. def __init__(self):
  12. pass
  13. def removed(self,url):
  14. page = requests.get(url)
  15. soup = BeautifulSoup(page.content, 'html.parser')
  16. if "removed.png" in soup.img["src"]:
  17. return True
  18. else:
  19. return False
  20. def get(self, url):
  21. if url.split(".")[-1] in ("png", "jpg", "jpeg", "gif", "gifv"):
  22. return url
  23. else:
  24. if self.removed(url):
  25. return False
  26. page = requests.get(url)
  27. soup = BeautifulSoup(page.content, 'html.parser')
  28. links = []
  29. for img in soup.find_all("img"):
  30. if "imgur" in img["src"]:
  31. if not img["src"] in links:
  32. links.append(img["src"])
  33. for video in soup.find_all("source"):
  34. if "imgur" in video["src"]:
  35. if not video["src"] in links:
  36. links.append(video["src"])
  37. if len(links) > 1:
  38. return url
  39. else:
  40. if not "http" in links[0]:
  41. links[0] = "https:" + links[0]
  42. return links[0]
  43. class Eroshare():
  44. def __init__(self):
  45. pass
  46. def get(self, url, name=None):
  47. if url.contains("eroshare"):
  48. url = "https://eroshae.com/" + url.split("/")[3]
  49. page = requests.get(url)
  50. tree = html.fromstring(page.content)
  51. links = tree.xpath('//source[@src]/@src')
  52. if links:
  53. return False
  54. links = tree.xpath('//*[@src]/@src')
  55. if len(links) > 2:
  56. return False
  57. for link in links:
  58. if "i." in link and "thumb" not in link:
  59. return "https:" + link
  60. class Scrapper():
  61. def __init__(self):
  62. pass
  63. def linkget(self, subreddit, israndom):
  64. if israndom:
  65. options = [".json?count=1000", "/top/.json?sort=top&t=all&count=1000"]
  66. choice = random.choice(options)
  67. subreddit += choice
  68. html = requests.get("https://reddit.com/r/"+subreddit, headers = {'User-agent': 'RoxBot Discord Bot'})
  69. try:
  70. reddit = html.json()["data"]["children"]
  71. except KeyError:
  72. return False
  73. return reddit
  74. def retriveurl(self, url):
  75. if url.split(".")[-1] in ("png", "jpg", "jpeg", "gif", "gifv", "webm", "mp4", "webp"):
  76. return url
  77. if "imgur" in url:
  78. return Imgur().get(url)
  79. elif "eroshare" in url:
  80. return Eroshare().get(url)
  81. elif "gfycat" in url or "redd.it" in url or "i.reddituploads" in url or "media.tumblr" in url or "streamable" in url:
  82. return url
  83. class Reddit():
  84. def __init__(self, bot_client):
  85. self.bot = bot_client
  86. @bot.command()
  87. async def subreddit(self, ctx, subreddit):
  88. """
  89. Grabs an image or video (jpg, png, gif, gifv, webm, mp4) from the subreddit inputted.
  90. Example:
  91. {command_prefix}subreddit pics
  92. """
  93. subreddit = subreddit.lower()
  94. links = Scrapper().linkget(subreddit, True)
  95. title = ""
  96. if not links:
  97. return await ctx.send("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
  98. url = ""
  99. for x in range(10):
  100. choice = random.choice(links)
  101. title = "**{}** from /r/{}\n".format(choice["data"]["title"], subreddit)
  102. if choice["data"]["over_18"] and not checks.nsfw_predicate(ctx):
  103. return await ctx.send("This server/channel doesn't have my NSFW stuff enabled. This extends to posting NFSW content from Reddit.")
  104. url = Scrapper().retriveurl(choice["data"]["url"])
  105. if url:
  106. break
  107. if not url:
  108. return await ctx.send("I couldn't find any images from that subreddit.")
  109. if url.split("/")[-2] == "a":
  110. text = "This is an album, click on the link to see more. "
  111. else:
  112. text = ""
  113. return await ctx.send(title + text + url)
  114. @bot.command()
  115. async def aww(self, ctx):
  116. """
  117. Gives you cute pics from reddit
  118. """
  119. subreddit = "aww"
  120. return await ctx.invoke(self.subreddit, subreddit=subreddit)
  121. @bot.command()
  122. async def feedme(self, ctx):
  123. """
  124. Feeds you with food porn. Uses multiple subreddits.
  125. Yes, I was very hungry when trying to find the subreddits for this command.
  126. Subreddits: "foodporn", "food", "DessertPorn", "tonightsdinner", "eatsandwiches", "steak", "burgers", "Pizza", "grilledcheese", "PutAnEggOnIt", "sushi"
  127. """
  128. subreddits = ["foodporn", "food", "DessertPorn", "tonightsdinner", "eatsandwiches", "steak", "burgers", "Pizza", "grilledcheese", "PutAnEggOnIt", "sushi"]
  129. subreddit_choice = random.choice(subreddits)
  130. return await ctx.invoke(self.subreddit, subreddit=subreddit_choice)
  131. @bot.command(aliases=["gssp"])
  132. async def gss(self, ctx):
  133. """
  134. Gives you the best trans memes ever
  135. """
  136. subreddit = "gaysoundsshitposts"
  137. return await ctx.invoke(self.subreddit, subreddit=subreddit)
  138. def setup(bot_client):
  139. bot_client.add_cog(Reddit(bot_client))