使用Clash Royale API,構建你的皇室應用

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

使用

<code>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" # 用戶代理
)/<code>

獲取所有卡牌清單

<code>all_cards = client.get_all_cards()

for card in all_cards:
print(card)/<code>

返回數據示例:

<refreshable>

'name': 'Knight',

'id': 26000000,

'maxLevel': 13,

'iconUrls': {

'medium': 'https: //api-assets.clashroyale.com/cards/300/jAj1Q5rclXxU9kVImGqSJxa4wEMfEhvwNQ_4jiGUuqg.png'

}

}>

獲取卡牌具體信息

<code>cat_info = client.get_card_info('Knight')
print(cat_info)/<code>

返回數據示例:

{

'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

}

獲取部落信息

<code>clan = client.get_clan('922QPY8')  # 傳入參數為部落標籤
print(clan)/<code>

返回數據示例:

<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

}

]

}>

獲取部落戰信息

<code>clan_war = client.get_clan_war('922QPY8')
print(clan_war)/<code>

返回數據示例:

<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

}

]

}>

獲取玩家信息

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

返回數據示例:

<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

}>

獲取玩家對戰信息

<code>player_battles = client.get_player_battles('U2CV2UGY')
for battle in player_battles:
print(battle)/<code>

返回數據示例:

<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場可以獲得什麼寶箱)

<code>player_chests = client.get_player_chests('U2CV2UGY')
for chest in player_chests:
    print(chest)/<code>

返回數據示例:

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

<refreshable>

獲取頂級部落清單

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

返回數據示例:

<refreshable>

<refreshable>

<refreshable>

獲取頂級玩家清單

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

返回數據示例:

<refreshable>

<refreshable>

<refreshable>

"/<baseattrdict>

/<refreshable>

/<refreshable>

/<refreshable>

/<refreshable>


分享到:


相關文章: