Skip to content
TORNLIFE More

Tutorial on using TornAPI With Python

Started by kYo [2106640] on in Tutorials & Guides.

18 replies · 3.82k views · thread synced · 4 days ago · View on torn.com
About this thread

Posts archived: 19 / 19 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
19
Discussion span
→
People posting
8
Likes on archived posts
141
Authority score
55 / 100
Historical score
48 / 100
Story score
53 / 100
Engagement score
74 / 100

Mentioned in this thread

McNeo [864688] ×2 tos [1976582] ×2

kYo [2106640]
: : 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.

[image: i.imgur.com]
[image: i.imgur.com]
[image: i.imgur.com]
[image: i.imgur.com]

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!"
[image: i.imgur.com]
[image: i.imgur.com]

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.
[image: i.imgur.com]
[image: i.imgur.com]

for loop, while loop
[image: i.imgur.com]
[image: i.imgur.com]

: : 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.
[image: i.imgur.com]
[image: i.imgur.com]

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.
[image: i.imgur.com]
[image: i.imgur.com]

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!

Mentions: McNeo [864688] · tos [1976582] · Python w/Requests Install Guide · Understanding and Using the Torn API

kYo [2106640]
<Reserved>

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 from <censor> with the message: thanks for your help 12:21:10 25/06/20