Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
.idea/
__pycache__
local_data/
ui/__pycache__/
env/
venv/

# Local Data and Databases
data/
*.db
*.sqlite3
*.csv
*.json

# C++ objects and libs
*.slo
*.lo
Expand Down
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/UniNews.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added __pycache__/app_settings.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/database.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/notifications.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/rss_service.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/settings.cpython-310.pyc
Binary file not shown.
43 changes: 43 additions & 0 deletions app_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
from pathlib import Path

from settings import APP_DATA_DIR


SETTINGS_FILE = APP_DATA_DIR / "settings.json"


DEFAULT_SETTINGS = {
"refresh_on_startup": True,
"refresh_interval_minutes": 0,
"notifications_enabled": False,
"notification_keywords": ["deadline", "scholarship", "application", "exam"],
"theme": "light",
"max_cached_articles": 500,
}


def load_app_settings() -> dict:
if not SETTINGS_FILE.exists():
save_app_settings(DEFAULT_SETTINGS)
return DEFAULT_SETTINGS.copy()

try:
with open(SETTINGS_FILE, "r", encoding="utf-8") as file:
settings = json.load(file)

merged_settings = DEFAULT_SETTINGS.copy()
merged_settings.update(settings)

return merged_settings

except json.JSONDecodeError:
save_app_settings(DEFAULT_SETTINGS)
return DEFAULT_SETTINGS.copy()


def save_app_settings(settings: dict) -> None:
SETTINGS_FILE.parent.mkdir(parents=True, exist_ok=True)

with open(SETTINGS_FILE, "w", encoding="utf-8") as file:
json.dump(settings, file, indent=4)
22 changes: 22 additions & 0 deletions data/feeds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "MIT News",
"url": "https://news.mit.edu/rss/feed"
},
{
"name": "Harvard Gazette",
"url": "https://news.harvard.edu/gazette/feed/"
},
{
"name": "Nature News",
"url": "https://www.nature.com/nature.rss"
},
{
"name": "Science Magazine",
"url": "https://www.science.org/action/showFeed?type=etoc&feed=rss&jc=science"
},
{
"name": "NIH News",
"url": "https://www.nih.gov/news-events/news-releases/feed"
}
]
7 changes: 7 additions & 0 deletions data/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"refresh_interval_minutes": 30,
"notifications_enabled": true,
"notification_keywords": ["scholarship", "deadline", "exam", "application"],
"theme": "light",
"max_cached_articles": 500
}
177 changes: 177 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import sqlite3
from pathlib import Path
from typing import Optional

import settings


class Database:
def __init__(self, database_path: Path | None = None):
if database_path is None:
database_path = settings.DATABASE_PATH

self.database_path = Path(database_path)
self.database_path.parent.mkdir(parents=True, exist_ok=True)

self.connection = sqlite3.connect(self.database_path)
self.connection.row_factory = sqlite3.Row

self.create_tables()

def create_tables(self) -> None:
self.connection.execute(
"""
CREATE TABLE IF NOT EXISTS articles
(
id
INTEGER
PRIMARY
KEY
AUTOINCREMENT,
university
TEXT
NOT
NULL,
title
TEXT
NOT
NULL,
summary
TEXT,
link
TEXT
NOT
NULL
UNIQUE,
published
TEXT,
fetched_at
TEXT
NOT
NULL,
image_url
TEXT
);
"""
)
self.connection.commit()

try:
self.connection.execute("ALTER TABLE articles ADD COLUMN image_url TEXT;")
self.connection.commit()
except sqlite3.OperationalError:
pass

def save_article(self, article: dict) -> None:
query = """
INSERT \
OR IGNORE INTO articles (
university,
title,
summary,
link,
published,
fetched_at,
image_url
)
VALUES (?, ?, ?, ?, ?, ?, ?); \
"""

self.connection.execute(
query,
(
article.get("university", "Unknown university"),
article.get("title", "Untitled"),
article.get("summary", ""),
article.get("link", ""),
article.get("published", "Unknown date"),
article.get("fetched_at", ""),
article.get("image_url", ""),
),
)

self.connection.commit()

def save_articles(self, articles: list[dict]) -> None:
for article in articles:
self.save_article(article)

def get_articles(
self,
university: Optional[str] = None,
search_text: Optional[str] = None,
limit: int = 200,
) -> list[dict]:
query = """
SELECT
id,
university,
title,
summary,
link,
published,
fetched_at,
image_url
FROM articles
WHERE 1 = 1
"""

params = []

if university and university != "All Universities":
query += " AND university = ?"
params.append(university)

if search_text:
query += """
AND (
title LIKE ?
OR summary LIKE ?
OR university LIKE ?
)
"""

search_pattern = f"%{search_text}%"
params.extend([search_pattern, search_pattern, search_pattern])

query += """
ORDER BY id DESC
LIMIT ?
"""

params.append(limit)

cursor = self.connection.execute(query, params)
rows = cursor.fetchall()

return [dict(row) for row in rows]

def delete_all_articles(self) -> None:
self.connection.execute("DELETE FROM articles;")
self.connection.commit()

def close(self) -> None:
self.connection.close()

def enforce_article_limit(self, max_articles: int) -> None:
query = """
DELETE \
FROM articles
WHERE id NOT IN (SELECT id \
FROM articles \
ORDER BY id DESC
LIMIT ?
); \
"""

self.connection.execute(query, (max_articles,))
self.connection.commit()

def load_cached_articles(self):
try:
self.articles = self.database.get_articles()
except Exception as error:
print(f"Could not load cached articles: {error}")
self.articles = []

self.render_articles()
Loading