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.

276 lines
7.9KB

  1. from discord.ext.commands import bot
  2. from config.server_config import ServerConfig
  3. from lxml import html
  4. import os
  5. import random
  6. import requests
  7. from bs4 import BeautifulSoup
  8. import checks
  9. # 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.
  10. # 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.
  11. class RedditMedia:
  12. def get(self, url):
  13. return url
  14. class Gfycat():
  15. def __init__(self):
  16. pass
  17. def url_get(self,url):
  18. urlsplit = url.split("/")
  19. urlsplit[2] = "giant." + urlsplit[2]
  20. urlsplit[-1] += ".gif"
  21. urlnew = "/".join(urlsplit)
  22. return urlnew
  23. def get(self,url):
  24. #url2 = self.url_get(url)
  25. url2 = url
  26. return url2
  27. class Imgur():
  28. """Class for all interactions with Imgur"""
  29. def __init__(self):
  30. pass
  31. def removed(self,url):
  32. page = requests.get(url)
  33. soup = BeautifulSoup(page.content, 'html.parser')
  34. if "removed.png" in soup.img["src"]:
  35. return True
  36. else:
  37. return False
  38. def get(self, url):
  39. if url.split(".")[-1] in ("png", "jpg", "jpeg", "gif", "gifv"):
  40. return url
  41. #elif url.split(".")[-1] == "gifv":
  42. # urlsplit = url.split(".")
  43. # urlsplit[-1] = "gif"
  44. # url = ".".join(urlsplit)
  45. # return url"""
  46. else:
  47. if self.removed(url):
  48. return False
  49. page = requests.get(url)
  50. soup = BeautifulSoup(page.content, 'html.parser')
  51. links = []
  52. for img in soup.find_all("img"):
  53. if "imgur" in img["src"]:
  54. if not img["src"] in links:
  55. links.append(img["src"])
  56. for video in soup.find_all("source"):
  57. if "imgur" in video["src"]:
  58. if not video["src"] in links:
  59. links.append(video["src"])
  60. if len(links) > 1:
  61. return url
  62. else:
  63. print(links)
  64. if not "http" in links[0]:
  65. links[0] = "https:" + links[0]
  66. return links[0]
  67. class Eroshare():
  68. def __init__(self):
  69. pass
  70. def album_create(self, name):
  71. self.album_create.hasbeencalled = True
  72. charlist = ("<", ">", '"', ":", "/", "|", "?", "*")
  73. # Can't use these in Windows Dir so next code used to remove chars from title
  74. for char in charlist:
  75. if char in name:
  76. name = name.replace(char, "")
  77. if name not in os.listdir("./"):
  78. os.mkdir("./" + name)
  79. os.chdir("./" + name)
  80. def get(self, url, name=None):
  81. if url.contains("eroshare"):
  82. url = "https://eroshae.com/" + url.split("/")[3]
  83. page = requests.get(url)
  84. tree = html.fromstring(page.content)
  85. links = tree.xpath('//source[@src]/@src')
  86. if links:
  87. return False
  88. #self.album_create(name)
  89. #for link in links:
  90. # if "lowres" not in link:
  91. # wget.download(link)
  92. # print("Downloaded ", link)
  93. links = tree.xpath('//*[@src]/@src')
  94. if len(links) > 2: #and not self.album_create.hasbeencalled:
  95. return False
  96. #self.album_create(name)
  97. for link in links:
  98. if "i." in link and "thumb" not in link:
  99. return "https:" + link
  100. #if link.split("/")[-1] not in os.listdir("./"):
  101. #wget.download("https:" + link)
  102. #print("Downloaded ", link)
  103. #else:
  104. # print("Already exists")
  105. #if album_create.hasbeencalled:
  106. # os.chdir("../")
  107. # album_create.hasbeencalled = False
  108. class Tumblr():
  109. def get(self,url):
  110. return url
  111. class Scrapper():
  112. def __init__(self):
  113. pass
  114. def linkget(self, subreddit, israndom):
  115. if israndom:
  116. options = [".json?count=1000", "/top/.json?sort=top&t=all&count=1000"]
  117. choice = random.choice(options)
  118. subreddit += choice
  119. html = requests.get("https://reddit.com/r/"+subreddit, headers = {'User-agent': 'RoxBot Discord Bot'})
  120. try:
  121. reddit = html.json()["data"]["children"]
  122. except KeyError:
  123. return False
  124. return reddit
  125. def retriveurl(self, url):
  126. url2 = ""
  127. if "imgur" in url:
  128. url2 = Imgur().get(url)
  129. elif "gfycat" in url:
  130. url2 = Gfycat().get(url)
  131. elif "eroshare" in url:
  132. url2 = Eroshare().get(url)
  133. elif "redd.it" in url or "i.reddituploads" in url:
  134. url2 = RedditMedia().get(url)
  135. elif "media.tumblr" in url:
  136. url2 = Tumblr().get(url)
  137. return url2
  138. class Reddit():
  139. def __init__(self, Bot):
  140. self.bot = Bot
  141. self.con = ServerConfig()
  142. self.servers = self.con.servers
  143. @bot.command(pass_context=True)
  144. async def subreddit(self, ctx, subreddit):
  145. """
  146. Grabs an image (png, gif, gifv, webm) from the subreddit inputted.
  147. Exmaple:
  148. {command_prefix}subreddit pics
  149. """
  150. links = Scrapper().linkget(subreddit, True)
  151. title = ""
  152. if not links:
  153. return await self.bot.say("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
  154. url = ""
  155. for x in range(10):
  156. choice = random.choice(links)
  157. title = "**{}** from /r/{}\n".format(choice["data"]["title"], subreddit)
  158. print(checks.is_nfsw_enabled())
  159. if choice["data"]["over_18"] and not checks.nsfw_predicate(ctx):
  160. return await self.bot.say("This server/channel doesn't have my NSFW stuff enabled. This extends to posting NFSW content from Reddit.")
  161. url = Scrapper().retriveurl(choice["data"]["url"])
  162. if url:
  163. break
  164. if not url:
  165. return await self.bot.say("I couldn't find any images from that subreddit.")
  166. if url.split("/")[-2] == "a":
  167. text = "This is an album, click on the link to see more. "
  168. else:
  169. text = ""
  170. return await self.bot.say(title + text + url)
  171. @bot.command(pass_context=True)
  172. async def aww(self, ctx):
  173. """
  174. Gives you cute pics from reddit
  175. """
  176. subreddit = "aww"
  177. links = Scrapper().linkget(subreddit, True)
  178. if not links:
  179. return await self.bot.say("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
  180. choice = random.choice(links)
  181. title = "**{}** from /r/{}\n".format(choice["data"]["title"], subreddit)
  182. if choice["data"]["over_18"] and not checks.nsfw_predicate(ctx):
  183. return await self.bot.say(
  184. "This server doesn't have my NSFW stuff enabled. This extends to posting NFSW content from Reddit.")
  185. url = Scrapper().retriveurl(choice["data"]["url"])
  186. if url.split("/")[-2] == "a":
  187. text = "This is an album, click on the link to see more. "
  188. else:
  189. text = ""
  190. return await self.bot.say(title + text + url)
  191. @bot.command(pass_context=True)
  192. async def feedme(self, ctx):
  193. """
  194. Feeds you with food porn. Uses multiple subreddits
  195. Yes, I was very hungry when trying to find the subreddits for this command.
  196. """
  197. subreddits = ["foodporn", "food", "DessertPorn", "tonightsdinner", "eatsandwiches", "steak", "burgers", "Pizza", "grilledcheese", "PutAnEggOnIt", "sushi"]
  198. subreddit = random.choice(subreddits)
  199. links = Scrapper().linkget(subreddit, True)
  200. if not links:
  201. return await self.bot.say("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
  202. choice = random.choice(links)
  203. title = "**{}** from /r/{}\n".format(choice["data"]["title"], subreddit)
  204. if choice["data"]["over_18"] and not checks.nsfw_predicate(ctx):
  205. return await self.bot.say(
  206. "This server doesn't have my NSFW stuff enabled. This extends to posting NFSW content from Reddit.")
  207. url = Scrapper().retriveurl(choice["data"]["url"])
  208. if url.split("/")[-2] == "a":
  209. text = "This is an album, click on the link to see more. "
  210. else:
  211. text = ""
  212. return await self.bot.say(title + text + url)
  213. @bot.command(pass_context=True)
  214. async def gss(self, ctx):
  215. """
  216. Gives you the best trans memes ever
  217. """
  218. subreddit = "gaysoundsshitposts"
  219. links = Scrapper().linkget(subreddit, True)
  220. if not links:
  221. return await self.bot.say("Error ;-; That subreddit probably doesn't exist. Please check your spelling")
  222. choice = random.choice(links)
  223. title = "**{}** from /r/{}\n".format(choice["data"]["title"], subreddit)
  224. if choice["data"]["over_18"] and not checks.nsfw_predicate(ctx):
  225. return await self.bot.say(
  226. "This server doesn't have my NSFW stuff enabled. This extends to posting NFSW content from Reddit.")
  227. url = Scrapper().retriveurl(choice["data"]["url"])
  228. if url.split("/")[-2] == "a":
  229. text = "This is an album, click on the link to see more. "
  230. else:
  231. text = ""
  232. return await self.bot.say(title + text + url)
  233. def setup(Bot):
  234. Bot.add_cog(Reddit(Bot))