# 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 random
import datetime as dt
from discord.ext import commands
from urllib.parse import unquote
	from html import unescape
	from html.parser import HTMLParser
from Cogs import DL, Message

		comic_title = u(unquote(comic_title))
		# Check if we need to get a description
		comic_desc = self._walk_replace(html, comic_data["comic_desc"]) if len(comic_data.get("comic_desc",[])) else None
		comic_desc = u(unquote(comic_desc)) if comic_desc else None
		return {"image":comic_url,"url":url,"title":comic_title,"description":comic_desc}

	async def _display_comic(self, ctx, comic, date = None, random = False):
		# Helper to display the comic, or post an error if there was an issue
		message = await Message.EmbedText(
			title="Locating comic...",
			description="Feeling around in the dark trying to find a {} comic...".format(self.comic_data[comic]["name"]),
			color=ctx.author
		).send(ctx)
		if random:
			desc = "a random {} comic".format(self.comic_data[comic]["name"])
			try: comic_out = await self._get_random_comic(self.comic_data[comic])
			except: comic_out = None
		else:
			desc = "{} comic {}".format(self.comic_data[comic]["name"],date if isinstance(date,int) else "for today" if date==None else "for "+date)
			try: comic_out = await self._get_comic(self.comic_data[comic],date)
			except Exception as e:
				print(e)
				comic_out = None
		if not comic_out:
			return await Message.EmbedText(
				title=self.comic_data[comic]["name"]+" Error",
				description="Could not get {} :(".format(desc),
				color=ctx.author
			).edit(ctx,message)
		comic_out["color"] = ctx.author

# 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/Comic.py
import random
import datetime as dt
from discord.ext import commands
from urllib.parse import unquote
	from html import unescape
	from html.parser import HTMLParser
from Cogs import DL, Message

		comic_title = u(unquote(comic_title))
		# Check if we need to get a description
		comic_desc = self._walk_replace(html, comic_data["comic_desc"]) if len(comic_data.get("comic_desc",[])) else None
		comic_desc = u(unquote(comic_desc)) if comic_desc else None
		return {"image":comic_url,"url":url,"title":comic_title,"description":comic_desc}

	async def _display_comic(self, ctx, comic, date = None, random = False):
		# Helper to display the comic, or post an error if there was an issue
		message = await Message.EmbedText(
			title="Locating comic...",
			description="Feeling around in the dark trying to find a {} comic...".format(self.comic_data[comic]["name"]),
			color=ctx.author
		).send(ctx)
		if random:
			desc = "a random {} comic".format(self.comic_data[comic]["name"])
			try: comic_out = await self._get_random_comic(self.comic_data[comic])
			except: comic_out = None
		else:
			desc = "{} comic {}".format(self.comic_data[comic]["name"],date if isinstance(date,int) else "for today" if date==None else "for "+date)
			try: comic_out = await self._get_comic(self.comic_data[comic],date)
			except Exception as e:
				print(e)
				comic_out = None
		if not comic_out:
			return await Message.EmbedText(
				title=self.comic_data[comic]["name"]+" Error",
				description="Could not get {} :(".format(desc),
				color=ctx.author
			).edit(ctx,message)
		comic_out["color"] = ctx.author

		return await Message.EmbedText(**comic_out).edit(ctx,message)
