Skip to content

Commit c32222f

Browse files
committed
Adding 6th Project
1 parent c0ab339 commit c32222f

File tree

1 file changed

+75
-0
lines changed
  • Project 6 - Desktop Notifier

1 file changed

+75
-0
lines changed

Diff for: Project 6 - Desktop Notifier/main.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import feedparser
2+
from win10toast_click import ToastNotifier
3+
import asyncio
4+
import webbrowser
5+
6+
7+
RSS_FEEDS = {
8+
"world": "http://rss.cnn.com/rss/edition_world.rss",
9+
"technology": "http://rss.cnn.com/rss/edition_technology.rss",
10+
"sports": "http://rss.cnn.com/rss/edition_sport.rss",
11+
"business": "http://rss.cnn.com/rss/money_news_international.rss",
12+
"health": "http://rss.cnn.com/rss/edition_health.rss",
13+
"science": "http://rss.cnn.com/rss/edition_space.rss",
14+
"entertainment": "http://rss.cnn.com/rss/edition_entertainment.rss"
15+
}
16+
17+
string_length = 60
18+
toaster = ToastNotifier()
19+
20+
def fetch_news(url):
21+
feed = feedparser.parse(url)
22+
return feed['entries']
23+
24+
def truncate_string(string, max_length):
25+
if len(string) > max_length:
26+
string[:max_length - 3] + "..."
27+
return string
28+
29+
def notify_news(news_item):
30+
title = truncate_string(news_item.get('title', 'No Title'), string_length)
31+
summary = truncate_string(news_item.get('summary', 'No Summary'), string_length)
32+
link = news_item.get('link', '')
33+
34+
def openLink():
35+
webbrowser.open_new(link)
36+
37+
toaster.show_toast(
38+
title=title,
39+
msg=summary,
40+
duration=10,
41+
icon_path=None,
42+
threaded=True,
43+
callback_on_click=openLink
44+
)
45+
46+
async def getUrl(url):
47+
seen_link = set()
48+
while True:
49+
news_items = fetch_news(url)
50+
for news_item in news_items:
51+
link = news_item.get('link', '')
52+
if link not in seen_link:
53+
seen_link.add(link)
54+
notify_news(news_item)
55+
await asyncio.sleep(10)
56+
await asyncio.sleep(10)
57+
58+
async def main():
59+
print("Please select category of the following Options : ")
60+
61+
for category in RSS_FEEDS.keys():
62+
print(f'- {category}')
63+
64+
selected_category = input("Enter the category : ").strip().lower()
65+
66+
if selected_category not in RSS_FEEDS:
67+
print("Invalid Category !!!!")
68+
return
69+
70+
url = RSS_FEEDS[selected_category]
71+
await getUrl(url)
72+
73+
if __name__ == '__main__':
74+
asyncio.run(main())
75+

0 commit comments

Comments
 (0)