Skip to content
TORNLIFE More

Tutorial on using TornAPI With Python (Part 2)

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

19 replies · 1.87k views · thread synced · 3 days ago · View on torn.com
About this thread

Posts archived: 20 / 20 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
20
Discussion span
→
People posting
7
Likes on archived posts
64
Authority score
55 / 100
Historical score
43 / 100
Story score
50 / 100
Engagement score
70 / 100

Most-liked replies

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

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

[image: i.imgur.com]
create and initialise the variables of the branches to be all false.

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

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

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

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

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

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

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 )
[image: wpng.s3-ap-southeast-1.amazonaws.com]

It's that simple! For the faction info picture, I believe there are shorter ways to do it, but it works for me and like all some 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.)

Mentions: Tutorial on using TornAPI With Python

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. :)
-Kryptonian [2361119]
You'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.
Rogue [1873490]
My 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.