Yoloswagger

abc

Jul 4th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. async def gelbooru(self, ctx, tags : list=[], cname : list=[], rand : list=[]):
  2.  
  3.     urldest = "/index.php?page=dapi&s=post&q=index"
  4.     domain = "www.gelbooru.com"
  5.     request_type = 'GET'
  6.     destination = '/index.php?page=dapi&s=post&q=index'
  7.     json_ = 1
  8.     limit_ = 1
  9.     try:    
  10.         if rand == "yes":
  11.             mesg = "Recherche d'une image sans tags."
  12.             e = discord.Embed(title=mesg, colour=0xe7a854)
  13.             msg = await self.bot.say(embed=e)
  14.             fullurl = "http://www.gelbooru.com/index.php?page=dapi&s=post&q=index"
  15.             async with aiohttp.get(fullurl, headers={'User-Agent': "cyvax"}) as r:
  16.                 site = await r.text()
  17.             countStart = site.find("count=\"")
  18.             countEnd = site.find("\"", countStart+7)
  19.             count = min(int(site[countStart+7:countEnd]), 20000)
  20.             pid_ = str(random.randint(0, count))
  21.  
  22.             urldest = destination + "&json=" + str(json_) + "&pid=" + str(pid_) + "&limit=" + str(limit_)
  23.             tagUnder = "Aucun"
  24.    
  25.         elif rand == "no":
  26.             if "_" in tags:
  27.                 tagUnder = tags.replace("_", " ")
  28.             else:
  29.                 tagUnder = tags
  30.  
  31.             msg1 = "Recherche d'une image avec le tag : {}".format(tags)
  32.             e = discord.Embed(title=msg1, colour=0xe7a854)
  33.             msg = await self.bot.say(embed=e)
  34.             fullurl = "http://www.gelbooru.com/index.php?page=dapi&s=post&q=index&tags={}".format(tags)
  35.             async with aiohttp.get(fullurl, headers={'User-Agent': "cyvax"}) as r:
  36.                 site = await r.text()
  37.             countStart = site.find("count=\"")
  38.             countEnd = site.find("\"", countStart+7)
  39.             count = min(int(site[countStart+7:countEnd]), 20000)
  40.             pid_ = str(random.randint(0, count))
  41.  
  42.             urldest = destination + "&json=" + str(json_) + "&pid=" + str(pid_) + "&tags=" + tags + "&limit=" + str(limit_)
  43.        
  44.         u = http.client.HTTPSConnection(domain)
  45.         u.request(request_type, urldest)
  46.         response = u.getresponse().read()
  47.         jsondata = json.loads(response)
  48.         u.close()
  49.         if cname == "yes":
  50.             mention = "<@405012289859878919>"  
  51.             for e in jsondata:
  52.                 if e["rating"] == "s":
  53.                     imageRating ="rating : safe"
  54.                     messages = "Salut {} tu a eu chaud :D".format(mention)
  55.                     color = "0x00cc99"
  56.                 elif e["rating"] == "q":
  57.                     imageRating = "rating : questionable"
  58.                     messages = "Faudrait peut être penser a te respecter {}".format(mention)
  59.                     color = "0xFF9900"
  60.                 elif e["rating"] == "e":
  61.                     imageRating = "rating : explicit"
  62.                     messages = "On se respecte pas {} ? x)".format(mention)
  63.                     color = "0xe32636"
  64.  
  65.             await self.bot.say(messages)
  66.         else:
  67.             entries = len(jsondata)
  68.             for e in jsondata:
  69.                 if e["rating"] == "s":
  70.                     imageRating ="rating : safe"
  71.                     color = "0x00cc99"
  72.                 elif e["rating"] == "q":
  73.                     imageRating = "rating : questionable"
  74.                     color = "0xe7a854"
  75.                 elif e["rating"] == "e":
  76.                     imageRating = "rating : explicit"
  77.                     color = "0xef25f9"
  78.        
  79.         imagelink = e["file_url"]
  80.         embed=discord.Embed(title=tagUnder, url=imagelink, description=imageRating, color=discord.Colour(value=int(color, 16)))
  81.         embed.set_image(url=imagelink)
  82.         await self.bot.edit_message(msg, embed=embed)
  83.  
  84.  
  85.     except ValueError:
  86.         if not tags:
  87.             error = "ERREUR AUCUN RESULTAT. (API ERROR)"
  88.             embed=discord.Embed(title="ERREUR", description=error, color=0xff0000)
  89.             await self.bot.edit_message(msg, embed=embed)
  90.         else:    
  91.             error = "aucun résultat avec le tag {}".format(tags)
  92.             embed=discord.Embed(title="ERREUR", description=error, color=0xff0000)
  93.             await self.bot.edit_message(msg, embed=embed)
  94.            
  95.     except TypeError:
  96.         error = "API ERROR, RÉPONSE VIDE."
  97.         embed=discord.Embed(title="ERREUR", description=error, color=0xff0000)
  98.         await self.bot.edit_message(msg, embed=embed)
Advertisement
Add Comment
Please, Sign In to add comment