Beispiel #1
0
    def _parse_rewards_column(cls, column, entry):
        """Parses a column from the tournament's reward section.

        Parameters
        ----------
        column: :class:`bs4.BeautifulSoup`
            The parsed content of the column.
        entry: :class:`RewardEntry`
            The reward entry where the data will be stored to.
        """
        col_str = str(column)
        img = column.find('img')
        if img and "tibiacoin" in img["src"]:
            entry.tibia_coins = parse_integer(column.text)
        if img and "tournamentcoin" in img["src"]:
            entry.tournament_coins = parse_integer(column.text)
        if img and "tournamentvoucher" in img["src"]:
            entry.tournament_ticker_voucher = parse_integer(column.text)
        if img and "trophy" in img["src"]:
            m = CUP_PATTERN.search(col_str)
            if m:
                entry.cup = m.group(1)
            m = DEED_PATTERN.search(col_str)
            if m:
                entry.deed = m.group(1)
        if img and "reward" in img["src"]:
            span = column.find('span', attrs={"class": "HelperDivIndicator"})
            mouse_over = span["onmouseover"]
            title, popup = parse_popup(mouse_over)
            label = popup.find('div', attrs={'class': 'ItemOverLabel'})
            entry.other_rewards = label.text.strip()
Beispiel #2
0
    def _parse_badges(self, rows):
        """Parse the character's displayed badges.

        Parameters
        ----------
        rows: :class:`list` of :class:`bs4.Tag`
            A list of all rows contained in the table.
        """
        row = rows[0]
        columns = row.find_all('td')
        for column in columns:
            popup_span = column.find("span", attrs={"class": "HelperDivIndicator"})
            if not popup_span:
                # Badges are visible, but none selected.
                return
            popup = parse_popup(popup_span['onmouseover'])
            name = popup[0]
            description = popup[1].text
            icon_image = column.find("img")
            icon_url = icon_image['src']
            self.account_badges.append(AccountBadge(name, icon_url, description))
Beispiel #3
0
    def from_content(cls, content):
        """Creates an instance of the class from the html content of the event's calendar.

        Parameters
        ----------
        content: :class:`str`
            The HTML content of the page.

        Returns
        -------
        :class:`EventSchedule`
            The event calendar contained in the page

        Raises
        ------
        InvalidContent
            If content is not the HTML of the event's schedule page.
        """
        parsed_content = parse_tibiacom_content(content)

        month_year_div = parsed_content.find("div", {"class": "eventscheduleheaderdateblock"})
        month, year = month_year_regex.search(month_year_div.text).groups()
        month = time.strptime(month, "%B").tm_mon
        year = int(year)

        schedule = cls(month, year)

        events_table = parsed_content.find("table", {"id": "eventscheduletable"})
        day_cells = events_table.find_all("td")
        # Keep track of events that are ongoing
        ongoing_events = []
        # Keep track of all events present in that day
        ongoing_day = 1
        first_day = True
        for day_cell in day_cells:
            day_div = day_cell.find("div")
            day = int(day_div.text)
            # The first cells may belong to the previous month
            if ongoing_day < day:
                month -= 1
            # The last cells may belong to the last month
            if day < ongoing_day:
                month += 1
            ongoing_day = day + 1
            today_events = []
            popup_spans = day_cell.find_all('span', attrs={"class": "HelperDivIndicator"})
            for popup in popup_spans:
                title, popup_content = parse_popup(popup["onmouseover"])
                divs = popup_content.find_all("div")
                # Multiple events can be described in the same popup, they come in pairs, title and content.
                for title, content in zip(*[iter(d.text for d in divs)]*2):
                    title = title.replace(":", "")
                    content = content.replace("• ", "")
                    event = EventEntry(title, content)
                    today_events.append(event)
                    # If this is not an event that was already ongoing from previous days, add to list
                    if event not in ongoing_events:
                        # Only add a start date if this is not the first day of the calendar
                        # We do not know the actual start date of the event.
                        if not first_day:
                            event.start_date = datetime.date(day=day, month=month, year=year)
                        ongoing_events.append(event)
            # Check which of the ongoing events did not show up today, meaning it has ended now
            for pending_event in ongoing_events[:]:
                if pending_event not in today_events:
                    # If it didn't show up today, it means it ended yesterday.
                    end_date = datetime.date(day=day, month=month, year=year) - datetime.timedelta(days=1)
                    pending_event.end_date = end_date
                    schedule.events.append(pending_event)
                    # Remove from ongoing
                    ongoing_events.remove(pending_event)
            first_day = False
        # Add any leftover ongoing events without a end date, as we don't know when they end.
        schedule.events.extend(ongoing_events)
        return schedule