-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgw2api.py
161 lines (120 loc) · 4.8 KB
/
gw2api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# gw2api - Python wrapper for Guild Wars 2 official API.
# Copyright (C) 2016 Exceen
#
# Endpoints:
# https://wiki.guildwars2.com/wiki/API:Main#Version_2_endpoints
# https://wiki.guildwars2.com/wiki/API:2
#
# API explorer
# https://keeky.github.io/Guild-Wars-2-API-Explorer/#v2/
import os
# Import proper urllib and JSON library
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse
import requests
import json
try:
import json
except ImportError:
import simplejson as json
api_key = open('/'.join(os.path.dirname(os.path.realpath(__file__)).split('/')[:-1]) + '/gw2apikey', 'r').readlines()[0]
def get_items():
# Get a list of all item ids.
return _request('items')
def get_item_details(id):
# Get details of specific item.
return _request('items', id=id)
def get_delivery(api_key=api_key):
return _request('commerce/delivery', access_token=api_key)
def get_characters(api_key=api_key):
return _request('characters', access_token=api_key)
def get_character_details(id, api_key=api_key):
return _request('characters/' + urllib.parse.quote(str(id)), access_token=api_key)
def get_listings(id):
return _request('commerce/listings', id=id)
def get_prices(id):
return _request('commerce/prices', id=id)
# def get_transactions(id):
# return _request('commerce/transactions', id=id)
def get_current_transactions_buys(api_key=api_key):
return _request('commerce/transactions/current/buys', access_token=api_key)
def get_current_transactions_sells(api_key=api_key):
return _request('commerce/transactions/current/sells', access_token=api_key)
def get_recipe_by_output(id):
# Get a list of the search results
return _request('recipes/search', output=id)
def get_recipes():
# Get a list of all recipes.
return _request('recipes')
def get_recipe_details(id):
# Get details of specific item.
return _request('recipes/' + str(id))
def get_wvw_matches():
# Get the current running WvW matches.
return _request('wvw/matches')
def get_wvw_match_details(id):
# Get the current match details.
return _request('wvw/match_details', match_id=id)
def get_wvw_objective_names(lang='en'):
# Get the names of all objectives in WvW maps.
return _request('wvw/objective_names', lang=lang)
def get_event_names(lang='en'):
# Get names of all existing events.
return _request('event_names', lang=lang)
def get_map_names(lang='en'):
# Get names of all maps.
return _request('map_names', lang=lang)
def get_world_names(lang='en'):
# Get names of all world servers.
return _request('world_names', lang=lang)
def get_events(**args):
# Get list events based on filtering by world, map and event.
return _request('events', **args)
def get_quaggans(**args):
# Get list of all quaggans.
return _request('quaggans', **args)
def _request(json_location, **args):
# Makes a request on the Guild Wars 2 API.
headers = {}
if args is not None and 'access_token' in args:
headers['Authorization'] = 'Bearer ' + args['access_token'].replace('\n', '')
del args['access_token']
url = 'https://api.guildwars2.com/v2/' + json_location
if len(args) > 0:
url += '?' + '&'.join(str(argument) + '=' + str(value) for argument, value in list(args.items()))
response = requests.get(url, headers=headers)
response = response.text
#response = response.replace(': true', ': True')
#response = response.replace(': false', ': False')
#response = response.replace('null', 'None')
return json.loads(response)
#exec('data = %s' % response, globals(), locals())
# data = response
# return data
# def find_item_id_by_name(queue):
# # url = 'http://gw2tno.com/api/finditemidbyname/' + urllib.quote(queue)
# data = {}
# results = []
# page = 1
# last_page = 1
# while page <= last_page:
# url = 'http://www.gw2spidy.com/api/v0.9/json/item-search/' + urllib.quote(queue) + '/'
# url = url + str(page)
# response = urllib2.urlopen(url).read()
# if response == 'null':
# return []
# exec 'data.update(%s)' % response in globals(), locals()
# if type(data) == dict:
# results.extend(data['results'])
# last_page = data['last_page']
# page += 1
# return results
def find_item_id_by_name(queue):
results = []
url = 'http://api.gw2tp.com/1/bulk/items.csv'
response = requests.get(url, headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36'})
csv = response.text.split('\n')
for line in csv:
if '"' + queue.lower() in line.lower():
itemid = int(line.split(',')[0])
results.append({'data_id': itemid})
return results