Skip to content

Instantly share code, notes, and snippets.

@z404
Last active September 1, 2022 14:08
Show Gist options
  • Save z404/c8f01eeb082a45faeb5c77066b3290b6 to your computer and use it in GitHub Desktop.
Save z404/c8f01eeb082a45faeb5c77066b3290b6 to your computer and use it in GitHub Desktop.
VTOP Coursepage File unzip and sorter
import zipfile
import tkinter as tk
from tkinter import filedialog
from pathlib import Path
import os
from datetime import timedelta, datetime
def unzip_file(path, folder_name):
with zipfile.ZipFile(path, 'r') as zip_ref:
zip_ref.extractall("{}".format(folder_name))
# CoursePage-Material_FALLSEM2022-23_CSE3001_ETH_VL2022230104121_16702_2022-08-31_10-34-57.zip
def convert_roman_to_int(roman):
roman = roman.upper()
roman_dict = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
int_value = 0
for i in range(len(roman)):
if i > 0 and roman_dict[roman[i]] > roman_dict[roman[i - 1]]:
int_value += roman_dict[roman[i]] - 2 * roman_dict[roman[i - 1]]
else:
int_value += roman_dict[roman[i]]
return int_value
def main():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
file_name = Path(file_path).name
if not file_path or "CoursePage-Material" not in file_name:
print("File chosen is not a CoursePage-Material file")
exit(0)
course_name = file_name.split("_")[1]+"_"+file_name.split("_")[2]
course_code = file_name.split("_")[2]
if file_path and file_path.endswith(".zip"):
unzip_file(file_path, course_name)
names_and_dates = {}
count_of_general_materials = 0
course_name_full = course_name
for i in os.listdir(course_name):
if i.startswith(course_code):
course_name_full = i.split("_")[0]+"_"+i.split("_")[1]
os.rename(course_name, course_name_full)
names_and_dates.update({i:datetime.strptime("2000-01-01", "%Y-%m-%d")})
os.rename(course_name_full+"/"+i, course_name_full+"/0) SYLLABUS.pdf")
elif "Reference_Material" in i:
name_of_file = " ".join(i.split("_")[8:])
os.rename(course_name_full+"/"+i, course_name_full+"/"+name_of_file)
date_of_file = datetime.strptime(i.split("_")[7], "%d-%m-%Y")
resource_number = i.split("_")[6]
for i in range(convert_roman_to_int(resource_number) - 1):
date_of_file+=timedelta(hours=1)
names_and_dates.update({name_of_file:date_of_file})
else:
name_of_file = " ".join(i.split("_")[4:])
os.rename(course_name_full+"/"+i, course_name_full+"/"+name_of_file)
date_of_file = datetime.strptime("01-01-2001", "%d-%m-%Y")
count_of_general_materials += 1
for i in range(count_of_general_materials):
date_of_file+=timedelta(hours=1)
names_and_dates.update({name_of_file:date_of_file})
list_of_names = list(names_and_dates.keys())
list_of_names.sort(key=lambda x: names_and_dates[x])
for i in range(1, len(list_of_names)):
os.rename(course_name_full+"/"+list_of_names[i], course_name_full+"/"+str(i)+") "+list_of_names[i])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment