예제 #1
0
    async def cyanide(self, ctx, *, date: str = None):
        """Displays the Cyanide & Happiness comic for the passed date (MM-DD-YYYY) if found."""

        channel = ctx.message.channel
        author = ctx.message.author
        server = ctx.message.guild

        if not self.canDisplay(server):
            return

        if not date:
            # Auto to today's date
            date = dt.datetime.today().strftime("%m-%d-%Y")

        if not self.dateIsValid(date):
            msg = 'Usage: `{}cyanide "[date MM-DD-YYYY]"`'.format(ctx.prefix)
            await channel.send(msg)
            return

        # Can't be after this date.
        todayDate = dt.datetime.today().strftime("%m-%d-%Y")
        # Can't be before this date.
        firstDate = "01-26-2005"

        if not self.isDateBetween(date, firstDate, todayDate):
            msg = "Date out of range. Must be between {} and {}".format(
                firstDate, todayDate)
            await channel.send(msg)
            return

        dateDict = self.dateDict(date)
        # Get Arvhive URL
        getURL = "http://explosm.net/comics/archive/" + dateDict[
            'Year'] + "/" + dateDict['Month']

        gotComic = False
        imageHTML = await ComicHelper.getImageHTML(getURL)
        if imageHTML:
            imagePage = ComicHelper.getCHURL(
                imageHTML, dateDict['Year'] + "." + dateDict['Month'] + "." +
                dateDict['Day'])
            if imagePage:
                comicHTML = await ComicHelper.getImageHTML(imagePage)
                if comicHTML:
                    imageURL = ComicHelper.getCHImageURL(comicHTML)
                    if imageURL:
                        gotComic = True

        if not gotComic:
            msg = 'No comic found for *{}*'.format(date)
            await channel.send(msg)
            return

        imageDisplayName = "Cyanide & Happiness Comic for " + dateDict[
            'Month'] + "-" + dateDict['Day'] + "-" + dateDict['Year']
        # Download Image
        await Message.Embed(title=imageDisplayName,
                            image=imageURL.strip(),
                            url=imageURL.strip(),
                            color=ctx.author).send(ctx)
예제 #2
0
    async def randcyanide(self, ctx):
        """Randomly picks and displays a Cyanide & Happiness comic."""

        channel = ctx.message.channel
        author = ctx.message.author
        server = ctx.message.guild

        if not self.canDisplay(server):
            return

        # Can't be after this date.
        todayDate = dt.datetime.today().strftime("%m-%d-%Y")
        # Can't be before this date.
        firstDate = "01-26-2005"

        # Get a random Julian date between the first comic and today
        gotComic = False
        tries = 0
        while not gotComic:

            if tries >= 10:
                break

            date = self.getRandDateBetween(firstDate, todayDate)

            # Get Arvhive URL
            getURL = "http://explosm.net/comics/archive/" + date[
                'Year'] + "/" + date['Month']

            # Retrieve html and info
            imageHTML = await ComicHelper.getImageHTML(getURL)
            if imageHTML:
                imagePage = ComicHelper.getCHURL(
                    imageHTML,
                    date['Year'] + "." + date['Month'] + "." + date['Day'])
                if imagePage:
                    comicHTML = await ComicHelper.getImageHTML(imagePage)
                    if comicHTML:
                        imageURL = ComicHelper.getCHImageURL(comicHTML)
                        if imageURL:
                            gotComic = True

            tries += 1

        if tries >= 10:
            msg = 'Failed to find working link.'
            await channel.send(msg)
            return

        imageDisplayName = "Cyanide & Happiness Comic for " + date[
            'Month'] + "-" + date['Day'] + "-" + date['Year']
        # Download Image
        await Message.Embed(title=imageDisplayName,
                            image=imageURL.strip(),
                            url=imageURL.strip(),
                            color=ctx.author).send(ctx)