Ejemplo n.º 1
0
    async def peanuts(self, ctx, *, date: str = None):
        """Displays the Peanuts 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
            date = dt.datetime.today().strftime("%m-%d-%Y")

        if not self.dateIsValid(date):
            msg = 'Usage: `{}peanuts "[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 = "10-02-1950"

        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 URL
        getURL = "http://www.gocomics.com/peanuts/" + dateDict[
            'Year'] + "/" + dateDict['Month'] + "/" + dateDict['Day']

        # Retrieve html and info
        imageHTML = await ComicHelper.getImageHTML(getURL)

        # Comment out to test
        '''if imageHTML == None:
			msg = 'No comic found for *{}*'.format(date)
			await channel.send(msg)
			return'''

        imageURL = ComicHelper.getPeanutsImageURL(imageHTML)

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

        imageDisplayName = "Peanuts Comic for " + dateDict[
            'Month'] + "-" + dateDict['Day'] + "-" + dateDict['Year']
        # Download Image
        await Message.Embed(title=imageDisplayName,
                            image=imageURL,
                            url=imageURL,
                            color=ctx.author).send(ctx)
Ejemplo n.º 2
0
    async def randpeanuts(self, ctx):
        """Randomly picks and displays a Peanuts 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 = "10-02-1950"

        # 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 URL
            getURL = "http://www.gocomics.com/peanuts/" + date[
                'Year'] + "/" + date['Month'] + "/" + date['Day']
            # Retrieve html and info
            imageHTML = await ComicHelper.getImageHTML(getURL)

            if imageHTML:
                imageURL = ComicHelper.getPeanutsImageURL(imageHTML)
                if imageURL:
                    gotComic = True

            tries += 1

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

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