-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovePDF.py
67 lines (60 loc) · 2.33 KB
/
movePDF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
movePDF
-------
This script moves PDFs from the Downloads folder to the specified contest folder.
You can ignore this file if you're here for answers of the problems.
"""
import os
import re
from glob import glob
from pathlib import Path
PDF_REGEX = re.compile(r"prob-([A-Z]+)\.pdf")
FOLDER_REGEX = re.compile(r"Problem ([A-Z]+): .+")
pdfs: dict[str, str] = {}
for pdf_path in glob(os.path.join(os.path.expanduser("~/Downloads"), "prob-*.pdf")):
pdfs[PDF_REGEX.match(os.path.basename(pdf_path)).group(1)] = pdf_path
if not pdfs:
print("No PDFs found in Downloads folder.")
exit(1)
contests = sorted([i for i in os.listdir() if "." not in i and os.path.isdir(Path.cwd() / i)])
print(f"Select a Contest (1-{len(contests)}): ")
for i, contest in enumerate(contests, start=1):
print(f"{i}. {contest}")
while True:
try:
selection = int(input("Enter the number of the contest: "))
except ValueError:
print(f"Please enter a valid number between 1 and {len(contests)}.")
else:
if not 1 <= selection <= len(contests):
print(f"Please select a number between 1 and {len(contests)}.")
continue
selected = contests[selection - 1]
confirmation = input(f"Move all PDFs to {selected} contest? (y/yes/n/no): ")
if confirmation.lower() in ("y", "yes"):
contest_folder = os.path.join(os.getcwd(), selected)
break
else:
if confirmation.lower() not in ("n", "no"):
print("I'll take that as a no.", end=" ")
print("Goodbye.")
exit(0)
for problem, pdf_file in pdfs.items():
destinations = sorted(
[i for i in os.listdir(contest_folder) if os.path.isdir(os.path.join(contest_folder, i))]
)
destination_file = None
for destination in destinations:
if FOLDER_REGEX.match(destination).group(1) == problem:
destination_file = os.path.join(
contest_folder, destination, os.path.basename(pdf_file)
)
break
if not destination_file:
print(f"Could not find a folder for problem {problem}.")
continue
try:
os.replace(pdf_file, destination_file)
print(f"Moved {os.path.basename(pdf_file)} to {destination}")
except Exception as e:
print(f"Error when moving {os.path.basename(pdf_file)}: {e}")