#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "isoweek",
# ]
# ///
import os
import time
from isoweek import Week
import subprocess
import re
from datetime import date
def next_markdown_number(directory, starting=33):
"""
Counts the number of markdown files (*.md) in a directory where the filename starts with an integer.
Returns the next integer in sequence.
:param directory: Path to the directory to search in.
:return: Next integer in the sequence of filenames.
"""
# Regular expression to match files starting with an integer
pattern = re.compile(r"^(\d+).*\.md$")
# List to hold the integers found at the start of filenames
numbers = []
# Traverse the directory and match each file against the pattern
for file in os.listdir(directory):
match = pattern.match(file)
if match:
number = int(match.group(1))
numbers.append(number)
# If no matching files are found, start with 1
if not numbers:
return starting
# Otherwise, return the next number in the sequence
return max(numbers) + 1
def main():
thisweek = Week.thisweek()
first = thisweek.days()[0].strftime("%Y-%m-%d")
last = thisweek.days()[-1].strftime("%Y-%m-%d")
print(f"week from {first} to {last}")
# fn = f"{time.strftime('%Y')}-w{thisweek.week}"
# fn = next_markdown_number("content/weekly")
cal = date.today().isocalendar()
fn = f"{cal[0]}-{cal[1]}"
wf = f"content/journal/{fn}.md"
dt = time.strftime("%Y-%m-%d")
title = f"{time.strftime('%Y')}, week {thisweek.week:02}"
if not os.path.exists(wf):
with open(wf, "w") as f:
f.write(
f"""---
title: {title}
date: {dt}
draft: true
tags:
- weekly
---
Monday, {first} to Sunday, {last}
"""
)
subprocess.call(("open", f"obsidian:///Users/pradeep/quartz/{wf}"))
if __name__ == "__main__":
main()