Commit 22d60399 authored by Paco Pompeani's avatar Paco Pompeani
Browse files

Modification de `send_mass_messages` pour qu'il n'envoie le message qu'aux users actifves

parent b591a4a6
......@@ -5,64 +5,60 @@ 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():
def get_auth(token: str):
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
def last_activity_recent_enough(sessions: str, oldTime: str) -> bool:
for session in sessions:
if oldTime < int(session["last_activity_at"]):
return True
me = requests.post(
f"{baseUrl}users/usernames", json=[username], headers=get_auth()
).json()[0]
def list_active_users(oldTimeHuman: str, username: str, token: str, baseUrl: str):
all_users = []
users = True
i = 0
print("Retrieving all users...", file=sys.stderr)
while users:
users = requests.get(
f"{baseUrl}users",
params={"per_page": "1000", "page": str(i), "active": "true"},#, "sort": "last_activity_at"},
headers=get_auth(token),
).json()
print(f"{i}: {len(users)}")
all_users.extend(users)
i = i + 1
print("sessions")
active_users = []
print(f"{len(all_users)} users.", file=sys.stderr)
print("Filtering users on sessions...", file=sys.stderr)
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()
for i in range(len(all_users)):
user = all_users[i]
if i % 100 == 0:
print(i)
sessions = requests.get(
f"{baseUrl}users/{user['id']}/sessions", headers=get_auth(token)
).json()
last_activity_at = 0
if last_activity_recent_enough(sessions, oldTime):
active_users.append(user)
for session in sessions:
if last_activity_at < int(session["last_activity_at"]):
last_activity_at = int(session["last_activity_at"])
return active_users
if last_activity_at > oldTime:
active_users.append(user)
if __name__ == "__main__":
username = "ppom"
token = "ch1ew66o8pnsxyzepo4opae8oh"
# 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/"
print()
print('active_users:', len(active_users))
print('all_users:', len(all_users))
print('active_users:', [ u["username"] for u in active_users ])
active_users = list_active_users(oldTimeHuman, username, token, baseUrl)
print()
print(len(active_users), 'active_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")
with open(f"active_since_{oldTimeHuman}.txt", "w+") as f:
for u in active_users:
f.write(u["username"]+"\n")
......@@ -4,42 +4,24 @@ import requests
import time
import datetime
from list_active_users import list_active_users
message = ""
username = ""
oldTime = (
time.mktime(datetime.datetime.strptime("01/08/2018", "%d/%m/%Y").timetuple()) * 1000
)
token = "" # À générer dans son "Profil"
oldTimeHuman = "10-05-2022"
baseUrl = "https://team.picasoft.net/api/v4/"
def get_auth():
return {"Authorization": f"Bearer {token}"}
r = requests.post(f"{baseUrl}users/login", json={"login_id": username, "password": ""})
token = r.headers["Token"]
all_users = []
i = 0
while True:
users = requests.get(
f"{baseUrl}users",
params={"per_page": "200", "page": str(i)},
headers=get_auth(),
).json()
if users == []:
break
for user in users:
all_users.append(user)
i = i + 1
me = requests.post(
f"{baseUrl}users/usernames", json=[username], headers=get_auth()
).json()[0]
for user in all_users:
active_users = list_active_users(oldTimeHuman, username, token, baseUrl)
for user in active_users:
sessions = requests.get(
f"{baseUrl}users/{user['id']}/sessions", headers=get_auth()
).json()
......
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