-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_feeds.py
executable file
·86 lines (75 loc) · 2.06 KB
/
parse_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
# Require https://github.com/Azelphur/pyPushBullet
import feedparser
from pushbullet import PushBullet
apiKey = ""
def read_config():
feeds_file = open("feeds.txt",'r')
feeds = []
current_feed = {}
for line in feeds_file:
if (line[0] == '#') or (line.isspace()):
continue
elif line[0] == '+':
feeds[-1]["positive"].append(line[1:-1])
elif line[0] == '-':
feeds[-1]["negative"].append(line[1:-1])
elif line.split()[0] == "pushbullet_api":
global apiKey
apiKey = line.split()[2]
else:
current_feed = dict()
current_feed["url"] = line[:-1]
current_feed["positive"] = []
current_feed["negative"] = []
feeds.append(current_feed)
feeds_file.close()
return feeds
def read_log():
log_file = open("log.txt",'r')
previous_files = []
for line in log_file:
previous_files.append(line[:-1])
log_file.close()
return previous_files
def write_log(entries_list):
log_file = open("log.txt",'a')
for entry in entries_list:
log_file.write(entry["title"] + '\n')
log_file.close()
def push_link(entries_list):
print apiKey
client = PushBullet(apiKey)
for entry in entries_list:
client.push_link(entry["title"], entry["link"])
def main():
feeds = read_config()
previous_titles = read_log()
new_entries = []
for feed_info in feeds:
entries = filter_feed(feed_info)
for entry in entries:
if entry["title"] not in previous_titles:
new_entries.append(entry)
print "New file found", entry["title"]
push_link(new_entries)
write_log(new_entries)
def filter_feed(feed_info):
new_entries = []
feed = feedparser.parse(feed_info["url"])
for entry in feed["entries"]:
is_valid = True
title = entry["title"]
for keyword in feed_info["positive"]:
if keyword not in title:
is_valid = False
break
if not is_valid:
continue
for keyword in feed_info["negative"]:
if keyword in title:
is_valid = False
break
if is_valid:
new_entries.append(entry)
return new_entries
main()