我想做一個簡單的腳本來計算游戲中某些動作的數量。游戲有一個 api,每個請求限制為 100 個結果,這就是為什么如果你想更進一步并繼續回圈搜索,它們有 unix 時間戳,問題是我從結果中取出所有 100 個時間戳,將它們添加到一個列出并排序它們以獲取最后一個并在下一個請求中使用它,直到沒有更多結果,但使用下面的代碼 1- 代碼不退出 2- 除錯代碼后,代碼僅更改時間戳一次
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)
uj5u.com熱心網友回復:
問題已解決:服務器使用了 30 秒快取,所以無論我搜索什么查詢,我都會得到相同的結果,這似乎是他們服務器中的一個問題,他們說不應該這樣做,但我將延遲更改為 31 秒確保它有效
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351842.html
上一篇:找不到錯誤-異步函式-回傳太快
