Commit 087b2137 authored by Paco's avatar Paco
Browse files

New file to showcase how to get only active users

parent 703ef74d
#/usr/bin/env python
import sys
import time
import requests
import datetime
username = "ppom"
token = "à générer dans son 'Profil'"
# ex: 15-05-2022
oldTimeHuman = sys.argv[1]
oldTime = (
time.mktime(datetime.datetime.strptime(oldTimeHuman, "%d-%m-%Y").timetuple()) * 1000)
baseUrl = "https://team.picasoft.net/api/v4/"
def get_auth():
return {"Authorization": f"Bearer {token}"}
all_users = []
users = True
i = 0
while True:
users = requests.get(
f"{baseUrl}users",
params={"per_page": "200", "page": str(i), "active": "true"},#, "sort": "last_activity_at"},
headers=get_auth(),
).json()
print(f"{i}: {len(users)}")
all_users.extend(users)
i = i + 1
if not users:
break
# elif users[-1]["last_activity_at"] < oldTime:
# break
me = requests.post(
f"{baseUrl}users/usernames", json=[username], headers=get_auth()
).json()[0]
print("sessions")
active_users = []
for i in range(len(all_users)):
user = all_users[i]
if i % 10 == 0:
print(i)
sessions = requests.get(
f"{baseUrl}users/{user['id']}/sessions", headers=get_auth()
).json()
last_activity_at = 0
for session in sessions:
if last_activity_at < int(session["last_activity_at"]):
last_activity_at = int(session["last_activity_at"])
if last_activity_at > oldTime:
active_users.append(user)
print()
print('active_users:', len(active_users))
print('all_users:', len(all_users))
print('active_users:', [ u["username"] for u in active_users ])
with open(f"active_since_{oldTimeHuman}.txt", "w+") as f:
for u in active_users:
f.write(u["username"]+"\n")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment