Last active
June 30, 2020 12:54
-
-
Save Robotto/600941f3da2931122102d64c55edd3a2 to your computer and use it in GitHub Desktop.
AGC klasseliste til adobe admin console kompatibel CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
from bs4 import BeautifulSoup | |
#Dette script indeholder ikke hemmelige eller personfølsomme oplysninger, men behandler manuelt anskaffede personoplysninger. | |
#Programmet æder klasselister (uden billeder) genereret via aarhus tech selvbetjeningen. | |
#Gem den genererede side som PrintPage.html i samme mappe som dette script. | |
inputFilename = "PrintPage.html" | |
outputFilename = "_klasseliste_til_adobe_admin_console_upload.csv" | |
if len(sys.argv) > 1: | |
inputFilename = int(sys.argv[1]) | |
print("Loading file:",inputFilename) | |
def dumpCSV(formattedAsCSV): | |
with open(outputFilename, 'w', newline="", encoding="utf-8") as file: | |
wr = csv.writer(file) | |
wr.writerows(formattedAsCSV) | |
def loadHTML(): | |
file = open(inputFilename, 'r', encoding="utf-8").read() | |
soup = BeautifulSoup(file, 'html.parser') | |
#find klasssenavn: | |
klasseTabel = soup.find('table') | |
klasseRows = klasseTabel.find_all('tr') | |
klassseBetegnelse = '' | |
for row in klasseRows: | |
cols = row.find_all('td') | |
if "Betegnelse" in str(cols[0]): | |
klassseBetegnelse = cols[1].text.strip() | |
#find klassedeltagere | |
data = [] | |
table = soup.findAll('table')[1] | |
rows = table.find_all('tr') | |
for row in rows: | |
cols = row.find_all('td') | |
cols = [ele.text.strip() for ele in cols] | |
data.append([ele for ele in cols if ele]) # Get rid of empty values | |
return data, klassseBetegnelse | |
def format(data,groupName): | |
count = 1 | |
formattedData=[] | |
formattedData.append(['Identity Type', 'Username', 'Domain', 'Email', 'First Name', 'Last Name','User Groups']) | |
while count < len(data): # skip first line | |
domain = 'edu.aarhustech.dk' | |
email = str(data[count][1]) + '@' + domain | |
firstNames = '' | |
for name in data[count][2].split(' ')[:-1]: # get everything from list except last name | |
firstNames += str(name) + ' ' | |
firstNames = firstNames[:-1] #remove trailing space | |
lastName = str(data[count][2].split(' ')[-1]) | |
formattedData.append(['Adobe ID', email, domain, email, firstNames, lastName,groupName]) | |
count += 1 | |
return formattedData | |
raw, klassenavn = loadHTML() | |
outputFilename = klassenavn+outputFilename | |
print("Klasseliste loaded for:",klassenavn) | |
adobeGroupName = 'Aarhus Gymnasium - ' + klassenavn | |
print("You should manually add the user group", "\""+adobeGroupName+"\",", "before attempting to upload the CSV to the adobe admin bulk upload corporate buzzword thingie") | |
formatted = format(raw,adobeGroupName) | |
print("Creating file:",outputFilename) | |
dumpCSV(formatted) | |
print("Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment