75 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import csv
import os
import requests
import re
URL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vR_SfJ7xUN3pcnRw3Z5b0DeINfFmZvWvquoJuBNS0UiA4h2xeftVlOml2BI162JiarV0XyvIOxaEJFQ/pub?gid=0&single=true&output=csv"
def download(url):
response = requests.get(url)
content = response.content.decode("utf-8")
return content
def download_csv(url):
"""
Downloads the CSV file at the given url and parses it.
Treats the first row as a header, and returns a list of dictionaries
built from subsequent rows, with the header row's fields as keys.
"""
response = download(url)
reader = csv.reader(response.splitlines())
data = list(reader)
header = data[0]
data = [dict(zip(header, row)) for row in data[1:]]
return header, data
def main():
header, data = download_csv(URL)
active = 0
inactive = 0
invalid = []
for row in data:
reason = []
if "@" not in row["email"] or "." not in row["email"].split("@")[1]:
reason.append(f"Invalid email address '{row['email']}'")
if row["role"] not in ["admin", "user"]:
reason.append(f"Invalid role '{row['role']}'")
if row["status"] not in ["active", "inactive"]:
reason.append(f"Invalid status '{row['status']}'")
elif row["status"] == "active":
active += 1
else:
inactive += 1
if not re.search(r"^\d\d\d\d(-\d\d){2}", row["created_on"]):
reason.append(f"Invalid creation date '{row['created_on']}'")
if len(reason) > 0:
invalid.append({"entry": row, "reason": reason})
print(f"Active users: {active}")
print(f"Inactive users: {inactive}")
if len(invalid) > 0:
print(f"{len(invalid)} invalid entries found")
for row in invalid:
print(f"\t User: {row['entry']['username']}")
print(f"\t Reasons:")
for reason in row["reason"]:
print(f"\t\t{reason}")
if __name__ == "__main__":
main()