Tutorial on using TornAPI With Python
-
Thread created on 07:18:25 - 26/05/20 (6 years ago)|Last replied 20:45:01 - 09/04/26 (6 months ago): : Chapter 0 : References : :
Usually people put their references underneath, but this guide is meant to provide information that other guides do not, so I shall not run through contents that others had already covered in this guide.
Understanding and Using the Torn API by McNeo [864688]
Read this to understand what TornAPI is and what it does. However, this guide does not cover the usage.
IMPORTANT: Read this if you totally have no idea what is TornAPI.
Python w/Requests Install Guide by tos [1976582]
Follow this guide to install python, unfortunately, some of the scripting content might be replicated.
IMPORTANT: Follow this set of instruction to install and run Python. I did not read everything in the guide, if the guide does not work for you, let me know, i'll find another and replace this.
Introducing JSON
This gives an introduction to json and how it is represented. It is a data-oriented data-interchange format and it is completely language independent. You can adapt some of the things covered below and use them for other languages (i.e. javascript, Excel vba)
: : Chapter 1 : API Basic : :
go to https://www.torn.com/api.html# and put in your API key. (This is legally torn's website)
Expand under user, under selection put in "basic" and click on "Try it!"
You'll see your public data, these are public data. It shows your level, gender, id, name... etc
the following screenshots show what you can see, what you can't see.



I will need your API key to know your private information, but things that are not private, like your status, it is easily accessible via anyone's API key
: : Chapter 2 : Python Crash Course : :
Assuming you have python installed. Regardless of versions. here's some basic stuff to kickstart the journey. If you have learnt python before, ignore this chapter and move on to next.
referencing
Python Syntax
datatype
if-else statement
for loops
while loops
Printing "Hello world!"

variables, if-else and indentation
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.
In the following example, i used x as a variable, did a comparison with 5, prints the output using if-else.

for loop, while loop

: : Chapter 3 : JSON Requests : :
open an elevated command prompt, key in the following command and press enter. It installs the request module into your python, you'll need this to pull the API.
pip install request
While you're at it, install JSON module as well.
pip install json.
The following script pulls the data from API and prints them.

line 1, the program creates a variable call API_KEY and initialise it with your API Key. If your API Key is abcdefgh12345678, then API_KEY = "abcdefgh12345678".
line 2-3 imports the request module and JSON module
line 6 performs a request to the TornAPI URL using your API Key
Line 7 deserialise the string of JSON data into a dictionary
Line 9 prints the whole deserialised data with indentation of 2 spaces.
This is not useful if the data is in a large datatype call dictionary. You can read up on dictionary here
This is where the really cool stuff is.

Do note that the API key is initialized at line 1. It is hidden away for obvious reasons.
line 5 imports DateTime module which we need to use later.
line 7 performs a request on both cooldown data and time now data
line 12 puts the timestamp into the variable tEpochTimeNow
line 14 is not required, but it converts the time from epoch to DateTime format. Do note that torn uses epoch time, but since this is a torn tutorial, I shall say Epoch Time originates from Torn Time instead.
line 16-18 puts the cooldown in secs left to their respective variables.
line 20-30 manipulates the data and prints out the time to set your alarm to the end of the cooldown.
Note that my variables have t in front of it, it is just a programming habit I inherited (bad pun) as a software engineer a few years back. It stands for temporary variable, eventually, all my variables start with t, m, a, g, tp, ap, mp, gp, where it stands for temporary, module, argument, global. The p behinds the prefix stands for pointers.
This tutorial allows you to make simple stuff with information from the API, it's entirely up to you on how you want to manipulate your data into something useful!Last edited by kYo on 06:14:04 - 05/06/20 (6 years ago)
-
-
Posted on 07:21:31 - 26/05/20 (6 years ago)Post link copied to clipboard Copy post linkLink To Chapter 0, 1, 2 and 3
: : Chapter 0 : References : :
: : Chapter 1 : API Basic : :
: : Chapter 2 : Python Crash Course : :
: : Chapter 3 : JSON Requests : :
Link To Chapter 4 and 5
: : Chapter 4 : Faction Stuff (Checking inactivity with a script) : :
: : Chapter 5 : Faction Stuff (Creating faction info image) : :Last edited by kYo on 16:56:34 - 27/05/20 (6 years ago)
-
-
Posted on 07:21:43 - 26/05/20 (6 years ago)Post link copied to clipboard Copy post link
Donations are welcome. if anyone needs help with python or API, or have any project ideas, feel free to find me. >.<
Small projects are free, anything that takes more than 6 hours of my time (when I'm free) or 2 hours of my time (when i'm slightly busy) might cost a limb or two.
P.S. I don't do price list unless you guys wanna pay a large sum of money for it. It's tedious and there are a lot other coders doing the same thing. :)
You were sent $3,000,000 fromwith the message: thanks for your help 12:21:10 25/06/20 Last edited by kYo on 12:28:21 - 25/06/20 (6 years ago)
-
-
Posted on 07:52:55 - 26/05/20 (6 years ago)Post link copied to clipboard Copy post linkOh, this is amazing! Thank you thank you thank you!!

-
-
Posted on 14:01:16 - 27/05/20 (6 years ago)Post link copied to clipboard Copy post linkNice guide!
-
-
Posted on 03:41:22 - 15/07/20 (6 years ago)Post link copied to clipboard Copy post linkbump

-
-
-
-
Posted on 17:34:56 - 09/08/20 (6 years ago)Post link copied to clipboard Copy post linkbump

-
-
Posted on 14:21:52 - 17/10/20 (6 years ago)Post link copied to clipboard Copy post linkbump

-
-
Posted on 03:41:23 - 08/02/21 (5 years ago)Post link copied to clipboard Copy post linkbump

-
-
Posted on 14:15:10 - 06/03/21 (5 years ago)Post link copied to clipboard Copy post linkGreat guide!
-
-
Posted on 10:25:12 - 08/03/21 (5 years ago)Post link copied to clipboard Copy post linkAGH if only i found this guide a month ago i would have saved myself 2 days of being stupid. Nice guide R+

-
-
Posted on 13:02:37 - 08/03/21 (5 years ago)Post link copied to clipboard Copy post linkThank you~ <3

-
-
Posted on 14:39:58 - 21/06/21 (5 years ago)Post link copied to clipboard Copy post linkb

-
-
Posted on 14:55:26 - 18/08/21 (5 years ago)Post link copied to clipboard Copy post linkb

-
-
Posted on 13:14:30 - 21/10/21 (5 years ago)Post link copied to clipboard Copy post linkb

-
-
Posted on 15:16:48 - 21/03/22 (4 years ago)Post link copied to clipboard Copy post linkb

-
-
Posted on 20:57:08 - 08/04/22 (4 years ago)Post link copied to clipboard Copy post linkbump
-
-
Posted on 20:45:01 - 09/04/26 (6 months ago)Post link copied to clipboard Copy post link
Great Guide!
-







