#
# wow_achievatron.py
#
# Reads achievement information of a World of Warcraft character through the
# Armory API.
#

import datetime
from src.modules import armory

def main():
	region = 'Europe'
	realm_name = 'Rexxar'
	char_name = 'Goraso'
	jdata = load_achieves_completed(region, realm_name, char_name)
	print_achieves_completed(jdata)

def load_achieves_completed(region, realm_name, char_name):
	""" Load Achievements completed by a given character and returns them in a
	Json object. Example URL:
	http://eu.battle.net/api/wow/character/Rexxar/Goraso?fields=achievements
	"""
	c = armory.connection(region)
	c.read_achievements_completed(realm_name, char_name)
	return c.jdata

def print_achieves_completed(jdata):
	""" Receives a Json object with a list of completed achievements by a
	specific character and prints it to the console.
	"""
	ac_completed = jdata['achievements']['achievementsCompleted']
	ac_completed_ts = jdata['achievements']['achievementsCompletedTimestamp']
	count = len(ac_completed)
	for i in range(0, count):
		ac_id = ac_completed[i]
		ac_ts = ac_completed_ts[i]
		ac_date = datetime.datetime.fromtimestamp(int(ac_ts) / 1000)
		print str(ac_id) + ' -- ' + str(ac_date)

if __name__ == "__main__":
	main()
