Introduction

For years I have been playing ‘Clash of Clans’. A strategy game that requires you to actively engage with others in either direct battles, or group based battles. You can improve your village by leveling-up housing, spells, troops, the town hall, heroes, having boosters etc. Ofcourse the makers allow you to play for free, which can take quite some time to get progress, or you can buy additional resources and improvements that speed up the process.

I have been playing this since I think 2014, with in between some resting periods. I think you cannot play this continuously without being bored or frustrated that things take a long time and get more expensive (with internal resources) after every upgrade.

My last round was from 2016 till just a few weeks ago. I played for the clan Holland Dukes, a nice group of people, with a good mixture of young people and older people. Both are represented in the group as member, Elder and coLeaders.

Why writing additional scripts?

You can view your stats mainly through the game, but that is more difficult when you want to get an overview of multiple users or see others stats. I always had a site for Holland Dukes, but it was not really filled with proper content. I planned on working with this when I tried to understand more of gohugo (a static website generator, written in go). So in the end I did. I needed to workout how Gohugo was going to help me with this first. If someone wants to know how I did that, let me know how I can help and I will try to assist.

After having the skeleton for the site, which added Chart.JS and more stuff I needed to write several scripts that got me the API data. I decided to write these scripts in Python since it is multi-portable and can talk to API easily. I never wrote something like this before so it was a learning experience. I fetched the clan details because every detail needed was inside.

Clash of Clans

The API code: Python

Below is the code that I used to fetch the clan information from the API.

#!/usr/bin/env python3

import requests, json, urllib, time
home_token = 'home_token here'

# version used on webhost itself
token = 'production_token here'

cur_time = int(str(time.time()).replace('.', ''))
clantag = 'clan_tag'

headers = {'Authorization': 'Bearer ' + token }
url_clan = 'https://api.clashofclans.com/v1/clans/%23' + clantag[1:]

players = []
all_info = {}

out_all = '../json_data/clan-info.json'
out_clan = '../json_data/clan-info-clan.json'
out_players = '../json_data/clan-info-players.json'

r = requests.get(url_clan, headers=headers)
if r.ok:
   json_clan = json.loads(r.text)

updated_line = { "updatedOn": cur_time }
clan_line = { "clan": json_clan }
all_info = updated_line
all_info.update(clan_line)

for player in json_clan['memberList']:
    player_tag = player['tag']
    url_player = 'https://api.clashofclans.com/v1/players/%23' + player_tag[1:]
    rp = requests.get(url_player, headers=headers)
    if rp.ok:
       json_player = json.loads(rp.text)
       players.append(json_player)

player_line = { "players": players }
all_info.update(player_line)

with open(out_all, "w") as fw_all:
    fw_all.write(json.dumps(all_info))
    fw_all.close()

with open(out_clan, "w") as fw_clan:
    fw_clan.write(json.dumps(json_clan))
    fw_clan.close()

with open(out_players, "w") as fw_players:
    fw_players.write(json.dumps(players))
    fw_players.close()

Parsing of the fetched data

Next to fetching the data with the API, I needed to parse the returned JSON and do something with it. I write the name_ variables to a gohugo markdown file where it is being used in the templates to generate a result.

For gohugo to understand these examples I needed to escape the curly brackets later on.

Sure enough I could not use variables instead and print them directly, which would make the code a lot smaller, but from my POV that would reduce readability.

#!/usr/bin/env python3

import json

json_file_all = 'json_data/clan-info.json'
json_file = '../json_data/clan-info-players.json'

with open(json_file) as f:
     data = json.load(f)

for player in data:
    name = player['name']
    name_tag = player['tag']
    name_explevel = player['expLevel']
    name_thlevel = player['townHallLevel']
    name_trophies = player['trophies']
    name_besttrophies = player['bestTrophies']
    name_warstars = player['warStars']
    name_attackwins = player['attackWins']
    name_defensewins = player['defenseWins']
    name_bhlevel = player['builderHallLevel']
    name_versustrophies = player['versusTrophies']
    name_bestversustrophies = player['bestVersusTrophies']
    name_versusbattlewins = player['versusBattleWins']
    name_versusbattlewincount = player['versusBattleWinCount']
    name_role = player['role']
    name_donations = player['donations']
    name_donationsreceived = player['donationsReceived']
    name_clan = dict(player['clan'])
    if 'townHallWeaponLevel' in player:
        name_townhallweaponlevel = player['townHallWeaponLevel']
    if 'league' in player:
        name_league = dict(player['league'])
    name_achievements = list(player['achievements'])
    name_troops = list(player['troops'])
    name_heroes = list(player['heroes'])
    name_spells = list(player['spells'])

    outfile = '../content/players/' + name + '.md'

    with open(outfile, "w") as fw:
         fw.write("---\n")
         fw.write("title: 'Detail info " + name + "'\n")
         fw.write("coc_css: true \n")
         fw.write("aliases: [/players/" + name + "]\n")
         fw.write("player_name: " + name + "\n")
         fw.write("player_tag: \"" + name_tag + "\"\n")
         fw.write("player_explevel: " + str(name_explevel) + "\n")
         fw.write("player_thlevel: " + str(name_thlevel) + "\n")
         if name_townhallweaponlevel:
            fw.write("player_townhallweaponlevel: " + str(name_townhallweaponlevel) + "\n")
         fw.write("player_trophies: " + str(name_trophies) + "\n")
         fw.write("player_besttrophies: " + str(name_besttrophies) + "\n")
         fw.write("player_warstars: " + str(name_warstars) + "\n")
         fw.write("player_attackwins: " + str(name_attackwins) + "\n")
         fw.write("player_defensewins: " + str(name_defensewins) + "\n")
         fw.write("player_bhlevel: " + str(name_bhlevel) + "\n")
         fw.write("player_versustrophies: " + str(name_versustrophies) + "\n")
         fw.write("player_bestversustrophies: " + str(name_bestversustrophies) + "\n")
         fw.write("player_versusbattlewins: " + str(name_versusbattlewins) + "\n")
         fw.write("player_versusbattlewincount: " + str(name_versusbattlewincount) + "\n")
         fw.write("player_donations: " + str(name_donations) + "\n")
         fw.write("player_donationsreceived: " + str(name_donationsreceived) + "\n")
         fw.write("player_role: " + name_role + "\n")
         fw.write("dict_achievements: " + str(name_achievements) + "\n")
         fw.write("dict_clan: " + str(name_clan) + "\n")
         if name_league:
            fw.write("dict_league: " + str(name_league) + "\n")
         fw.write("dict_troops: " + str(name_troops) + "\n")
         fw.write("dict_heroes: " + str(name_heroes) + "\n")
         fw.write("dict_spells: " + str(name_spells) + "\n")
         fw.write("---\n")
         fw.write("\{\{< clan_player_structure url=\"" + json_file_all + "\" >\}\}\n")
         fw.write("\n")
         fw.close()

    outfile_bb = '../content/builderbase/' + name + '.md'
    with open(outfile_bb, "w") as fbb:
        fbb.write("---\n")
        fbb.write("title: 'Builder Base info for " + name + "'\n")
        fbb.write("coc_css: true \n")
        fbb.write("aliases: [/builderbase/" + name + "]\n")
        fbb.write("player_name: " + name + "\n")
        fbb.write("player_tag: \"" + name_tag + "\"\n")
        fbb.write("player_explevel: " + str(name_explevel) + "\n")
        fbb.write("player_bhlevel: " + str(name_bhlevel) + "\n")
        fbb.write("player_versustrophies: " + str(name_versustrophies) + "\n")
        fbb.write("player_bestversustrophies: " + str(name_bestversustrophies) + "\n")
        fbb.write("player_versusbattlewins: " + str(name_versusbattlewins) + "\n")
        fbb.write("player_versusbattlewincount: " + str(name_versusbattlewincount) + "\n")
        fbb.write("player_donations: " + str(name_donations) + "\n")
        fbb.write("player_donationsreceived: " + str(name_donationsreceived) + "\n")
        fbb.write("player_role: " + name_role + "\n")
        fbb.write("dict_achievements: " + str(name_achievements) + "\n")
        fbb.write("dict_clan: " + str(name_clan) + "\n")
        if name_league:
           fbb.write("dict_league: " + str(name_league) + "\n")
        fbb.write("dict_troops: " + str(name_troops) + "\n")
        fbb.write("dict_heroes: " + str(name_heroes) + "\n")
        fbb.write("dict_spells: " + str(name_spells) + "\n")
        fbb.write("---\n")
        fbb.write("\{\{< clan_player_base_structure url=\"" + json_file_all + "\" >\}\}\n")
        fbb.write("\n")
        fbb.close()