-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaditlater-report.py
executable file
·68 lines (51 loc) · 2.2 KB
/
readitlater-report.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vi: set ft=python :
import os
from pathlib import Path
import argparse
from datetime import datetime, timedelta
from orgparse import load
def get_monday_of_week(week: int, year: int) -> datetime:
monday = datetime.fromisocalendar(year, week, 1) # 1 = Monday
return monday.replace(hour=0, minute=0, second=0)
def get_sunday_from_monday(date: datetime) -> datetime:
sunday = date + timedelta(days=6)
return sunday.replace(hour=23, minute=59, second=59)
def get_monday_from_date_in_same_week(date: datetime) -> datetime:
""" Given a date, return the Monday date of the same week"""
monday = date - timedelta(days=date.weekday())
return monday.replace(hour=0, minute=0, second=0)
def get_sunday_from_date_in_same_week(date: datetime) -> datetime:
""" Given a date, return the Sunday date of the same week"""
monday = get_monday_from_date_in_same_week(date)
sunday = monday + timedelta(days=6)
return sunday.replace(hour=23, minute=59, second=59)
def main(week: int, year: int, input_file:Path):
# today = datetime.today()
monday = get_monday_of_week(week, year)
sunday = get_sunday_from_monday(monday)
print(f"## Readitlater from {monday.strftime("%Y-%m-%d")} to {sunday.strftime("%Y-%m-%d")}\n")
if not input_file.exists():
print(f"could not find {input_file}")
return
root = load(input_file.resolve())
for node in root[1:]:
if node.todo != "DONE":
continue
closed = node.closed.start
if monday <= closed <= sunday:
headline = f"* {node.heading} - {node.tags}"
print(headline)
if __name__ == "__main__":
today = datetime.today()
today_week = today.isocalendar()[1]
parser = argparse.ArgumentParser()
parser.add_argument("week", nargs="?", help="Week number", type=int, default=today_week)
parser.add_argument("year", nargs="?", help="Year number", type=int, default=today.year)
args = parser.parse_args()
ME = os.getenv("ME")
if not ME:
ME = os.path.expanduser(os.path.join("~", "Me"))
input_file = Path(os.path.join(ME, "Orgmode", "ReadItLater.org"))
main(args.week, args.year, input_file)