import asyncio
import aiohttp
import discord
from   discord.ext import commands
import json
import os
import tempfile
import shutil
import time
from   os.path     import splitext
from   PIL         import Image
from   Cogs        import DL

def setup(bot):
	# This module isn't actually a cog
    return

# A helper module for images.

def get_ext(url):
	"""Return the filename extension from url, or ''."""
	parsed = urlparse(url)
	root, ext = splitext(parsed.path)
	return ext[1:]  # or ext if you want the leading '.'

def canDisplay( firstTime, threshold ):
	# Check if enough time has passed since the last picture to display another
	currentTime = int(time.time())
	if currentTime > (int(firstTime) + int(threshold)):
		return True
	else:
		return False

async def download(url, ext : str = "jpg", sizeLimit : int = 8000000, ua : str = 'CorpNewt DeepThoughtBot'):
	"""Download the passed URL and return the file path."""
	# Set up a temp directory
	dirpath = tempfile.mkdtemp()
	tempFileName = url.rsplit('/', 1)[-1]
	# Strip question mark
	tempFileName = tempFileName.split('?')[0]
	imagePath = dirpath + "/" + tempFileName
	rImage = None
	
	# try:
	hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
		'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
		'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
		'Accept-Encoding': 'none',
		'Accept-Language': 'en-US,en;q=0.8',
		'Connection': 'keep-alive'}
	rImage = await DL.async_dl(url)
	print("Got {} bytes".format(len(rImage)))
	#except:
	#	pass
	if not rImage:
		print("{}\n - Returned no data.".format(url))
		remove(dirpath)
		return None

	with open(imagePath, 'wb') as f:
		f.write(rImage)
		#for chunk in rImage.iter_content(chunk_size=1024):
		#	if chunk:
		#		f.write(chunk)

	# Check if the file exists
	if not os.path.exists(imagePath):
		print("{}\n - Doesn't exist.".format(imagePath))
		remove(dirpath)
		return None
	
	# Let's make sure it's less than the passed limit
	imageSize = os.stat(imagePath)
	
	while int(imageSize.st_size) > sizeLimit:
		try:
			# Image is too big - resize
			myimage = Image.open(imagePath)
			xsize, ysize = myimage.size
			ratio = sizeLimit/int(imageSize.st_size)
			xsize *= ratio
			ysize *= ratio
			myimage = myimage.resize((int(xsize), int(ysize)), Image.ANTIALIAS)
			myimage.save(imagePath)
			imageSize = os.stat(imagePath)
		except Exception:
			# Image too big and can't be opened
			print("{}\n - Image too large and can't be opened.".format(imagePath))
			remove(dirpath)
			return None
	try:
		# Try to get the extension
		img = Image.open(imagePath)
		ext = img.format
		img.close()
	except Exception:
		# Not something we understand - error out
		print("{}\n - Couldn't get extension.".format(imagePath))
		remove(dirpath)
		return None
	
	if ext:
		os.rename(imagePath, '{}.{}'.format(imagePath, ext))
		return '{}.{}'.format(imagePath, ext)
	else:
		return imagePath
	
async def upload(path, bot, channel):
	with open (path, 'rb') as f:
		await channel.send(file=discord.File(f))

def addExt(path):
	img = Image.open(path)
	os.rename(path, '{}.{}'.format(path, img.format))
	path = '{}.{}'.format(path, img.format)
	return path
	
def remove(path):
	"""Removed the passed file's containing directory."""
	shutil.rmtree(os.path.dirname(path), ignore_errors=True)

async def get(url, bot, channel, title : str = 'Unknown', ua : str = 'CorpNewt DeepThoughtBot'):
	"""Download passed image, and upload it to passed channel."""

	message = await channel.send('Downloading...')
	afile = await download(url)

	if not afile:
		await message.edit(content='Oh *shoot* - I couldn\'t get that image...')
		return

	await message.edit(content='Uploading...')
	await upload(afile, bot, channel)
	await message.edit(content=title)
	remove(afile)

@@ -1189,383 +1189,15 @@
 %09%0D%0A%09
-# 
 try:%0D%0A%09
-hdr = %7B'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',%0D%0A%09%09'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',%0D%0A%09%09'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',%0D%0A%09%09'Accept-Encoding': 'none',%0D%0A%09%09'Accept-Language': 'en-US,en;q=0.8',%0D%0A%09%09'Connection': 'keep-alive'%7D%0D%0A
 %09rIm
@@ -1227,16 +1227,18 @@
 (url)%0D%0A%09
+%09#
 print(%22G
@@ -1273,17 +1273,16 @@
 ge)))%0D%0A%09
-#
 except:%0D
@@ -1283,17 +1283,16 @@
 cept:%0D%0A%09
-#
 %09pass%0D%0A%09
@@ -1305,24 +1305,25 @@
  rImage:%0D%0A%09%09
+#
 print(%22%7B%7D%5Cn 
@@ -1603,24 +1603,25 @@
 gePath):%0D%0A%09%09
+#
 print(%22%7B%7D%5Cn 
@@ -2217,16 +2217,17 @@
 ned%0D%0A%09%09%09
+#
 print(%22%7B
@@ -2496,16 +2496,17 @@
  out%0D%0A%09%09
+#
 print(%22%7B

Update GetImage.py