summaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-04-09 14:21:50 +1000
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-04-09 14:21:50 +1000
commit5b221fb72d6b915a56df4c17ecc747bb6f15dee8 (patch)
treef1199057ffa9cdffca2a69f9ef2246d185894b06 /src/services
parentweather: store api key directly in config (diff)
downloadcaelestia-shell-5b221fb72d6b915a56df4c17ecc747bb6f15dee8.tar.gz
caelestia-shell-5b221fb72d6b915a56df4c17ecc747bb6f15dee8.tar.bz2
caelestia-shell-5b221fb72d6b915a56df4c17ecc747bb6f15dee8.zip
feat: news headlines for alerts pane
Also handle news api errors Also config num pages
Diffstat (limited to 'src/services')
-rw-r--r--src/services/news.ts35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/services/news.ts b/src/services/news.ts
index 5845aff..3d56186 100644
--- a/src/services/news.ts
+++ b/src/services/news.ts
@@ -2,13 +2,15 @@ import { notify } from "@/utils/system";
import { execAsync, GLib, GObject, property, readFileAsync, register, writeFileAsync } from "astal";
import { news as config } from "config";
-export interface Article {
+export interface IArticle {
title: string;
link: string;
- keywords: string;
- creator: string;
- description: string;
+ keywords: string[] | null;
+ creator: string[] | null;
+ description: string | null;
pubDate: string;
+ source_name: string;
+ category: string[];
}
@register({ GTypeName: "News" })
@@ -24,7 +26,8 @@ export default class News extends GObject.Object {
#notified = false;
#loading: boolean = false;
- #articles: Article[] = [];
+ #articles: IArticle[] = [];
+ #categories: { [category: string]: IArticle[] } = {};
@property(Boolean)
get loading() {
@@ -36,6 +39,11 @@ export default class News extends GObject.Object {
return this.#articles;
}
+ @property(Object)
+ get categories() {
+ return this.#categories;
+ }
+
async getNews() {
if (!config.apiKey.get()) {
if (!this.#notified) {
@@ -77,11 +85,14 @@ export default class News extends GObject.Object {
const url = `https://newsdata.io/api/1/latest?apikey=${config.apiKey.get()}&${args}`;
try {
const res = JSON.parse(await execAsync(["curl", url]));
- this.#articles = res.results;
+ if (res.status !== "success") throw new Error(`Failed to get news: ${res.results.message}`);
+
+ this.#articles = [...res.results];
let page = res.nextPage;
- for (let i = 0; i < 3; i++) {
+ for (let i = 1; i < config.pages.get(); i++) {
const res = JSON.parse(await execAsync(["curl", `${url}&page=${page}`]));
+ if (res.status !== "success") throw new Error(`Failed to get news: ${res.results.message}`);
this.#articles.push(...res.results);
page = res.nextPage;
}
@@ -95,6 +106,15 @@ export default class News extends GObject.Object {
}
this.notify("articles");
+ this.#categories = {};
+ for (const article of this.#articles) {
+ for (const category of article.category) {
+ if (!this.#categories.hasOwnProperty(category)) this.#categories[category] = [];
+ this.#categories[category].push(article);
+ }
+ }
+ this.notify("categories");
+
this.#loading = false;
this.notify("loading");
}
@@ -109,5 +129,6 @@ export default class News extends GObject.Object {
config.languages.subscribe(() => this.getNews().catch(console.error));
config.domains.subscribe(() => this.getNews().catch(console.error));
config.timezone.subscribe(() => this.getNews().catch(console.error));
+ config.pages.subscribe(() => this.getNews().catch(console.error));
}
}