summaryrefslogtreecommitdiff
path: root/src/backend/getnews.py
blob: b2d29782802d6340b787dd21f1bb4521a997e401 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3

import os
import sisyphus.getclr
import sisyphus.getfs

NEWS_DIR = "news"

N_NEWS_FILE = os.path.join(sisyphus.getfs.p_cfg_dir,
                           os.path.join(NEWS_DIR, "n_news.txt"))
R_NEWS_FILE = os.path.join(sisyphus.getfs.s_cfg_dir,
                           os.path.join(NEWS_DIR, "r_news.txt"))
DELIMITER = "---"


def ld_n_news():
    with open(N_NEWS_FILE, "r") as file:
        content = file.read()
        articles = content.split(DELIMITER)
        return [article.strip() for article in articles if article.strip()]


def ld_r_news():
    try:
        with open(R_NEWS_FILE, "r") as file:
            return [int(line.strip()) for line in file.readlines()]
    except FileNotFoundError:
        return []


def save_r_news(r_news):
    with open(R_NEWS_FILE, "w") as file:
        for index in r_news:
            file.write(f"{index}\n")


def mark_read(article_nr):
    n_news = ld_n_news()

    if 1 <= article_nr <= len(n_news):
        article_index = article_nr - 1
        r_news_index = ld_r_news()

        if article_index in r_news_index:
            print(
                f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} is already marked as {sisyphus.getclr.green}read{sisyphus.getclr.reset}.")
        else:
            r_news_index.append(article_index)
            save_r_news(r_news_index)
            print(
                f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} marked as {sisyphus.getclr.green}read{sisyphus.getclr.reset}.")
    else:
        print(
            f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} doesn't exist.")


def mark_unread(article_nr):
    n_news = ld_n_news()

    if 1 <= article_nr <= len(n_news):
        article_index = article_nr - 1
        r_news_index = ld_r_news()

        if article_index not in r_news_index:
            print(
                f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} is already marked as {sisyphus.getclr.green}unread{sisyphus.getclr.reset}.")
        else:
            r_news_index.remove(article_index)
            save_r_news(r_news_index)
            print(
                f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} marked as {sisyphus.getclr.green}unread{sisyphus.getclr.reset}.")
    else:
        print(
            f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} doesn't exist.")


def list_all_news():
    n_news = ld_n_news()
    r_news_index = ld_r_news()

    for index, news in enumerate(n_news):
        status = "Read" if index in r_news_index else "Unread"
        print(
            f"\nArticle {sisyphus.getclr.green}{index + 1}{sisyphus.getclr.reset} ({status}):\n{news}")


def check_n_news():
    n_news = ld_n_news()
    r_news_index = ld_r_news()

    unread_count = len(n_news) - len(r_news_index)

    return unread_count


def start(check=False, list=False, read=False, unread=False, article_nr=None):
    if check:
        check_n_news()

    if list:
        list_all_news()

    if read:
        if article_nr is not None:
            mark_read(article_nr)
        else:
            print("\nError: No article number provided to mark as read.")

    if unread:
        if article_nr is not None:
            mark_unread(article_nr)
        else:
            print("\nError: No article number provided to mark as unread.")