Skip to content

Instantly share code, notes, and snippets.

@Bouni
Created October 26, 2023 15:13
Show Gist options
  • Save Bouni/df0f80a9375ab2b00e4411722f0eaded to your computer and use it in GitHub Desktop.
Save Bouni/df0f80a9375ab2b00e4411722f0eaded to your computer and use it in GitHub Desktop.
Hacky python script to get a list uf all Nextcloud users ordered by their last login (for use with docker-compose)
from subprocess import Popen, PIPE
from datetime import datetime as dt
import json
occ = ["docker-compose", "exec", "-u", "www-data", "app", "/var/www/html/occ"]
list_users_cmd = occ + ["user:list", "--output=json"]
with Popen(list_users_cmd, stdout=PIPE) as proc:
users = json.loads(proc.stdout.read().decode("UTF-8"))
data = []
for user, name in users.items():
last_seen_cmd = occ + ["user:lastseen", user]
with Popen(last_seen_cmd, stdout=PIPE) as proc:
last = proc.stdout.read().decode("UTF-8")
date = dt.strptime("01.01.1970", "%d.%m.%Y")
try:
_, date = last.split(":", 1)
date = date.strip()
date = dt.strptime(date, "%d.%m.%Y %H:%M")
except Exception as e:
pass
data.append({"name": name, "date": date})
data.sort(key=lambda x: x["date"], reverse=True)
for user in data:
print(f"{user['name']: <30}:{user['date']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment