Guest User

Untitled

a guest
Jan 10th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. #~~~FOR SOME UNGODLY REASON THE FIRST TIME A LINK IS SENT ITS NOT REGISTERED AS A LINK, JUST AS TEXT
  2. #THANKFULLY FILES ARE ALWAYS RECOGNIZED AS FILES
  3. #AND THANKFULLY EVEN LINK MESSAGES THAT ARE SEEN AS LINKS STILL HAVE A .content
  4. #FRICK, A SIMPLE REGEX ISNT GOING TO WORK BECAUSE MANY IMAGE LINKS DONT END IN THEIR FILE EXTENSION
  5. #YOU'RE GONNA HAVE TO DO A GET REQUEST ON EVERY LINK LIKE OBJECT AND CHECK THE Content-Type TO SEE IF ITS AN IMAGE
  6. #SO YOU'LL ALSO WANT YOUR BOT TO SAY WHETHER THE IMAGE WAS ACCEPTED OR NOT
  7.  
  8. #!!!THERES A MESSAGE STACK SO YOU DONT HAVE TO CARE ABOUT DOING ASYNC REQUESTS
  9.  
  10. #IT ACTUALLY SEEMS TO BE DOING MULTIPLE EVENTS AT ONCE SO YOU'RE GONNA HAVE TO SAVE EACH IMAGE INDIVIDUALLY
  11. #MAKE A NEW FOLDER WITH THE NAME str(time.time()), FILL THAT FOLDER UNTIL ITS >1000 SECONDS OLD, THEN DEL IT AND START A NEW ONE
  12.  
  13.  
  14. import discord as dis
  15. import requests as req
  16. import re
  17. import asyncio
  18. import logging
  19. import NoidFaxer as noid
  20. from PIL import Image
  21. import random
  22. import os
  23.  
  24. logging.basicConfig(level=logging.INFO) #so that info is logged
  25. #logging.basicConfig(level=logging.ERROR) #so that I can get errors through the logging module
  26.  
  27. bot = dis.Client()
  28. IMG_FILE = 'NoidFaxImg.png'
  29.  
  30. def links_in(s):
  31. return re.findall(r'https?://[^\s]+', s)
  32.  
  33. def get_image_data(url): #UNTESTED
  34. try:
  35. page = req.get(
  36. url,
  37. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'},
  38. timeout = 10
  39. )
  40. if 'image' in page.headers['Content-Type'].lower():
  41. return page.content
  42. except:
  43. pass
  44. return ''
  45.  
  46. def save_and_resize(data): #saves the data to 'NoidFaxImg.png' and resizes it so it can be printed
  47. path = str(random.random()) + '.png'
  48. f = open(path, 'wb')
  49. f.write(data)
  50. f.close()
  51. i = Image.open(path)
  52. i = i.resize((950,1400))
  53. i.save(path)
  54. return path
  55.  
  56. async def say(mess, channel):
  57. await bot.send_message(channel, "`" + mess + "`")
  58.  
  59. async def fax(url, channel): #faxes the image at url to the noid, communicates to user on channel
  60. for attempt in range(2): #try to download the data twice
  61. data = get_image_data(url)
  62. if data: #if image data was succesfully found
  63. print(url)
  64. await say("Got image data! Resizing image...", channel)
  65. f_path = save_and_resize(data)
  66. await say("Resized, Faxing...", channel)
  67. noid.sendFile(f_path, '[email protected]')
  68. os.remove(f_path)
  69. await say("Payload delivered, may god help us all.", channel)
  70. break #don't retry
  71. else: #if image data was not found
  72. error = "Error: Could not get image data from " + url
  73. if attempt == 0:
  74. error += '\nRetrying...'
  75. await say(error, channel)
  76.  
  77. @bot.event #pass on_message corutine to bot's message event listener
  78. async def on_message(mess): #fired when there is a message
  79. if mess.channel.name == 'noid-pipeline' and mess.author.id != bot.user.id: #if its the correct channel and not it's own message
  80. print("Recieved message")
  81. await say("Recieved message", mess.channel)
  82. for link_data in mess.attachments: #go through all the links, fax them if possible
  83. if 'width' in link_data: #if an image file
  84. print('FILE')
  85. await fax(link_data['url'], mess.channel)
  86. for url in links_in(mess.content):
  87. await fax(url, mess.channel) #we'll check if it's an actual image later
  88.  
  89.  
  90. bot.run('Mzk5MDc1NDM2ODM3MjA4MDY0.DTHzlQ.OXi2E5P0PB7CAb56xef0pGArPnA') #login and start the bot
  91.  
  92.  
  93.  
  94. """
  95. @bot.event #pass on_message corutine to bot's message event listener
  96. async def on_message(mess): #fired when there is a message
  97. print("recieved message")
  98. if mess.channel.name == 'noid-pipeline':
  99. links = mess.attachments + mess.embeds #combine links and direct file transfers
  100. for link_data in links: #go through all the links, fax them if possible
  101. if 'type' in link_data and link_data['type'] == 'image': #if a link to an image
  102. print('LINK')
  103. fax(link_data['url'])
  104. elif 'width' in link_data: #if an image file
  105. print('FILE')
  106. fax(link_data['url'])
  107. """
  108.  
  109. """
  110. def call_corutine(call): #BLOCKING, I can only use await inside a corutine itself so I can use this to more simply call corutines
  111. asyncio.get_event_loop().run_until_complete(call)
  112. """
  113.  
  114. """
  115. def save_and_resize(data): #saves the data to 'NoidFaxImg.png' and resizes it so it can be printed
  116. f = open(IMG_FILE, 'wb')
  117. f.write(data)
  118. f.close()
  119. i = Image.open(IMG_FILE)
  120. w2, h2 = (i.width, i.height)
  121. while w2 < 800 or w2 < 800: #just keep doubling it until its good (I HAVE ACTUALLY CONFIRMED THAT IT WILL PRINT AN 800X800 IMAGE)
  122. w2 *= 2
  123. h2 *= 2
  124. i = i.resize((w2,h2))
  125. i.save(IMG_FILE)
  126. """
Advertisement
Add Comment
Please, Sign In to add comment