War LogsΒΆ
This is an example of how to handle war logs.
1import asyncio
2import os
3
4import coc
5
6# email and password is your login credentials used at https://developer.clashofclans.com
7client = coc.login(os.environ["DEV_SITE_EMAIL"], os.environ["DEV_SITE_PASSWORD"], key_names="coc.py tests")
8
9
10async def get_warlog_for_clans(clan_tags: list):
11 war_logs = {}
12 for tag in clan_tags:
13 # if we're not allowed to view warlog (private in game), this will raise an exception
14 try:
15 warlog = await client.get_warlog(tag)
16 except coc.PrivateWarLog:
17 warlog = []
18
19 war_logs[tag] = warlog
20
21 # return a dict of list of war logs: {'tag': [list_of_warlog_objects]}
22 return war_logs
23
24
25async def get_clan_tags_names(name: str, limit: int):
26 clans = await client.search_clans(name=name, limit=limit)
27 # return a list of tuples of name/tag pair ie. [(name, tag), (another name, another tag)]
28 return [(n.name, n.tag) for n in clans]
29
30
31async def get_warlog_opponents_from_clan_name(name: str, no_of_clans: int):
32 clan_tags_names = await get_clan_tags_names(name, no_of_clans)
33
34 # search for war logs with clan tags found
35 war_logs = await get_warlog_for_clans([n[1] for n in clan_tags_names])
36
37 for name, tag in clan_tags_names:
38 # iterate over the wars
39 for war in war_logs[tag]:
40 # if it is a league war we will error below because it does not return a WarLog object,
41 # and thus no opponent
42 if war.is_league_entry:
43 print("League War Season - No opponent info available")
44 else:
45 print("War: {} vs {}".format(war.clan.name, war.opponent.name))
46
47
48if __name__ == "__main__":
49 client.loop.run_until_complete(get_warlog_opponents_from_clan_name("name", 5))
50 client.close()