summaryrefslogtreecommitdiff
path: root/org.py
blob: dde53ae09d749727645222a1db4607f914d6bab5 (plain)
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
import locale
import os
from datetime import datetime

from orgparse import loads

from nextcloud import NextCloudConnection
from settings import ORG_CAPTURE_FILENAME, ORG_LINKS_FILENAME, ORG_PLAN_FILENAME

locale.setlocale(locale.LC_ALL, "es_ES.utf8")


class OrgData(NextCloudConnection):
    def _generate_today(self):
        today = datetime.today()
        today_ymd = today.strftime("%Y-%m-%d")
        today_day = today.strftime("%a").lower()
        today_hour = today.strftime("%H:%M")
        return f"{today_ymd} {today_day} {today_hour}"

    def add_new_todo(self, keyword: str, description: str, outcome: str, extra: str) -> None:

        self.client.download_file(ORG_CAPTURE_FILENAME, "./capture.org")

        today = self._generate_today()

        todo_template = f"""
** {keyword.upper()} {description} :NEW::BOT:
  Desired outcome: {outcome}
  :LOGBOOK:
  - Added: [{today}]
  :END:

  {extra}

"""

        with open("./capture.org", "a") as capture_file:
            capture_file.write(todo_template)

        self.client.upload_file("./capture.org", ORG_CAPTURE_FILENAME, overwrite=True)

        os.remove("./capture.org")

    def list_plan(self, filename: str) -> str:
        with self.client.open(ORG_PLAN_FILENAME.replace("{filename}", filename), mode="r") as agenda:
            plan = agenda.read()

        plan = loads(plan)

        return plan[-1].get_body().replace("[X]", "✅").replace("[ ]", "❌")

    def add_new_link(self, link: str) -> None:

        self.client.download_file(ORG_LINKS_FILENAME, "./links.org")

        with open("./links.org", "a") as capture_file:
            capture_file.write(link)

        self.client.upload_file("./links.org", ORG_LINKS_FILENAME, overwrite=True)

        os.remove("./links.org")