54 lines
1.8 KiB
Python
Executable file
54 lines
1.8 KiB
Python
Executable file
#!/usr/bin/python3
|
|
|
|
import argparse
|
|
from datetime import date, timedelta, datetime
|
|
from wcmatch.pathlib import Path
|
|
|
|
|
|
def get_week_start_end(year, week):
|
|
# create a date object for the first day of the year
|
|
first_day = date(year, 1, 1)
|
|
# get the weekday of the first day (0 for Monday, 6 for Sunday)
|
|
first_day_weekday = first_day.weekday()
|
|
# calculate the offset to get the first Monday of the year
|
|
offset = timedelta(days=(7 - first_day_weekday) % 7)
|
|
# add the offset and the number of weeks to get the first day of
|
|
# the given week
|
|
week_start = first_day + offset + timedelta(weeks=(week - 1))
|
|
# add six days to get the last day of the given week
|
|
week_end = week_start + timedelta(days=6)
|
|
# return the start and end dates as strings in yyyy-mm-dd format
|
|
return week_start.strftime('%Y-%m-%d'), week_end.strftime('%Y-%m-%d')
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--base-dir",
|
|
help="base directory to move the images to",
|
|
default=Path.cwd())
|
|
parser.add_argument("path", help="Path containing images in PNG format")
|
|
options = parser.parse_args()
|
|
|
|
data_path = Path(options.path)
|
|
images = data_path.glob(['*.png', '*.webp'])
|
|
|
|
base_dir = Path(options.base_dir)
|
|
|
|
for image in images:
|
|
data = datetime.fromtimestamp(image.stat().st_mtime)
|
|
current_cal = data.isocalendar()
|
|
base_path = (
|
|
base_dir /
|
|
str(data.year) /
|
|
data.strftime("%m") /
|
|
"-".join(get_week_start_end(data.year, current_cal.week))
|
|
)
|
|
base_path.mkdir(exist_ok=True)
|
|
path = base_path / image.name
|
|
print(f"{image} -> {path}")
|
|
image.rename(path)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|