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
68
69
70
71
72
73
74
75
76
77
78
79
#!/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()