Tutorial on using TornAPI With Python (Part 2) | Tutorials & G…

Tutorial on using TornAPI With Python (Part 2)

​

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    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 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.)
    Last edited by kYo on 17:02:11 - 27/05/20 (6 years ago)

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 16:53:05 - 27/05/20 (6 years ago)
    Post link copied to clipboard Copy post link
    Link 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)

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    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)

    Animated GIF

    • Triplicant [2037705]
    • Role: Civilian
    • Level: 92
    • Posts: 10,487
    • Karma: 24,632
    • Last Action: 4 years
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 00:11:49 - 05/06/20 (6 years ago)
    Post link copied to clipboard Copy post link
    Just finished following your instructions here, except for the Pillow bit at the end.

    Absolutely marvellous <3

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 02:50:55 - 05/06/20 (6 years ago)
    Post link copied to clipboard Copy post link
    Thanks Trips!

    You managed to get this?

    Last edited by kYo on 03:39:37 - 06/06/20 (6 years ago)

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 03:42:02 - 15/07/20 (6 years ago)
    Post link copied to clipboard Copy post link
    bump

    Animated GIF

    • Svalbard [733499]
    • Role: Civilian
    • Level: 91
    • Posts: 501
    • Karma: 421
    • Last Action: 3 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 10:26:04 - 15/07/20 (6 years ago)
    Post link copied to clipboard Copy post link
    Absolutely brilliant! R+

    INVICTUS

     

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 17:35:12 - 09/08/20 (6 years ago)
    Post link copied to clipboard Copy post link
    bump

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 14:22:09 - 17/10/20 (6 years ago)
    Post link copied to clipboard Copy post link
    bump

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 03:41:42 - 08/02/21 (5 years ago)
    Post link copied to clipboard Copy post link
    bump

    Animated GIF

    • -Kryptonian [2361119]
    • Role: Civilian
    • Level: 97
    • Posts: 485
    • Karma: 102
    • Last Action: 1 day
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 07:59:32 - 08/02/21 (5 years ago)
    Post link copied to clipboard Copy post link
    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.
    Last edited by -Kryptonian on 13:37:02 - 08/02/21 (5 years ago)
    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 14:39:47 - 21/06/21 (5 years ago)
    Post link copied to clipboard Copy post link
    b

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 14:55:37 - 18/08/21 (5 years ago)
    Post link copied to clipboard Copy post link
    b

    Animated GIF

    • Rogue [1873490]
    • Role: Civilian
    • Level: 100
    • Posts: 2,819
    • Karma: 6,480
    • Last Action: 11 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 15:43:01 - 19/08/21 (5 years ago)
    Post link copied to clipboard Copy post link
    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.
    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 20:23:12 - 19/08/21 (5 years ago)
    Post link copied to clipboard Copy post link

    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.
    no, you're my altered ego. XD

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 13:14:18 - 21/10/21 (5 years ago)
    Post link copied to clipboard Copy post link
    b

    Animated GIF

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 15:17:07 - 21/03/22 (4 years ago)
    Post link copied to clipboard Copy post link
    b

    Animated GIF

    • dogeboom [3308074]
    • Role: Civilian
    • Level: 38
    • Posts: 20
    • Karma: 10
    • Last Action: 1 month
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    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?

    • ChatGPT [1762864]
    • Role: Civilian
    • Level: 100
    • Posts: 1,493
    • Karma: 791
    • Last Action: 50 minutes
      • 1
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 23:52:08 - 25/04/25 (1 year ago)
    Post link copied to clipboard Copy post link

    dogeboom [3308074]

    this still valid in 2025 or were there major updates to API?

    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/

    37532402-0d99-496a-a39b-266b87dd8a70-1762864.png

    • kYo [2106640]
    • Role: Civilian
    • Level: 100
    • Posts: 2,876
    • Karma: 2,717
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 14:15:44 - 26/04/25 (1 year ago)
    Post link copied to clipboard Copy post link

    dogeboom [3308074]

    this still valid in 2025 or were there major updates to API?

    This was written on API 1.0 basis, I've not been playing the API for some time due to work and stuff.

    Animated GIF

Reply
Thread Title: