Exemple #1
0
    def image(self, _):
        """Generates an image with MTB status"""

        resp = requests.get('https://www.trianglemtb.com')
        soup = BeautifulSoup(resp.text, 'html.parser')
        status_dict = {}
        a = soup.findAll("span")
        for item in a:
            if 'CLOSED' in item.text or 'OPEN' in item.text:
                test = item
                status_dict[test.parent.parent.parent.a.
                            text] = True if 'OPEN' in item.text else False
        status = status_dict
        trail_num = len(status.keys())
        # Create a blank image.
        image = Image.new(mode='RGB',
                          size=(DISPLAY_WIDTH, DISPLAY_HEIGHT),
                          color=BACKGROUND_COLOR)
        draw = Draw(image)
        # Determine the spacing of the trails in the image.
        y_stride = DISPLAY_HEIGHT // (trail_num + 1)

        #Draw title text
        draw_text('Mountain Bike',
                  SCREENSTAR_LARGE_REGULAR,
                  TEXT_COLOR,
                  xy=(200, (DISPLAY_HEIGHT / 2) - 20),
                  image=image)
        draw_text('Trail Status',
                  SCREENSTAR_LARGE_REGULAR,
                  TEXT_COLOR,
                  xy=(200, (DISPLAY_HEIGHT / 2)),
                  image=image)

        # Draw each trail status
        trails = list(status.keys())
        for trail_index in range(trail_num):

            if not status[trails[trail_index]]:
                bg_color = HIGHLIGHT_COLOR
                color = HIGHLIGHT_COLOR
            else:
                color = TEXT_COLOR
                bg_color = None

            y = (trail_index + 1) * y_stride
            draw_text(str(trails[trail_index]),
                      SUBVARIO_CONDENSED_MEDIUM,
                      TEXT_COLOR,
                      xy=(400, y - NUMBER_Y_OFFSET),
                      image=image,
                      box_color=bg_color)

            dot = Image.open(DOT_FILE).convert(mode='RGBA')
            dot = dot.resize((15, 15))
            dot_xy = [600, y - dot.width // 2]
            draw.bitmap(dot_xy, dot, color)

        return image
Exemple #2
0
    def image(self, user, width, height):
        """Generates an image with a calendar view."""

        # Show a calendar relative to the current date.
        try:
            time = self._local_time.now(user)
        except DataError as e:
            raise ContentError(e)

        # Get the number of events per day from the API.
        event_counts = self._event_counts(time, user)

        # Create a blank image.
        image = Image.new(mode='RGB',
                          size=(width, height),
                          color=BACKGROUND_COLOR)
        draw = Draw(image)

        # Get this month's calendar.
        try:
            firstweekday = WEEK_DAYS[user.get('first_week_day')]
        except KeyError:
            firstweekday = SUNDAY
        calendar = Calendar(firstweekday=firstweekday)
        weeks = calendar.monthdayscalendar(time.year, time.month)

        # Determine the spacing of the days in the image.
        x_stride = width // (DAYS_IN_WEEK + 1)
        y_stride = height // (len(weeks) + 1)

        # Draw each week in a row.
        for week_index in range(len(weeks)):
            week = weeks[week_index]

            # Draw each day in a column.
            for day_index in range(len(week)):
                day = week[day_index]

                # Ignore days from other months.
                if day == 0:
                    continue

                # Determine the position of this day in the image.
                x = (day_index + 1) * x_stride
                y = (week_index + 1) * y_stride

                # Mark the current day with a squircle.
                if day == time.day:
                    squircle = Image.open(SQUIRCLE_FILE).convert(mode='RGBA')
                    squircle_xy = (x - squircle.width // 2,
                                   y - squircle.height // 2)
                    draw.bitmap(squircle_xy, squircle, HIGHLIGHT_COLOR)
                    number_color = TODAY_COLOR
                    event_color = TODAY_COLOR
                else:
                    number_color = NUMBER_COLOR
                    event_color = HIGHLIGHT_COLOR

                # Draw the day of the month number.
                number = str(day)
                draw_text(number,
                          SUBVARIO_CONDENSED_MEDIUM,
                          number_color,
                          xy=(x, y - NUMBER_Y_OFFSET),
                          image=image)

                # Draw a dot for each event.
                num_events = min(MAX_EVENTS, event_counts[day])
                dot = Image.open(DOT_FILE).convert(mode='RGBA')
                if num_events > 0:
                    events_width = (num_events * dot.width +
                                    (num_events - 1) * DOT_MARGIN)
                    for event_index in range(num_events):
                        event_offset = (event_index *
                                        (dot.width + DOT_MARGIN) -
                                        events_width // 2)
                        dot_xy = [
                            x + event_offset, y + DOT_OFFSET - dot.width // 2
                        ]
                        draw.bitmap(dot_xy, dot, event_color)

        return image