1
\$\begingroup\$

OpenDota, A community driven platform for DotA 2 matches and players data is the best open-access API in gaming scene. I found no casual tutorial for using its API, So, I decided to make a one. Your feedback is welcome for inquiries or further improvements

Sincerely,

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Probably, You came along OpenDota's documention but did not know where to go from there.

As python is popular among data scientists, We shall use it in our tutorial. Python's standard library comes with URL handling module but for convenience I am going to use requests. If requests module is not already installed, obtain it via pip install requests. See the official installation guide for more details.

start by importing requests module by import requests

get a webpage from OpenDota's API. Its form is https://api.opendota.com/api/{entry1}/../{entry n}. For example, GET /players/{account_id}/matches is implemented as r = requests.get("https://api.opendota.com/api/players/107828036/matches")

Then, you could access the desired content by r.text. print(t.text) returns raw string. However, processing raw text is tedious and it would be more accessible if data where formed as dictionary. requests module comes with JSON parser out of the box by r.json(). Usually, r.json() is list, and r.json()[0] is the first element of the list which is a dictionary. You could print(r.json()) for manual check.

Finally, you might wish the write the modified data. Python's standard library comes with JSON writer out of the box. By the way, We could have parsed r.text to a dictionary using it instead of requests r.json(). Anyway, import json. Then

with open('<file-name>', 'w') as out:
                json.dump(<dictionary-object>, out, sort_keys=True, indent='\t')

where <dictionary-object> is r.json() in our case. both of sort_keys=True and indent='\t' beautify the writing by introducing tabs and newlines

Here is a sample code:

import requests
import json

r = requests.get("https://api.opendota.com/api/players/107828036/matches")

with open('output.json', 'w') as out:
                json.dump(r.json(), out, sort_keys=True, indent='\t')
\$\endgroup\$

You must log in to answer this question.