Last active
August 29, 2015 14:24
-
-
Save NixonInnes/0581aa33759d6b225f93 to your computer and use it in GitHub Desktop.
GW2 API Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Jul 3 11:09:36 2015 | |
@author: James.Innes | |
""" | |
import requests | |
import json | |
item_end = 'http://api.guildwars2.com/v2/items' | |
market_end = 'http://api.guildwars2.com/v2/commerce/listings' | |
def get_ids(): | |
return(json.loads(requests.get(item_end).text)) | |
def get_ids_market(): | |
return(json.loads(requests.get(market_end).text)) | |
def get_item(id_num): | |
return(json.loads(requests.get(item_end, params='id='+str(id_num)).text)) | |
def get_item_price(id_num): | |
return(json.loads(requests.get(market_end, params='id='+str(id_num)).text)) | |
def to_coinage(value): | |
c = int(value%100) | |
s = int((((value)%1000)-c)/100) | |
g = int((value-s-c)/1000) | |
return('{}g {}s {}c'.format(g,s,c)) | |
#item_ids = get_ids_market() | |
# | |
#for item in item_ids[:10]: | |
# i = get_item(item) | |
# print('Name: {}'.format(i['name'])) | |
# i_price = get_item_price(item) | |
# if 'buys' in i_price: | |
# print('Buy: {}'.format(to_coinage(i_price['buys']['unit_price']))) | |
# print('Sell: {}'.format(to_coinage(i_price['sells']['unit_price']))) | |
# else: | |
# print('Not marketable') | |
def lookup_item(id_num): | |
id_num = str(id_num) | |
item = get_item(id_num) | |
item_price = {'market':get_item_price(id_num)} | |
item.update(item_price) | |
return item | |
class item: | |
def __init__(self, id_num): | |
self.id = id_num | |
self.item_end = 'http://api.guildwars2.com/v2/items' | |
self.market_end = 'http://api.guildwars2.com/v2/commerce/listings' | |
try: | |
requests.get('http://api.guildwars2.com/v2/commerce', params='id='+str(self.id)).raise_for_status() | |
except requests.exceptions.HTTPError as e: | |
print(e) | |
self.attrib = json.loads(requests.get(self.item_end, params='id='+str(self.id)).text) | |
self.market = json.loads(requests.get(self.market_end, params='id='+str(self.id)).text) | |
def update_market(self): | |
self.market = json.loads(requests.get(self.market_end, params='id='+str(self.id)).text) | |
def sell_prices(self): | |
return(self.market['sells'][:5]) | |
def buy_prices(self): | |
return(self.market['buys'][:5]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment