Advertisement
Guest User

printer

a guest
Feb 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script language="JavaScript">
  2.  
  3. var nl = getNewLine()
  4.  
  5. function getNewLine() {
  6.     var agent = navigator.userAgent
  7.  
  8.     if (agent.indexOf("Win") >= 0)
  9.         return "\r\n"
  10.     else
  11.         if (agent.indexOf("Mac") >= 0)
  12.             return "\r"
  13.  
  14.     return "\r"
  15.  
  16. }
  17.  
  18. pagecode = 'import asyncio
  19. import discord
  20. import time
  21. import os
  22. import random
  23. import math
  24. import numpy as np
  25. from   PIL import Image
  26. from   discord.ext import commands
  27. from   Cogs import GetImage
  28. from   Cogs import DisplayName
  29. from   Cogs import Message
  30.  
  31. def setup(bot):
  32.     # Add the bot and deps
  33.     settings = bot.get_cog("Settings")
  34.     bot.add_cog(Printer(bot, settings))
  35.  
  36. class Printer:
  37.  
  38.     # Init with the bot reference
  39.     def __init__(self, bot, settings):
  40.         self.bot = bot
  41.         self.settings = settings
  42.  
  43.     def canDisplay(self, server):
  44.         # Check if we can display images
  45.         lastTime = int(self.settings.getServerStat(server, "LastPicture"))
  46.         threshold = int(self.settings.getServerStat(server, "PictureThreshold"))
  47.         if not GetImage.canDisplay( lastTime, threshold ):
  48.             # await self.bot.send_message(channel, \'Too many images at once - please wait a few seconds.\')
  49.             return False
  50.        
  51.         # If we made it here - set the LastPicture method
  52.         self.settings.setServerStat(server, "LastPicture", int(time.time()))
  53.         return True
  54.  
  55.     def _ascii(self, image):
  56.         try:
  57.             chars = np.asarray(list(\' .,:;irsXA253hMHGS#9B&@\'))
  58.             f, WCF, GCF = image, 7/4, .6
  59.             img = Image.open(image)
  60.             # Make sure we have frame 1
  61.             img = img.convert(\'RGBA\')
  62.            
  63.             # Let\'s scale down
  64.             w, h = 0, 0
  65.             adjust = 2
  66.             w = img.size[0]*adjust
  67.             h = img.size[1]
  68.  
  69.             # Make sure we\'re under max params of 50h, 50w
  70.             ratio = 1
  71.             max_wide = 80
  72.             if h*2 > w:
  73.                 if h > max_wide/adjust:
  74.                     ratio = max_wide/adjust/h
  75.             else:
  76.                 if w > max_wide:
  77.                     ratio = max_wide/w
  78.             h = ratio * h
  79.             w = ratio * w
  80.  
  81.             # Shrink to an area of 1900 or so (allows for extra chars)
  82.             target = 1900
  83.             if w*h > target:
  84.                 r = h/w
  85.                 w1 = math.sqrt(target/r)
  86.                 h1 = target/w1
  87.                 w = w1
  88.                 h = h1
  89.  
  90.             S = ( round(w), round(h) )
  91.             img = np.sum( np.asarray( img.resize(S) ), axis=2)
  92.             img -= img.min()
  93.             img = (1.0 - img/img.max())**GCF*(chars.size-1)
  94.             a = "\\n".join( ("".join(r) for r in chars[len(chars)-img.astype(int)-1]))
  95.             a = "```\\n" + a + "```"
  96.             return a
  97.         except Exception:
  98.             pass
  99.         return False
  100.  
  101.     @commands.command(pass_context=True)
  102.     async def printavi(self, ctx, *, member = None):
  103.         """Returns a url to the passed member\'s avatar."""
  104.         if member == None:
  105.             # Assume author
  106.             member = ctx.author
  107.         if type(member) is str:
  108.             new_mem = DisplayName.memberForName(member, ctx.guild)
  109.             if not new_mem:
  110.                 await ctx.send("I couldn\'t find that member...")
  111.                 return
  112.             member = new_mem
  113.         url = member.avatar_url
  114.         if not len(url):
  115.             url = member.default_avatar_url
  116.         url = url.split("?size=")[0]
  117.         name = DisplayName.name(member)
  118.         if name[-1].lower() == "s":
  119.             name += "\' Avatar"
  120.         else:
  121.             name += "\'s Avatar"
  122.         await Message.Embed(title=name, image=url, color=ctx.author).send(ctx)
  123.         # await ctx.send(url)
  124.  
  125.     @commands.command(pass_context=True)
  126.     async def print(self, ctx, *, url = None):
  127.         """DOT MATRIX.  Accepts a url - or picks the first attachment."""
  128.         if not self.canDisplay(ctx.guild):
  129.             return
  130.         if url == None and len(ctx.message.attachments) == 0:
  131.             await ctx.send("Usage: `{}print [url or attachment]`".format(ctx.prefix))
  132.             return
  133.  
  134.         if url == None:
  135.             url = ctx.message.attachments[0].url
  136.  
  137.         # Let\'s check if the "url" is actually a user
  138.         test_user = DisplayName.memberForName(url, ctx.guild)
  139.         if test_user:
  140.             # Got a user!
  141.             url = test_user.avatar_url
  142.             if not len(url):
  143.                 url = test_user.default_avatar_url
  144.             url = url.split("?size=")[0]
  145.  
  146.         message = await ctx.send("Downloading...")
  147.        
  148.         path = await GetImage.download(url)
  149.         if not path:
  150.             await message.edit(content="I guess I couldn\'t print that one...  Make sure you\'re passing a valid url or attachment.")
  151.             return
  152.  
  153.         # Prant that shaz
  154.         final = self._ascii(path)
  155.         if os.path.exists(path):
  156.             GetImage.remove(path)
  157.         if not final:
  158.             await message.edit(content="I couldn\'t print that image...  Make sure you\'re pointing me to a valid image file.")
  159.             return
  160.         if len(final) > 2000:
  161.             # Too many bigs
  162.             await message.edit(content="Whoops!  I ran out of ink - maybe try a different image.")
  163.             return
  164.  
  165.         print_sounds = [ "ZZzzzzzt", "Bzzt", "Vvvvrrrr", "Chhhaakkakaka", "Errrttt", "Kkkkkkkktttt", "Eeehhhnnkkk" ]
  166.  
  167.         msg = "Printing..."
  168.         await message.edit(content=msg)
  169.         for i in range(5):
  170.             await asyncio.sleep(1)
  171.             msg += " " + random.choice(print_sounds) + "..."
  172.             await message.edit(content=msg)
  173.  
  174.         await asyncio.sleep(1)
  175.         await message.edit(content=final)
  176.  
  177. '
  178.  
  179. document.write(pagecode);
  180.  
  181. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement