Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #~~~FOR SOME UNGODLY REASON THE FIRST TIME A LINK IS SENT ITS NOT REGISTERED AS A LINK, JUST AS TEXT
- #THANKFULLY FILES ARE ALWAYS RECOGNIZED AS FILES
- #AND THANKFULLY EVEN LINK MESSAGES THAT ARE SEEN AS LINKS STILL HAVE A .content
- #FRICK, A SIMPLE REGEX ISNT GOING TO WORK BECAUSE MANY IMAGE LINKS DONT END IN THEIR FILE EXTENSION
- #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
- #SO YOU'LL ALSO WANT YOUR BOT TO SAY WHETHER THE IMAGE WAS ACCEPTED OR NOT
- #!!!THERES A MESSAGE STACK SO YOU DONT HAVE TO CARE ABOUT DOING ASYNC REQUESTS
- #IT ACTUALLY SEEMS TO BE DOING MULTIPLE EVENTS AT ONCE SO YOU'RE GONNA HAVE TO SAVE EACH IMAGE INDIVIDUALLY
- #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
- import discord as dis
- import requests as req
- import re
- import asyncio
- import logging
- import NoidFaxer as noid
- from PIL import Image
- import random
- import os
- logging.basicConfig(level=logging.INFO) #so that info is logged
- #logging.basicConfig(level=logging.ERROR) #so that I can get errors through the logging module
- bot = dis.Client()
- IMG_FILE = 'NoidFaxImg.png'
- def links_in(s):
- return re.findall(r'https?://[^\s]+', s)
- def get_image_data(url): #UNTESTED
- try:
- page = req.get(
- url,
- 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'},
- timeout = 10
- )
- if 'image' in page.headers['Content-Type'].lower():
- return page.content
- except:
- pass
- return ''
- def save_and_resize(data): #saves the data to 'NoidFaxImg.png' and resizes it so it can be printed
- path = str(random.random()) + '.png'
- f = open(path, 'wb')
- f.write(data)
- f.close()
- i = Image.open(path)
- i = i.resize((950,1400))
- i.save(path)
- return path
- async def say(mess, channel):
- await bot.send_message(channel, "`" + mess + "`")
- async def fax(url, channel): #faxes the image at url to the noid, communicates to user on channel
- for attempt in range(2): #try to download the data twice
- data = get_image_data(url)
- if data: #if image data was succesfully found
- print(url)
- await say("Got image data! Resizing image...", channel)
- f_path = save_and_resize(data)
- await say("Resized, Faxing...", channel)
- noid.sendFile(f_path, '[email protected]')
- os.remove(f_path)
- await say("Payload delivered, may god help us all.", channel)
- break #don't retry
- else: #if image data was not found
- error = "Error: Could not get image data from " + url
- if attempt == 0:
- error += '\nRetrying...'
- await say(error, channel)
- @bot.event #pass on_message corutine to bot's message event listener
- async def on_message(mess): #fired when there is a message
- if mess.channel.name == 'noid-pipeline' and mess.author.id != bot.user.id: #if its the correct channel and not it's own message
- print("Recieved message")
- await say("Recieved message", mess.channel)
- for link_data in mess.attachments: #go through all the links, fax them if possible
- if 'width' in link_data: #if an image file
- print('FILE')
- await fax(link_data['url'], mess.channel)
- for url in links_in(mess.content):
- await fax(url, mess.channel) #we'll check if it's an actual image later
- bot.run('Mzk5MDc1NDM2ODM3MjA4MDY0.DTHzlQ.OXi2E5P0PB7CAb56xef0pGArPnA') #login and start the bot
- """
- @bot.event #pass on_message corutine to bot's message event listener
- async def on_message(mess): #fired when there is a message
- print("recieved message")
- if mess.channel.name == 'noid-pipeline':
- links = mess.attachments + mess.embeds #combine links and direct file transfers
- for link_data in links: #go through all the links, fax them if possible
- if 'type' in link_data and link_data['type'] == 'image': #if a link to an image
- print('LINK')
- fax(link_data['url'])
- elif 'width' in link_data: #if an image file
- print('FILE')
- fax(link_data['url'])
- """
- """
- def call_corutine(call): #BLOCKING, I can only use await inside a corutine itself so I can use this to more simply call corutines
- asyncio.get_event_loop().run_until_complete(call)
- """
- """
- def save_and_resize(data): #saves the data to 'NoidFaxImg.png' and resizes it so it can be printed
- f = open(IMG_FILE, 'wb')
- f.write(data)
- f.close()
- i = Image.open(IMG_FILE)
- w2, h2 = (i.width, i.height)
- while w2 < 800 or w2 < 800: #just keep doubling it until its good (I HAVE ACTUALLY CONFIRMED THAT IT WILL PRINT AN 800X800 IMAGE)
- w2 *= 2
- h2 *= 2
- i = i.resize((w2,h2))
- i.save(IMG_FILE)
- """
Advertisement
Add Comment
Please, Sign In to add comment