代码之家  ›  专栏  ›  技术社区  ›  Piramyxed

在Scryfall API调用上选择颜色

  •  0
  • Piramyxed  · 技术社区  · 1 年前

    我正在使用python和PyQt6进行一个简单的项目,该项目允许用户选择MTG命令器所需的颜色,然后使用Scryfall API返回该颜色标识的随机命令器。我已经通读了API和常规scryfall的文档,并且已经到了一个我不明白发生了什么的地步。 我正在使用 https://api.scryfall.com/cards/random?q=is%3Acommander 作为基础链接并添加额外内容。

    起初,我以为我可以在一个链中为所有选定的颜色做+c%3A{color},但这只需要这些颜色出现在卡上,有时它会得到一张颜色超出我指定颜色的卡。然后我尝试了一系列的-而不是+,但这只是打破了它。 最终我得到了这样的结果:“-id:wg%20+id:br%20id:c”,但它只得到黑卡,而不是黑卡和红卡。我还尝试了“-id:wgu%20+id:br%20id:c”,因为我意识到我忘记了蓝色,但后来它只返回404。

    文档中是否有我找不到的页面解释了如何仅搜索特定颜色,或者我如何调用特定颜色标识而不在搜索中包括任何其他颜色标识?

    0 回复  |  直到 1 年前
        1
  •  -1
  •   Bench Vue    1 年前

    您可以在中看到指挥官颜色选项 here

    Advanced Search

    Commander Color enter image description here

    https://scryfall.com/search?as=grid&order=name&q=commander:{color characters}+(game:paper)&page={number}
    
    颜色 性格
    白色 W
    蓝色 U
    黑色 B
    红色 R
    绿色 G
    无色 C

    示例

    White color commander cards

    https://scryfall.com/search?as=grid&order=name&q=commander:W+(game:paper)&page=1
    

    White or Red colors commander cards

    https://scryfall.com/search?as=grid&order=name&q=commander:WR+(game:paper)&page=1
    

    后果

    白色指挥官卡

    第1页的卡片

    enter image description here

    特定卡

    enter image description here

    获取JSON数据

    enter image description here

    enter image description here

    // 20240516230035
    // https://api.scryfall.com/cards/e882c9f9-bf30-46b6-bedc-379d2c80e5cb?format=json&pretty=true
    
    {
      "object": "card",
      "id": "e882c9f9-bf30-46b6-bedc-379d2c80e5cb",
      "oracle_id": "629fe1be-272d-465f-b9b1-2ce177410f13",
      "multiverse_ids": [
        
      ],
      "mtgo_id": 91504,
      "arena_id": 77106,
      "tcgplayer_id": 243201,
      "cardmarket_id": 571299,
      "name": "+2 Mace",
      "lang": "en",
      "released_at": "2021-07-23",
      "uri": "https://api.scryfall.com/cards/e882c9f9-bf30-46b6-bedc-379d2c80e5cb",
      "scryfall_uri": "https://scryfall.com/card/afr/1/+2-mace?utm_source=api",
      "layout": "normal",
      "highres_image": true,
      "image_status": "highres_scan",
      "image_uris": {
        "small": "https://cards.scryfall.io/small/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.jpg?1627701221",
        "normal": "https://cards.scryfall.io/normal/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.jpg?1627701221",
        "large": "https://cards.scryfall.io/large/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.jpg?1627701221",
        "png": "https://cards.scryfall.io/png/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.png?1627701221",
        "art_crop": "https://cards.scryfall.io/art_crop/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.jpg?1627701221",
        "border_crop": "https://cards.scryfall.io/border_crop/front/e/8/e882c9f9-bf30-46b6-bedc-379d2c80e5cb.jpg?1627701221"
      },
      "mana_cost": "{1}{W}",
      "cmc": 2.0,
      "type_line": "Artifact — Equipment",
      "oracle_text": "Equipped creature gets +2/+2.\nEquip {3} ({3}: Attach to target creature you control. Equip only as a sorcery.)",
      "colors": [
        "W"
      ],
      "color_identity": [
        "W"
      ],
      "keywords": [
        "Equip"
      ],
      "legalities": {
        "standard": "not_legal",
        "future": "not_legal",
        "historic": "legal",
        "timeless": "legal",
        "gladiator": "legal",
        "pioneer": "legal",
        "explorer": "legal",
        "modern": "legal",
        "legacy": "legal",
        "pauper": "legal",
        "vintage": "legal",
        "penny": "not_legal",
        "commander": "legal",
        "oathbreaker": "legal",
        "standardbrawl": "not_legal",
        "brawl": "legal",
        "alchemy": "not_legal",
        "paupercommander": "legal",
        "duel": "legal",
        "oldschool": "not_legal",
        "premodern": "not_legal",
        "predh": "not_legal"
      },
      "games": [
        "arena",
        "paper",
        "mtgo"
      ],
      "reserved": false,
      "foil": true,
      "nonfoil": true,
      "finishes": [
        "nonfoil",
        "foil"
      ],
      "oversized": false,
      "promo": false,
      "reprint": false,
      "variation": false,
      "set_id": "e1ef87ba-ba92-4573-817f-543b996d2851",
      "set": "afr",
      "set_name": "Adventures in the Forgotten Realms",
      "set_type": "expansion",
      "set_uri": "https://api.scryfall.com/sets/e1ef87ba-ba92-4573-817f-543b996d2851",
      "set_search_uri": "https://api.scryfall.com/cards/search?order=set&q=e%3Aafr&unique=prints",
      "scryfall_set_uri": "https://scryfall.com/sets/afr?utm_source=api",
      "rulings_uri": "https://api.scryfall.com/cards/e882c9f9-bf30-46b6-bedc-379d2c80e5cb/rulings",
      "prints_search_uri": "https://api.scryfall.com/cards/search?order=released&q=oracleid%3A629fe1be-272d-465f-b9b1-2ce177410f13&unique=prints",
      "collector_number": "1",
      "digital": false,
      "rarity": "common",
      "flavor_text": "The weight of this magic weapon falls heavy on the wicked.",
      "card_back_id": "0aeebaf5-8c7d-4636-9e82-8c27447861f7",
      "artist": "Jarel Threat",
      "artist_ids": [
        "0fd89fbd-bd59-4ded-890e-731a8982f278"
      ],
      "illustration_id": "444cd469-4a05-4a58-a098-8af8a7b0f886",
      "border_color": "black",
      "frame": "2015",
      "full_art": false,
      "textless": false,
      "booster": true,
      "story_spotlight": false,
      "edhrec_rank": 14852,
      "preview": {
        "source": "Wizards of the Coast",
        "source_uri": "https://twitter.com/MTG_Arena/status/1410327287317270534",
        "previewed_at": "2021-07-01"
      },
      "prices": {
        "usd": "0.02",
        "usd_foil": "0.05",
        "usd_etched": null,
        "eur": "0.07",
        "eur_foil": "0.15",
        "tix": "0.03"
      },
      "related_uris": {
        "tcgplayer_infinite_articles": "https://tcgplayer.pxf.io/c/4931599/1830156/21018?subId1=api&trafcat=infinite&u=https%3A%2F%2Finfinite.tcgplayer.com%2Fsearch%3FcontentMode%3Darticle%26game%3Dmagic%26partner%3Dscryfall%26q%3D%252B2%2BMace",
        "tcgplayer_infinite_decks": "https://tcgplayer.pxf.io/c/4931599/1830156/21018?subId1=api&trafcat=infinite&u=https%3A%2F%2Finfinite.tcgplayer.com%2Fsearch%3FcontentMode%3Ddeck%26game%3Dmagic%26partner%3Dscryfall%26q%3D%252B2%2BMace",
        "edhrec": "https://edhrec.com/route/?cc=%2B2+Mace"
      },
      "purchase_uris": {
        "tcgplayer": "https://tcgplayer.pxf.io/c/4931599/1830156/21018?subId1=api&u=https%3A%2F%2Fwww.tcgplayer.com%2Fproduct%2F243201%3Fpage%3D1",
        "cardmarket": "https://www.cardmarket.com/en/Magic/Products/Singles/Adventures-in-the-Forgotten-Realms/2-Mace?referrer=scryfall&utm_campaign=card_prices&utm_medium=text&utm_source=scryfall",
        "cardhoarder": "https://www.cardhoarder.com/cards/91504?affiliate_id=scryfall&ref=card-profile&utm_campaign=affiliate&utm_medium=card&utm_source=scryfall"
      }
    }
    

    转换

    要使用Python将带有特殊字符的URL转换为其编码形式,可以使用urlib.parse模块。以下是您的操作方法:

    import urllib.parse
    
    original_url = 'https://scryfall.com/search?as=grid&order=name&q=commander:WR+(game:paper)&page=1'
    encoded_url = urllib.parse.quote(original_url, safe=':/?&=')
    
    print(encoded_url)
    

    后果

    https://scryfall.com/search?as=grid&order=name&q=commander:WR%2B%28game:paper%29&page=1