Skip to content

Instantly share code, notes, and snippets.

@breuerfelix
Created October 19, 2018 13:09
Show Gist options
  • Save breuerfelix/ae4e61998145c56cfc5376e4e9e8bf18 to your computer and use it in GitHub Desktop.
Save breuerfelix/ae4e61998145c56cfc5376e4e9e8bf18 to your computer and use it in GitHub Desktop.
login to the unofficial tribal wars api
import requests
import time
s = requests.Session()
username = 'scriptworld'
password = 'apitest123'
gameworld = 'en104'
s.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:63.0) Gecko/20100101 Firefox/63.0"
s.headers['Content-Type'] = 'application/x-www-form-urlencoded'
s.headers['X-Requested-With'] = 'XMLHttpRequest'
# get page cookies
url = 'https://www.tribalwars.net/en-dk/'
res = s.get(url)
# login to account
authurl = 'https://www.tribalwars.net/en-dk/page/auth'
data = f"username={username}&password={password}&remember=1"
res = s.post(authurl, data=data, allow_redirects=False)
# login to gameworld
url_gameworld = f"https://www.tribalwars.net/en-dk/page/play/{gameworld}"
res = s.get(url_gameworld)
url_token = res.json()['uri']
# get token
res = s.get(url_token)
# set tribal wars header
s.headers['TribalWars-Ajax'] = '1'
# logged into game
# get gameworld data
now = int(time.time())
url = f"https://{gameworld}.tribalwars.net/game.php?village=5854&screen=api&ajax=resources_schedule&id=5854&client_time={now}"
res = s.get(url)
game_data = res.json()
# get h token
h = game_data['game_data']['csrf']
# example upgrade farm
upgrade_url = f"https://{gameworld}.tribalwars.net/game.php?village=5854&screen=main&ajaxaction=upgrade_building&type=main&h={h}&&client_time={int(time.time())}"
body = 'id=farm&force=1&destroy=0&source=5854'
res = s.post(upgrade_url, data=body, allow_redirects=False)
print(res.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment