【皇室戰爭】使用Clash Royale API,構建你的皇室應用

Clash Royale API

通過Clash Royale API你可以準確並安全的訪問皇室戰爭的數據,獲取美妙的體驗,開發令人驚歎的工具或是網站。

Clash Royale API保持和遊戲本身一樣的質量保準,而且持續開發和改進着。

第三方包:clashroyale

clashroyale是一個用python編寫的支持異步和同步的對於官方Clash Royale API的封裝。

當然,還有其他語言編寫的包,如Java編寫的jcrapi、PHP編寫的clash-royale-php、Go編寫的goroyale等等。

本文主要針對clashroyale,做簡單介紹。

安裝

開發版本:pip install git+https://github.com/cgrok/clashroyale

穩定版本:pip install clashroyale

使用

import os
import clashroyale


cache_fp = os.path.abspath(  # 緩存文件地址
    os.path.join(
        __file__,
        "../data"
    )
)

table_name = "clashroyale"

client = clashroyale.official_api.Client(
    token="從clashroyale官網申請的apiToken",
    is_async=False,  # 是否使用異步方式
    error_debug=False,  # 是否錯誤調試模式(會爲每個方法都拋出異常)
    session=None,  # http會話,可以是requests.Session,也可以是aiohttp.ClientSession
    timeout=10,  # 請求API超時時間
    url='https://api.clashroyale.com/v1',  # API地址
    cache_fp=cache_fp,  # 緩存文件地址(使用sqlite3作爲緩存數據庫)
    cache_expires=10,  # 客戶端從API請求特定路由之前等待的秒數
    table_name=table_name,  # 緩存數據表名
    camel_case=False,  # 訪問數據時是否使用駝峯式的key
    constants=None,  # 使用常量
    user_agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"  # 用戶代理
)

獲取所有卡牌清單

all_cards = client.get_all_cards()

for card in all_cards:
    print(card)

返回數據示例:

<Refreshable: {
    'name': 'Knight',
    'id': 26000000,
    'maxLevel': 13,
    'iconUrls': {
        'medium': 'https: //api-assets.clashroyale.com/cards/300/jAj1Q5rclXxU9kVImGqSJxa4wEMfEhvwNQ_4jiGUuqg.png'
    }
}>

獲取卡牌具體信息

cat_info = client.get_card_info('Knight')
print(cat_info)

返回數據示例:

{
    'key': 'knight',
    'name': 'Knight',
    'sc_key': 'Knight',
    'elixir': 3,
    'type': 'Troop',
    'rarity': 'Common',
    'arena': 0,
    'description': "A tough melee fighter. The Barbarian's handsome, cultured cousin. Rumor has it that he was knighted based on the sheer awesomeness of his mustache alone.",
    'id': 26000000
}

獲取部落信息

clan = client.get_clan('922QPY8')  # 傳入參數爲部落標籤
print(clan)

返回數據示例:

<Refreshable: {
    'tag': '#922QPY8',
    'name': '白羽部落',
    'type': 'open',
    'description': '【2016.2.21成立】🇨🇳不忘初心,方得始終。寧缺毋濫🏆歡迎積極打部落戰活躍玩家🔥我們一直都在,只因你回頭找到家。認真部落戰🏅參戰打滿3+1❗部落戰勝率低於30%/部落戰連續3次未打/周捐未200✈👉可請假👈常駐加Q羣:***',
    'badgeId': 16000102,
    'clanScore': 53379,
    'clanWarTrophies': 13032,
    'location': {
        'id': 57000056,
        'name': 'China',
        'isCountry': True,
        'countryCode': 'CN'
    },
    'requiredTrophies': 5000,
    'donationsPerWeek': 22320,
    'clanChestStatus': 'inactive',
    'clanChestLevel': 1,
    'clanChestMaxLevel': 0,
    'members': 50,
    'memberList': [
        {
            'tag': '#URPQ8RJP',
            'name': 'DaYang',
            'role': 'member',
            'lastSeen': '20200308T122141.000Z',
            'expLevel': 13,
            'trophies': 5734,
            'arena': {
                'id': 54000017,
                'name': 'MasterIII'
            },
            'clanRank': 1,
            'previousClanRank': 3,
            'donations': 174,
            'donationsReceived': 600,
            'clanChestPoints': 0
        }
    ]
}>

獲取部落戰信息

clan_war = client.get_clan_war('922QPY8')
print(clan_war)

返回數據示例:

<Refreshable: {
    'state': 'warDay',
    'warEndTime': '20200308T170813.886Z',
    'clan': {
        'tag': '#922QPY8',
        'name': '白羽部落',
        'badgeId': 16000102,
        'clanScore': 13032,
        'participants': 28,
        'battlesPlayed': 28,
        'wins': 14,
        'crowns': 28
    },
    'participants': [
        {
            'tag': '#U2CV2UGY',
            'name': '龍少',
            'cardsEarned': 1869,
            'battlesPlayed': 0,
            'wins': 0,
            'collectionDayBattlesPlayed': 3,
            'numberOfBattles': 1
        }
    ],
    'clans': [
        {
            'tag': '#CGCV0Q',
            'name': '#BEASTGANG',
            'badgeId': 16000029,
            'clanScore': 18818,
            'participants': 29,
            'battlesPlayed': 29,
            'wins': 23,
            'crowns': 47
        }
    ]
}>

獲取玩家信息

player = client.get_player('U2CV2UGY')  # 傳入參數爲玩家標籤(個人資料中玩家名字下方#開頭的一串字符串)
print(player)

返回數據示例:

<Refreshable: {
    'tag': '#U2CV2UGY',
    'name': '龍少',
    'expLevel': 13,
    'trophies': 5294,
    'bestTrophies': 5925,
    'wins': 5231,
    'losses': 5229,
    'battleCount': 14023,
    'threeCrownWins': 2842,
    'challengeCardsWon': 2171,
    'challengeMaxWins': 9,
    'tournamentCardsWon': 0,
    'tournamentBattleCount': 298,
    'role': 'coLeader',
    'donations': 180,
    'donationsReceived': 200,
    'totalDonations': 48591,
    'warDayWins': 146,
    'clanCardsCollected': 362035,
    'clan': {
        'tag': '#922QPY8',
        'name': '白羽部落',
        'badgeId': 16000102
    },
    'arena': {
        'id': 54000015,
        'name': 'MasterI'
    },
    'leagueStatistics': {
        'currentSeason': {
            'trophies': 5294,
            'bestTrophies': 5338
        }
    },
    'badges': [
        {
            'name': '1000Wins',
            'progress': 5231
        }
    ],
    'achievements': [
        {
            'name': 'TeamPlayer',
            'stars': 3,
            'value': 11,
            'target': 1,
            'info': 'JoinaClan',
            'completionInfo': None
        }
    ],
    'cards': [
        {
            'name': 'IceGolem',
            'id': 26000038,
            'level': 7,
            'maxLevel': 11,
            'count': 812,
            'iconUrls': {
                'medium': 'https: //api-assets.clashroyale.com/cards/300/r05cmpwV1o7i7FHodtZwW3fmjbXCW34IJCsDEV5cZC4.png'
            }
        }
    ],
    'currentFavouriteCard': {
        'name': 'Rascals',
        'id': 26000053,
        'maxLevel': 13,
        'iconUrls': {
            'medium': 'https: //api-assets.clashroyale.com/cards/300/KV48DfwVHKx9XCjzBdk3daT_Eb52Me4VgjVO7WctRc4.png'
        }
    },
    'starPoints': 35636
}>

獲取玩家對戰信息

player_battles = client.get_player_battles('U2CV2UGY')
for battle in player_battles:
    print(battle)

返回數據示例:

<BaseAttrDict: {
    'type': 'challenge',
    'battleTime': '20200307T143357.000Z',
    'challengeId': 73003042,
    'isLadderTournament': False,
    'arena': {
        'id': 54000041,
        'name': 'LegendaryArena'
    },
    'gameMode': {
        'id': 72000005,
        'name': 'DraftMode'
    },
    'challengeWinCountBefore': 9,
    'deckSelection': 'draft',
    'team': [
        {
            'tag': '#U2CV2UGY',
            'name': '龍少',
            'startingTrophies': 5294,
            'crowns': 2,
            'kingTowerHitPoints': 3500,
            'princessTowersHitPoints': [
                900
            ],
            'clan': {
                'tag': '#922QPY8',
                'name': '白羽部落',
                'badgeId': 16000102
            },
            'cards': [
                {
                    'name': 'Lumberjack',
                    'id': 26000035,
                    'level': 1,
                    'maxLevel': 5,
                    'iconUrls': {
                        'medium': 'https: //api-assets.clashroyale.com/cards/300/E6RWrnCuk13xMX5OE1EQtLEKTZQV6B78d00y8PlXt6Q.png'
                    }
                }
            ]
        }
    ],
    'opponent': [
        {
            'tag': '#882988',
            'name': 'Ignars',
            'startingTrophies': 5174,
            'crowns': 1,
            'kingTowerHitPoints': 2708,
            'clan': {
                'tag': '#YRJ9C0',
                'name': 'España',
                'badgeId': 16000172
            },
            'cards': [
                {
                    'name': 'RoyalGiant',
                    'id': 26000024,
                    'level': 9,
                    'maxLevel': 13,
                    'iconUrls': {
                        'medium': 'https: //api-assets.clashroyale.com/cards/300/mnlRaNtmfpQx2e6mp70sLd0ND-pKPF70Cf87_agEKg4.png'
                    }
                }
            ]
        }
    ],
    'challengeTitle': '20200307LegendaryLadiesDraftChallenge'
}>

獲取玩家寶箱信息(接下來再對戰x場可以獲得什麼寶箱)

player_chests = client.get_player_chests('U2CV2UGY')
for chest in player_chests:
    print(chest)

返回數據示例:

<Refreshable: {'index': 0, 'name': 'Golden Chest'}>
<Refreshable: {'index': 1, 'name': 'Silver Chest'}>
<Refreshable: {'index': 2, 'name': 'Silver Chest'}>
<Refreshable: {'index': 3, 'name': 'Silver Chest'}>
<Refreshable: {'index': 4, 'name': 'Silver Chest'}>
<Refreshable: {'index': 5, 'name': 'Magical Chest'}>
<Refreshable: {'index': 6, 'name': 'Silver Chest'}>
<Refreshable: {'index': 7, 'name': 'Silver Chest'}>
<Refreshable: {'index': 8, 'name': 'Golden Chest'}>
<Refreshable: {'index': 32, 'name': 'Giant Chest'}>
<Refreshable: {'index': 193, 'name': 'Mega Lightning Chest'}>
<Refreshable: {'index': 283, 'name': 'Epic Chest'}>
<Refreshable: {'index': 653, 'name': 'Legendary Chest'}>

獲取頂級部落清單

top_clans = client.get_top_clans('global', limit=3)
for clan in top_clans:
    print(clan)

返回數據示例:

<Refreshable: {'tag': '#PPCLCJG9', 'name': 'AK Syndicate', 'rank': 1, 'previousRank': 1, 'location': {'id': 57000249, 'name': 'United States', 'isCountry': True, 'countryCode': 'US'}, 'clanScore': 65235, 'members': 50, 'badgeId': 16000044}>
<Refreshable: {'tag': '#PGLQ0VQ', 'name': 'Lost Orbit', 'rank': 2, 'previousRank': 2, 'location': {'id': 57000249, 'name': 'United States', 'isCountry': True, 'countryCode': 'US'}, 'clanScore': 64172, 'members': 50, 'badgeId': 16000096}>
<Refreshable: {'tag': '#2LUJ09CC', 'name': 'GRAND CROWN', 'rank': 3, 'previousRank': 3, 'location': {'id': 57000122, 'name': 'Japan', 'isCountry': True, 'countryCode': 'JP'}, 'clanScore': 63649, 'members': 49, 'badgeId': 16000014}>

獲取頂級玩家清單

top_players = client.get_top_players('global', limit=3)
for player in top_players:
    print(player)

返回數據示例:

<Refreshable: {'tag': '#8QQ8QJ99P', 'name': 'phatcat', 'expLevel': 13, 'trophies': 6975, 'rank': 1, 'previousRank': 1, 'clan': {'tag': '#29C98ULY', 'name': '寶貝軍團', 'badgeId': 16000015}, 'arena': {'id': 54000020, 'name': 'Royal Champion'}}>
<Refreshable: {'tag': '#CC0CLGUC', 'name': '☠️ SHADOW ☠️', 'expLevel': 13, 'trophies': 6958, 'rank': 2, 'previousRank': 6, 'clan': {'tag': '#98U890U', 'name': 'EGY FORCES', 'badgeId': 16000043}, 'arena': {'id': 54000020, 'name': 'Royal Champion'}}>
<Refreshable: {'tag': '#220U2GV', 'name': 'RH❄I Viiper', 'expLevel': 13, 'trophies': 6946, 'rank': 3, 'previousRank': 45, 'clan': {'tag': '#RVPJJQP', 'name': 'Pirates', 'badgeId': 16000078}, 'arena': {'id': 54000020, 'name': 'Royal Champion'}}>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章