# Path: Cogs/DL.py
# def setup(bot):
# async def async_post_json(url, data = None, headers = None, ssl = None):
# async def async_post_text(url, data = None, headers = None, ssl = None):
# async def async_post_bytes(url, data = None, headers = None, ssl = None):
# async def async_head_json(url, headers = None, ssl = None):
# async def async_dl(url, headers = None, ssl = None):
# async def async_text(url, headers = None, ssl = None):
# async def async_json(url, headers = None, ssl = None):
# 
# Path: Cogs/Message.py
# class Message:
#     def __init__(self, **kwargs):
#         # Creates a new message - with an optional setup dictionary
#         self.max_chars = 2000
#         self.pm_after = kwargs.get("pm_after", 1) # -1 to disable, 0 to always pm
#         self.force_pm = kwargs.get("force_pm", False)
#         self.header = kwargs.get("header", "")
#         self.footer = kwargs.get("footer", "")
#         self.pm_react = kwargs.get("pm_react", "📬")
#         self.message = kwargs.get("message", None)
#         self.file = kwargs.get("file", None) # Accepts a file path
#         self.max_pages = 0
#         self.delete_after = kwargs.get("delete_after",None)
# 
#     def _get_file(self, file_path):
#         if not os.path.exists(file_path):
#             return None
#         # Path exists, let's get the extension if there is one
#         ext = file_path.split(".")
#         fname = "Upload." + ext[-1] if len(ext) > 1 else "Upload"
#         file_handle = discord.File(fp=file_path, filename=fname)
#         return (file_handle, fname)
# 
#     async def _send_message(self, ctx, message, pm = False, file_path = None):
#         # Helper method to send embeds to their proper location
#         send_file = None
#         if not file_path == None:
#             dfile = self._get_file(file_path)
#             if not dfile:
#                 # File doesn't exist...
#                 try:
#                     await ctx.send("An error occurred!\nThe file specified couldn't be sent :(")
#                 except:
#                     # We tried...
#                     pass
#                 return None
#             else:
#                 # Setup our file
#                 send_file = dfile[0]
#         if pm == True and type(ctx) is discord.ext.commands.Context and not ctx.channel == ctx.author.dm_channel:
#             # More than 2 pages - try to dm
#             try:
#                 message = await ctx.author.send(message, file=send_file, delete_after=self.delete_after)
#                 await ctx.message.add_reaction(self.pm_react)
#                 return message
#             except discord.Forbidden:
#                 if self.force_pm:
#                     # send an error message
#                     try:
#                         await ctx.send("An error occurred!\nCould not dm this message to you :(")
#                     except:
#                         # We tried...
#                         pass
#                     return None
#                 pass
#         return await ctx.send(message, file=send_file, delete_after=self.delete_after)
# 
#     async def send(self, ctx):
#         if not ctx or not self.message or not len(self.message):
#             return
#         text_list = textwrap.wrap(
#             self.message,
#             self.max_chars - len(self.header) - len(self.footer),
#             break_long_words=True,
#             replace_whitespace=False)
# 
#         # Only pm if our self.pm_after is above -1
#         to_pm = len(text_list) > self.pm_after if self.pm_after > -1 else False
#         page_count = 1
#         for m in text_list:
#             if self.max_pages > 0 and page_count > self.max_pages:
#                 break
#             message = await self._send_message(ctx, self.header + m + self.footer, to_pm)
#             # Break if things didn't work
#             if not message:
#                 return None
#             page_count += 1
#         return message


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

		os.rename(imagePath, '{}.{}'.format(imagePath, ext))
		return '{}.{}'.format(imagePath, ext)
	else:
		return imagePath
	
async def upload(ctx, file_path, title = None):
	return await Message.Embed(title=title, file=file_path, color=ctx.author)

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."""
	if not path == None and os.path.exists(path):
		shutil.rmtree(os.path.dirname(path), ignore_errors=True)

async def get(ctx, url, title = None, ua : str = 'CorpNewt DeepThoughtBot', **kwargs):
	"""Download passed image, and upload it to passed channel."""
	downl = kwargs.get("download", False)
	if not downl:
		# Just show the embed?
		await Message.Embed(title=title, url=url, image=url, color=ctx.author).send(ctx)
		return
	message = await Message.Embed(description="Downloading...", color=ctx.author).send(ctx)
	afile = await download(url)
	if not afile:
		return await Message.Embed(title="An error occurred!", description="Oh *shoot* - I couldn't get that image...").edit(ctx, message)

# Path: Cogs/DL.py
# def setup(bot):
# async def async_post_json(url, data = None, headers = None, ssl = None):
# async def async_post_text(url, data = None, headers = None, ssl = None):
# async def async_post_bytes(url, data = None, headers = None, ssl = None):
# async def async_head_json(url, headers = None, ssl = None):
# async def async_dl(url, headers = None, ssl = None):
# async def async_text(url, headers = None, ssl = None):
# async def async_json(url, headers = None, ssl = None):
# 
# Path: Cogs/Message.py
# class Message:
#     def __init__(self, **kwargs):
#         # Creates a new message - with an optional setup dictionary
#         self.max_chars = 2000
#         self.pm_after = kwargs.get("pm_after", 1) # -1 to disable, 0 to always pm
#         self.force_pm = kwargs.get("force_pm", False)
#         self.header = kwargs.get("header", "")
#         self.footer = kwargs.get("footer", "")
#         self.pm_react = kwargs.get("pm_react", "📬")
#         self.message = kwargs.get("message", None)
#         self.file = kwargs.get("file", None) # Accepts a file path
#         self.max_pages = 0
#         self.delete_after = kwargs.get("delete_after",None)
# 
#     def _get_file(self, file_path):
#         if not os.path.exists(file_path):
#             return None
#         # Path exists, let's get the extension if there is one
#         ext = file_path.split(".")
#         fname = "Upload." + ext[-1] if len(ext) > 1 else "Upload"
#         file_handle = discord.File(fp=file_path, filename=fname)
#         return (file_handle, fname)
# 
#     async def _send_message(self, ctx, message, pm = False, file_path = None):
#         # Helper method to send embeds to their proper location
#         send_file = None
#         if not file_path == None:
#             dfile = self._get_file(file_path)
#             if not dfile:
#                 # File doesn't exist...
#                 try:
#                     await ctx.send("An error occurred!\nThe file specified couldn't be sent :(")
#                 except:
#                     # We tried...
#                     pass
#                 return None
#             else:
#                 # Setup our file
#                 send_file = dfile[0]
#         if pm == True and type(ctx) is discord.ext.commands.Context and not ctx.channel == ctx.author.dm_channel:
#             # More than 2 pages - try to dm
#             try:
#                 message = await ctx.author.send(message, file=send_file, delete_after=self.delete_after)
#                 await ctx.message.add_reaction(self.pm_react)
#                 return message
#             except discord.Forbidden:
#                 if self.force_pm:
#                     # send an error message
#                     try:
#                         await ctx.send("An error occurred!\nCould not dm this message to you :(")
#                     except:
#                         # We tried...
#                         pass
#                     return None
#                 pass
#         return await ctx.send(message, file=send_file, delete_after=self.delete_after)
# 
#     async def send(self, ctx):
#         if not ctx or not self.message or not len(self.message):
#             return
#         text_list = textwrap.wrap(
#             self.message,
#             self.max_chars - len(self.header) - len(self.footer),
#             break_long_words=True,
#             replace_whitespace=False)
# 
#         # Only pm if our self.pm_after is above -1
#         to_pm = len(text_list) > self.pm_after if self.pm_after > -1 else False
#         page_count = 1
#         for m in text_list:
#             if self.max_pages > 0 and page_count > self.max_pages:
#                 break
#             message = await self._send_message(ctx, self.header + m + self.footer, to_pm)
#             # Break if things didn't work
#             if not message:
#                 return None
#             page_count += 1
#         return message


# Path: Cogs/GetImage.py
import asyncio
import aiohttp
import discord
import json
import os
import tempfile
import shutil
import time
from   discord.ext import commands
from   os.path     import splitext
from   PIL         import Image
from   Cogs        import DL
from   Cogs        import Message
    from urllib.parse import urlparse
    from urlparse import urlparse

		os.rename(imagePath, '{}.{}'.format(imagePath, ext))
		return '{}.{}'.format(imagePath, ext)
	else:
		return imagePath
	
async def upload(ctx, file_path, title = None):
	return await Message.Embed(title=title, file=file_path, color=ctx.author)

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."""
	if not path == None and os.path.exists(path):
		shutil.rmtree(os.path.dirname(path), ignore_errors=True)

async def get(ctx, url, title = None, ua : str = 'CorpNewt DeepThoughtBot', **kwargs):
	"""Download passed image, and upload it to passed channel."""
	downl = kwargs.get("download", False)
	if not downl:
		# Just show the embed?
		await Message.Embed(title=title, url=url, image=url, color=ctx.author).send(ctx)
		return
	message = await Message.Embed(description="Downloading...", color=ctx.author).send(ctx)
	afile = await download(url)
	if not afile:
		return await Message.Embed(title="An error occurred!", description="Oh *shoot* - I couldn't get that image...").edit(ctx, message)

	message = await Message.Embed(description="Uploading...").edit(ctx, message)
