-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss-feeds.py
120 lines (92 loc) · 4.05 KB
/
rss-feeds.py
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
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
"""
@author: Jimit.Dholakia
"""
from datetime import datetime, timedelta
import time
import os
import itertools
import feedparser
import urllib.parse
import dateutil.parser
import signal
import sys
os.environ['TZ'] = 'Asia/Kolkata'
if os.name != 'nt':
time.tzset()
TIME_FMT = '%b %d, %Y %H:%M:%S %Z'
RSS_FEEDS = ['https://jimit105.medium.com/feed',
'https://jimit105.github.io/pytricks/rss.xml',
'https://scitech105.blogspot.com/rss.xml',
'https://jimit105.github.io/medium-articles/rss.xml',
'https://www.pyimagesearch.com/feed/',
'https://machinelearningmastery.com/feed/',
'https://www.fast.ai/atom.xml',
'https://openai.com/blog/rss/',
'https://research.fb.com/feed/',
'http://googleaiblog.blogspot.com/atom.xml',
'https://blogs.microsoft.com/ai/feed/',
'https://www.analyticsvidhya.com/feed/',
'https://www.hackerearth.com/blog/machine-learning/feed',
'https://mlfromscratch.com/rss/',
'https://www.mygreatlearning.com/blog/category/artificial-intelligence/feed/',
'https://blog.tensorflow.org/atom.xml',
'http://feeds.feedburner.com/kdnuggets-data-mining-analytics',
'https://lazyprogrammer.me/feed/'
]
TOP_N = 250
def handler(signum, frame):
print('Signal handler called with signal', signum)
sys.exit('Took too long.. Goodbye!')
def parse_date(input_date):
dt = dateutil.parser.parse(input_date)
dt2 = dt - timedelta(seconds=time.timezone)
return dt2
def convert_timezone(datetime_struct_time):
dt = datetime.fromtimestamp(time.mktime(datetime_struct_time))
dt2 = dt - timedelta(seconds=time.timezone)
return dt2
def fetch_feeds(feed_url):
try:
feed = feedparser.parse(feed_url)
output = []
for entry in feed.entries:
if entry.updated_parsed is None:
article = '<p><a href="' + entry.link + '" target="_blank">' + entry.title + '</a><br/>' + feed.feed.title + \
' | ' + \
parse_date(entry.updated).strftime(
TIME_FMT) + '</p>'
output.append((article, parse_date(entry.updated)))
else:
article = '<p><a href="' + entry.link + '" target="_blank">' + entry.title + '</a><br/>' + feed.feed.title + \
' | ' + \
convert_timezone(entry.updated_parsed).strftime(
TIME_FMT) + '</p>'
output.append(
(article, convert_timezone(entry.updated_parsed)))
print('Fetching complete for ' + feed_url)
except Exception as e:
print('Fetching failed for ' + feed_url + '\tException: ' + str(e))
print('Exception:', str(e))
return output
signal.signal(signal.SIGALRM, handler)
signal.alarm(300)
result = list(map(fetch_feeds, RSS_FEEDS))
signal.alarm(0)
merged = list(itertools.chain(*result))
merged = list(set(merged))
output = sorted(merged, key=lambda x: x[-1], reverse=True)
all_articles = ''
for article in output[:TOP_N]:
all_articles += article[0]
current_time = time.strftime(TIME_FMT, time.localtime())
action_badge = ''
maintainer_badge = '[![Maintained by Jimit Dholakia](https://img.shields.io/badge/Maintained%20by-jimit105-brightgreen)](https://github.com/jimit105)'
linkedin_badge = '[![LinkedIn: jimit105](https://img.shields.io/badge/LinkedIn-Jimit%20Dholakia-blue)](https://www.linkedin.com/in/jimit105/)'
medium_badge = '[![Medium: jimit105](https://img.shields.io/badge/Medium-jimit105-orange)](https://jimit105.medium.com/)'
header = action_badge + '\n' + maintainer_badge + '\n' + linkedin_badge + '\n' + medium_badge + '\n![Last Updated](https://img.shields.io/badge/Last%20Updated%20on-' + \
urllib.parse.quote(current_time) + '-yellowgreen)' + '\n\n'
complete_text = header + all_articles
with open('README.md', 'w') as f:
f.write(complete_text)
print('RSS Feeds Update Complete')