mirror of
https://codeberg.org/angestoepselt/homepage.git
synced 2025-05-24 14:46:16 +00:00
Add form export script
This commit is contained in:
parent
befbee9845
commit
5a983f384f
2 changed files with 82 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -19,4 +19,6 @@ _site/
|
||||||
|
|
||||||
# Private environments in the HTTP playground folder
|
# Private environments in the HTTP playground folder
|
||||||
/playground/*.private.env.json
|
/playground/*.private.env.json
|
||||||
|
|
||||||
|
coderdojo.csv
|
||||||
/httpd.dev.conf
|
/httpd.dev.conf
|
||||||
|
|
|
||||||
80
playground/coderdojo.py
Normal file
80
playground/coderdojo.py
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import csv
|
||||||
|
import getpass
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
BASE_URL = "https://ticket.z31.it/api/v1"
|
||||||
|
|
||||||
|
session = requests.Session()
|
||||||
|
session.headers.update({
|
||||||
|
"Authorization": f"Token token={getpass.getpass(prompt='Zammad-Token eingeben: ')}"
|
||||||
|
})
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ticket_ids = []
|
||||||
|
|
||||||
|
print("Suche nach aktuellen CoderDojo-Anmeldungen…", file=sys.stderr)
|
||||||
|
has_more = True
|
||||||
|
page = 1
|
||||||
|
page_size = 50
|
||||||
|
while has_more:
|
||||||
|
response = session.get(
|
||||||
|
f"{BASE_URL}/tickets/search",
|
||||||
|
params={
|
||||||
|
"query": 'tags:"Kontaktformular – CoderDojo-Anmeldung"',
|
||||||
|
"page": str(page),
|
||||||
|
"per_page": page_size,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
ticket_dict = response.json().get("assets", {}).get("Ticket", {})
|
||||||
|
for ticket in ticket_dict.values():
|
||||||
|
if ticket["close_at"] is not None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
assert isinstance(ticket["title"], str)
|
||||||
|
if not ticket["title"].endswith(" – CoderDojo-Anmeldung"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
assert isinstance(ticket["article_ids"], list)
|
||||||
|
if len(ticket["article_ids"]) < 2:
|
||||||
|
continue
|
||||||
|
|
||||||
|
assert isinstance(ticket["id"], int)
|
||||||
|
ticket_ids.append(ticket["id"])
|
||||||
|
|
||||||
|
page += 1
|
||||||
|
has_more = len(ticket_dict) == page_size
|
||||||
|
|
||||||
|
print(f"{len(ticket_ids)} Anmeldung(en) gefunden. Hole jetzt die Details…", file=sys.stderr)
|
||||||
|
|
||||||
|
fieldnames = []
|
||||||
|
rows = []
|
||||||
|
for ticket_id in ticket_ids:
|
||||||
|
row = {}
|
||||||
|
|
||||||
|
response = session.get(f"{BASE_URL}/ticket_articles/by_ticket/{ticket_id}")
|
||||||
|
response.raise_for_status()
|
||||||
|
articles = response.json()
|
||||||
|
assert len(articles) >= 2
|
||||||
|
|
||||||
|
row.update({"Nachricht": articles[0].get("body", "")})
|
||||||
|
for line in articles[1].get("body", "").split("\n"):
|
||||||
|
key, value = line.split(": ", 1)
|
||||||
|
row.update({key: value})
|
||||||
|
if key not in fieldnames:
|
||||||
|
fieldnames.append(key)
|
||||||
|
|
||||||
|
rows.append(row)
|
||||||
|
|
||||||
|
if "Nachricht" not in fieldnames:
|
||||||
|
fieldnames.append("Nachricht")
|
||||||
|
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
|
||||||
|
writer.writeheader()
|
||||||
|
for row in rows:
|
||||||
|
writer.writerow(row)
|
||||||
|
sys.stdout.flush()
|
||||||
Loading…
Add table
Reference in a new issue