Tutorial on using TornAPI With Python (Part 2)
-
Thread created on 16:50:35 - 27/05/20 (6 years ago)|Last replied 14:15:44 - 26/04/25 (1 year ago)This is a continuation of the Tutorial On Using TornAPI With Python. This is more faction oriented.
Note that for some of this part of the tutorial, you may need to have AA from your faction.
(P.S. Yes, I can steal from faction vault, or be faction vault thief. But my faction members give me TCD2 for every TCD1 everytime they want to withdraw. I'll get bountied and thrown out of faction if I become a faction vault thief, so nahhhh.)
: : Chapter 4 : Faction Stuff (Checking inactivity with a script) : :
Let's do the simple stuff, checking inactivity. Inactivity is something that you don't need AA to check, and with some understanding of the API, you can modify the following script to check inactivity for any faction.
Basically, the algorithm for this is to know their last login time (i.e. last_action), comparing it with the current timestamp. If it is over your threshold, then they'll be flagged up. It's up to the leader to consider them as inactive or on leave. (Which is why we have an inactivity thread to let leaders know that they're going to be away for an extended period of time.)
(P.S. the change in colour scheme is because I switched to Visual Studio Code instead of notepad++ as it's night time. )
The first part of the program does the one-time calculation of how many days in secs does the member need to be considered inactive. I did this to avoid hardcoding of days. Imagine one day, the leader says he wants 3 days, another day, the leader says he wants 1.5 days. All I need to do is to change NUMBER_OF_DAYS instead of calculating all over and over and over again. (P.S. I used 0.99 because it coincides with OC, if my leader wants anything more than 1 day, he can still read it for himself)
As always, you'll need to pull a request from the server. Then manipulate the data.
Next, you'll need to iterate through all the members dictionary using a for loop. A dictionary has 2 components, the Key and the Items. When the program iterate through the keys of the dictionary, the User ID is returned in the form of tMemberID. Then the program used tMemberID to access last_action in timestamp.
The program does simple maths to what's the time lapsed since the person last logged in in seconds. Using a If-Else Statement, it compares the difference of time between last_action and tTimeNow with TIME_TO_CONSIDER_INACTIVE. If his time lapsed meets the threshold, that would be inactive and his name will be printed out. the leader can't make sense of anything in seconds, so i converted them into hours for easier readability.
You'll notice that instead of explaining line by line what the program does, i'm basically describing the algorithm in blocks of code.
: : Chapter 5 : Faction Stuff (Creating faction info image) : :
Next, some of the faction leaders might be wondering how do people create faction info picture from API. This part of the API requires AA privileges. I am not sure if members without AA are able to access this.
As the program is too long, i will be skipping the part where I drew the faction info picture. But it is done with Python Pillow. You can read up yourself here.
As usual, you'll need to perform a request from Python API. The Request URL is "https://api.torn.com/faction/?selections=upgrades&key={YOUR_API_KEY}". This can be retrieve from Torn's API website
As I do not know the branches that are active, i have to find out the individual branches first.
create and initialise the variables of the branches to be all false.
This creates a function to check if any of the branches exists. On hindsight, I could use dictionary keys to find out, but it shall still be an iterative method as I will also need to find out which branches exists. This function takes in the UpgradeDictionary and IDKey as arguments. It returns if the ID is true. It uses try and except as I am testing every valid key.
These 8 for-loops calls the function, and update the variables of the branches. Note that I used a "or" here. If any of the upgrade is true for the branches, it shall remain true until end of the function.
In other words, the above code snippet can be represented in the following flow chart.
The text output will look something like this on your console, this part of the program kind of verify that you are reading in the correct API.
This is the coordinate system used by Python Pillow. It is similar to alot of cartesian coordinates like Paint to make it easier. Top Left is (0,0)
At this point in time, I can slowly cook up my faction picture. (Note that this portion of the code is not optimized as I've just started exploring python and TornAPI. In fact, this was the first program I wrote, not the program for checking of inactivity shown above.)
The offset coordinates are entirely dependent on you. I used the variables like iLeftAlign0 and iHeight to configure. Everytime i move down, the iHeight increases.
This is the result of the image drawn using Pillow.
Then beautify it a little, add background, add the little logo on top, add the respect. You could even add your own name (As long as your leader don't bounty me for teaching the wrong stuff. TBH, I don't care if your leader bounty you. XD )
It's that simple! For the faction info picture, I believe there are shorter ways to do it, but it works for me and likeallsome other programmer, don't break when it is working. (I KNOW THIS IS NOT WHAT I SHOULD DO OR PREACH BUT I'M NOT A SOFTWARE ENGINEER! There will always be a software engineer to clean my shit.)Last edited by kYo on 17:02:11 - 27/05/20 (6 years ago)
-
-
Posted on 16:53:05 - 27/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:58 - 27/05/20 (6 years ago)
-
-
Posted on 16:53:16 - 27/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. :)Last edited by kYo on 05:32:11 - 15/06/20 (6 years ago)
-
-
Posted on 00:11:49 - 05/06/20 (6 years ago)Post link copied to clipboard Copy post linkJust finished following your instructions here, except for the Pillow bit at the end.
Absolutely marvellous <3
-
-
Posted on 02:50:55 - 05/06/20 (6 years ago)Post link copied to clipboard Copy post linkThanks Trips!
You managed to get this?
Last edited by kYo on 03:39:37 - 06/06/20 (6 years ago)
-
-
Posted on 03:42:02 - 15/07/20 (6 years ago)Post link copied to clipboard Copy post linkbump

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

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

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

-
-
Posted on 07:59:32 - 08/02/21 (5 years ago)Post link copied to clipboard Copy post linkYou'll need to go one more level deep in the function, something like
data = r.json()
iUpgrades = data["upgrades"]
now your loops and function should work as is.Last edited by -Kryptonian on 13:37:02 - 08/02/21 (5 years ago) -
-
Posted on 14:39:47 - 21/06/21 (5 years ago)Post link copied to clipboard Copy post linkb

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

-
-
Posted on 15:43:01 - 19/08/21 (5 years ago)Post link copied to clipboard Copy post linkMy alter ego is a nerd. I had no idea!
Someday I will read this and get smarter, I've just been using Excel cause lazy. -
-
Posted on 20:23:12 - 19/08/21 (5 years ago)Post link copied to clipboard Copy post linkno, you're my altered ego. XD

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

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

-
-
Posted on 17:21:49 - 24/04/25 (1 year ago)Post link copied to clipboard Copy post link
this still valid in 2025 or were there major updates to API?
-
-
Posted on 23:52:08 - 25/04/25 (1 year ago)Post link copied to clipboard Copy post link
You can always try it and find out ;)
Since this guide, v2 has been released. You can find docs here: https://www.torn.com/swagger.php
Alternatively, you can still use the v1 (though there will have been some changes), for which you can find unofficial docs here: https://tornapi.tornplayground.eu/

-
-
Posted on 14:15:44 - 26/04/25 (1 year ago)Post link copied to clipboard Copy post link
This was written on API 1.0 basis, I've not been playing the API for some time due to work and stuff.

-






