async def build_foreign_info(
            self, soup: bs4.BeautifulSoup,
            second_card: bool) -> List[mtg_global.ForeignNamesDescription]:
        """
        Get the name and MID of this card for each other set it's printed in
        From there, we will get the foreign text/type for the user to decipher
        """
        language_rows = soup.select('table[class^=cardList]')[0]
        language_rows = language_rows.select('tr[class^=cardItem]')

        card_languages: List[mtg_global.ForeignNamesDescription] = list()
        for div in language_rows:
            table_rows = div.findAll('td')

            a_tag = table_rows[0].find('a')
            foreign_mid = a_tag['href'].split('=')[-1]
            card_language_mid = int(foreign_mid)
            card_foreign_name_in_language = a_tag.get_text(strip=True)

            card_language_name = table_rows[1].get_text(strip=True)

            # Download foreign URLs and append
            soup_print = await self.get_card_html(foreign_mid, True)

            # Determine if Card is Normal, Flip, or Split
            div_name = self.determine_layout_and_div_name(
                soup_print, second_card)[1]

            # Get Card Foreign Type
            c_foreign_type = mtg_parse.parse_card_types(soup_print,
                                                        div_name)[3]

            # Following types are optionals, so we just build it here instead
            c_foreign_dict: mtg_global.ForeignNamesDescription = {
                'language': card_language_name,
                'name': card_foreign_name_in_language,
                'multiverseid': card_language_mid,
                'type': c_foreign_type,
                'text': None,
                'flavor': None
            }

            # Get Card Foreign Printed Rules Text
            c_foreign_text = mtg_parse.parse_text_and_color_identity(
                soup_print, div_name, None)[0]
            if c_foreign_text:
                c_foreign_dict['text'] = c_foreign_text

            # Get Card Foreign Flavor Text
            c_flavor_text = mtg_parse.parse_card_flavor(soup_print, div_name)
            if c_flavor_text:
                c_foreign_dict['flavor'] = c_flavor_text

            card_languages.append(c_foreign_dict)

        return card_languages
Exemple #2
0
    async def build_main_part(self,
                              set_name: List[str],
                              card_info: mtg_global.CardDescription,
                              other_cards_holder: Optional[List[object]],
                              second_card: bool = False) -> None:
        """
        This is the main builder for each card. This will put together the card, key by key, until it
        has most of its elements finished. There are some this doesn't encompass at this time, and those
        are handled by subsequent build functions.
        This builder only builds the main page of Gatherer.
        """

        # Parse web page so we can gather all data from it
        card_mid = card_info['multiverseid']
        soup_oracle = await self.get_card_html(card_mid)

        card_layout, div_name, alt_div_name, add_other_card = self.determine_layout_and_div_name(
            soup_oracle, second_card)

        if add_other_card and (other_cards_holder is not None):
            other_card_mid: int = mtg_parse.parse_card_other_id(soup_oracle, alt_div_name)
            other_cards_holder.append(
                self.loop.create_task(self.build_card(set_name, other_card_mid, None, second_card=True)))

        card_info['multiverseid'] = int(card_mid)

        card_info['name'] = mtg_parse.parse_card_name(soup_oracle, div_name)
        card_info['convertedManaCost'] = mtg_parse.parse_card_cmc(soup_oracle, div_name)

        # Get other side's name for the user
        card_other_name = mtg_parse.parse_card_other_name(soup_oracle, div_name, card_layout)
        if card_other_name is not None:
            if second_card:
                insert_order = [card_other_name, card_info['name']]
            else:
                insert_order = [card_info['name'], card_other_name]

            card_info['names'] = insert_order

        # Get card's colors and mana cost
        card_colors, card_cost = mtg_parse.parse_colors_and_cost(soup_oracle, div_name)
        if card_colors:
            card_info['colors'] = card_colors
        if card_cost:
            card_info['manaCost'] = card_cost

        # Get Card Type(s)
        card_super_types, card_types, card_sub_types, full_type = mtg_parse.parse_card_types(soup_oracle, div_name)
        if card_super_types:
            card_info['supertypes'] = card_super_types
        if card_types:
            card_info['types'] = card_types
        if card_sub_types:
            card_info['subtypes'] = card_sub_types
        if full_type:
            card_info['type'] = full_type

        # Get Card Text and Color Identity
        c_text, c_color_identity = mtg_parse.parse_card_text_and_color_identity(soup_oracle, div_name, card_colors)
        if c_text:
            if 'Planeswalker' in full_type:
                # Surround planeswalker activation cost by []
                # Ex: +1 => [+1]
                c_text = re.sub(r'([\+−][0-9]):', r'[\1]:', c_text)

            card_info['text'] = c_text
        if c_color_identity:
            card_info['colorIdentity'] = c_color_identity

        # Get Card Flavor Text
        c_flavor = mtg_parse.parse_card_flavor(soup_oracle, div_name)
        if c_flavor:
            card_info['flavor'] = c_flavor

        # Get Card P/T OR Loyalty OR Hand/Life
        c_power, c_toughness, c_loyalty, c_hand, c_life = mtg_parse.parse_card_pt_loyalty_vanguard(
            soup_oracle, div_name)
        if c_power:
            card_info['power'] = c_power
        if c_toughness:
            card_info['toughness'] = c_toughness
        if c_loyalty:
            card_info['loyalty'] = c_loyalty
        if c_hand:
            card_info['hand'] = c_hand
        if c_life:
            card_info['life'] = c_life

        # Get Card Rarity
        card_info['rarity'] = mtg_parse.parse_card_rarity(soup_oracle, div_name)

        # Get Card Set Number
        c_number = mtg_parse.parse_card_number(soup_oracle, div_name)
        if c_number:
            card_info['number'] = c_number

        # Get Card Artist(s)
        card_info['artist'] = mtg_parse.parse_artists(soup_oracle, div_name)

        # Get Card Watermark
        c_watermark = mtg_parse.parse_watermark(soup_oracle, div_name)
        if c_watermark:
            card_info['watermark'] = c_watermark

        # Get Card Reserve List Status
        if card_info['name'] in mtg_global.RESERVE_LIST:
            card_info['reserved'] = True

        # Get Card Rulings
        c_rulings = mtg_parse.parse_rulings(soup_oracle, div_name)
        if c_rulings:
            card_info['rulings'] = c_rulings

        # Get Card Sets
        card_info['printings'] = mtg_parse.parse_card_sets(soup_oracle, div_name, set_name[1], self.sets_to_build)

        # Get Card Variations
        c_variations = mtg_parse.parse_card_variations(soup_oracle, div_name, card_mid)
        if c_variations:
            card_info['variations'] = c_variations