-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.py
51 lines (40 loc) · 1.3 KB
/
watcher.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
"""
This is a watcher that given a list of scrapers will check every X interval forr
new links and yield a new message when that happens.
"""
import time
class Watcher:
"""
A watcher runs in the background and checks for new links.
"""
def __init__(self, scrapers=[], interval=60):
"""
Create a new Watcher.
"""
self.scrapers = scrapers
self.interval = interval
self.seen_articles = {}
self.init_articles()
def init_articles(self):
"""
Init articles will store all current articles in the watcher.
:return:
"""
for scraper in self.scrapers:
headers = scraper.headers()
for header in headers:
self.seen_articles[header.url] = header
def articles(self):
"""
Articles watches all the news sites and returns a message if new
articles are posted.
:return: Yields message
"""
while True:
for scraper in self.scrapers:
headers = scraper.headers()
for header in headers[0:2]:
if header.url not in self.seen_articles:
yield header
self.seen_articles[header.url] = header
time.sleep(self.interval)