简体   繁体   中英

Im trying to get data from an Api with 100 results limit but im getting an issue when using timestamps to go further

I wanted to do a simple script that calculates the number of certain actions in the game . The game has an api that is limited to 100 results per request, that is why they have unix timestamps if you want to go further and keep searching in a loop,the issue is that i take all 100 timestamps from the results add them to a list and sort them to take the last one and use it in the next request until there are no more results, but using the code below 1- The code doesn't exit 2- After debugging the code the code changes the timestamp only once

import requests
import json
import time


c = 0
link = "https://api.torn.com/user/"
key = "secret_api_key"
Monarch_Engineering = {
        "dummmyId":[0,0,0,0,0],# user id : success, fail, bad, good, number of revs payed
    }
Monarch = {
        "dummmyId":[0,0,0,0,0],# user id : success, fail, bad, good, number of revs payed
    }
non_contract = {
        "dummmyId":[0,0,0],# user id : success, fail, number of revs payed
    }

def search(frm,tt,ky=key):
    parameter = {

        "selections" : "revives",
        "key"        : ky,
        "from"       : frm,
        "to"         : tt
    }
    timestamp = True
    while True:
        with requests.get(link,params=parameter) as resp:
            respon = json.loads(resp.text)
        # Makes the get request and returns json
        # transforms json str to a dictionary
        if resp.status_code != 200 :
            print("Request Error : ",resp.status_code, "\nExiting ....")
        if "error" in respon.keys():
            print(respon['error']['error']+" Error Code : ", respon['error']['code'])
            q = input("Do you Want to continue Y/N : ").lower()
            if q != "y":
                print("script exiting ......")
                return
            # makes sure that it is a successful
            # wait for a user input here to confirm the issue also print the error code
        time.sleep(1)
        # to prevent getting banned 
        timestamp = bool(respon['revives'])
        if timestamp !=True :
            return
        print(parameter)# for debuging
        parameter['from'] = revive(respon)
        



def revive(respond):
    global c
    global Monarch_Engineering
    global Monarch
    global non_contract
    ff = [] # timestamps list
    if "revives" not in respond.keys():
        print("error no revs found ")
        return
    for key in respond["revives"]:
        c+=1
        tkey = respond['revives'][key]
        uid = tkey['target_id']
        ff.append(tkey['timestamp'])
        # if in ME
        if tkey['target_faction'] == 7835 :
            if uid not in Monarch_Engineering:
                Monarch_Engineering[uid]=[0,0,0,0,0]
            if "hospitalized" in tkey['target_hospital_reason'].lower() and tkey['chance'] >= 50:
                Monarch_Engineering[uid][3]+=1
                if tkey['result'].lower() == "success":
                    Monarch_Engineering[uid][0]+=1
                else:
                    Monarch_Engineering[uid][1]+=1
            else:
                Monarch_Engineering[uid][2]+=1


        elif tkey['target_faction'] == 8336 :
            if uid not in Monarch:
                Monarch[uid]=[0,0,0,0,0]
            if "hospitalized" in tkey['target_hospital_reason'].lower() and tkey['chance'] >= 40:
                Monarch[uid][3]+=1
                if tkey['result'].lower() == "success":
                    Monarch[uid][0]+=1
                else:
                    Monarch[uid][1]+=1
            else:
                Monarch[uid][2]+=1
        
        
        else:
            if uid not in non_contract:
                non_contract[uid]=[0,0,0]
            if tkey['result'].lower() == "success":
                non_contract[uid][0]+=1
            else:
                non_contract[uid][1]+=1
    ff.sort()
    return ff[-1]


search(1635163200, 1635768000)

for i in Monarch_Engineering:
    print("user id :", i)
    print("The number of Success :", Monarch_Engineering[i][0])
    print("The number of Failed  :", Monarch_Engineering[i][1])
    print("The number of Bad     :", Monarch_Engineering[i][2])
    print("The number of Good    :", Monarch_Engineering[i][3])
print("\nthe number of revs searched/done = ",c)
    
    

问题已解决:服务器使用了 30 秒缓存,所以无论我搜索什么查询,我都会得到相同的结果,这似乎是他们服务器中的一个问题,他们说不应该这样做,但我将延迟更改为 31 秒确保它有效

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM