-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhattai-fortune.py
executable file
·254 lines (197 loc) · 6.73 KB
/
hattai-fortune.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# hattai-fortune
#
# Original author: Marcos Marado <[email protected]>
# Author of python version: Nuno Nunes <[email protected]>
import sys
import getopt
sys.path.append("./feedparser")
import feedparser
import pickle
import traceback
from html.parser import HTMLParser
import logging
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', filename='debug.log')
import requests
# Configuration
#
debug = False
feed_url = \
"https://www.rtp.pt/noticias/rss"
# "https://www.noticiasaominuto.com/rss/ultima-hora"
title_file_name = "title"
link_file_name = "link"
memory_file_name = "memory"
desc_file_name = "desc"
max_memory_size = 100
bad_words = ["olhanense", "psilon", "benfic", "assinant", "sporting",
"chelsea", "arsenal", "derby", "golo", "djokovic", "jogo",
"ronaldo", "Brasil"]
substitute_chars = {'“': '"', '”': '"'}
#
################
# Global variables
#
memory = []
logger = logging.getLogger(__name__)
#
################
# Functions
#
def getNewNews():
"""Read the RSS feed and fetch new articles."""
global memory
seen_titles = [article["title"] for article in memory]
logger.debug("===> Parsing feed")
new_memories = []
response = requests.get(feed_url, timeout=120)
feed = feedparser.parse(response.text)
for post in feed.entries:
if post.title == "":
logger.debug("Empty title, ignoring")
continue
if post.link == "":
logger.debug("Empty link, ignoring")
continue
post.title = post.title.encode("utf-8")
post.link = post.link.encode("utf-8")
post.description = post.description.encode("utf-8")
logger.debug("\"%s\"" % post.title)
if post.title in seen_titles:
logger.debug("Already seen this title, ignoring")
continue
has_bad_words = False
for bad_word in bad_words:
if bad_word.encode('utf-8') in post.title.lower():
has_bad_words = True
logger.debug("Title has bad word \""+bad_word+"\" ignoring")
continue
if has_bad_words:
continue
new_memories.append({"title": post.title,
"link": post.link,
"description": post.description,
"published": post.published,
"used": 0})
if len(new_memories) > max_memory_size:
logger.warning("The max memory size (" + str(max_memory_size) +
") is too small for this feed (which needs at least " +
str(len(new_memories)) +
"), we will have memory issues!")
memory = new_memories + memory
memory = memory[:max_memory_size]
if logger.getEffectiveLevel() <= logging.DEBUG:
__dump_memory__()
def chooseArticle():
"""Chooses an article from our memory, as fresh as possible, and returns
it's index in memory."""
best_used = 999999
best_index = None
i = 0
logger.debug("===> Choosing the best article")
logger.debug("Memory has " + str(len(memory)) + " articles")
for article in memory:
logger.debug("Analizyng article \"%s\" (%s)" % (article["title"],
str(article["used"])))
if article["used"] < best_used:
best_used = article["used"]
best_index = i
logger.debug("Best so far")
i += 1
if logger.getEffectiveLevel() <= logging.DEBUG:
__dump_memory__()
return best_index
def initializeStuff():
"""Read state from files (memory)."""
global memory
try:
memory_file = open(memory_file_name, "rb")
memory = pickle.load(memory_file)
memory_file.close()
except:
memory = []
logger.debug("===> Initializing")
logger.debug("Found " + str(len(memory)) + " articles on disk:")
if logger.getEffectiveLevel() <= logging.DEBUG:
__dump_memory__()
def closeUpShop(chosen_article_index):
"""Commit memory to file, write title and link to files and reply with
the chosen title."""
logger.debug("===> Writing results and saving state")
try:
title_file = open(title_file_name, "w")
link_file = open(link_file_name, "w")
memory_file = open(memory_file_name, "wb")
desc_file = open(desc_file_name, "w")
title = memory[chosen_article_index]["title"].decode('utf-8')
title = clean_string(title)
title_file.write(title)
link_file.write(memory[chosen_article_index]["link"].decode('utf-8'))
desc_file.write(memory[chosen_article_index]["description"].decode('utf-8'))
print(title)
memory[chosen_article_index]["used"] += 1
pickle.dump(memory, memory_file)
title_file.close()
link_file.close()
memory_file.close()
except:
logger.error("BORK! : " + traceback.format_exc())
logger.debug("Stored memory with " + str(len(memory)) + " articles")
def clean_string(text):
clean_text = __strip_tags__(text)
clean_text = __substitute_weird_chars__(clean_text)
return clean_text
def __strip_tags__(html):
s = MLStripper()
logger.debug("__strip_tags__(html), htmlk is " + str(html));
s.feed(str(html))
return s.get_data()
def __substitute_weird_chars__(string):
clean_string = string
for char, subst in substitute_chars.items():
clean_string = clean_string.replace(char, subst)
return clean_string
def __dump_memory__():
"""Print the memory contents in a pretty way."""
logger.debug("Memory contents:")
for article in memory:
logger.debug("---------- (%s) Title: \"%s\"" % (
article["used"], article["title"]))
logger.debug("Link: \"%s\"" % article["link"])
logger.debug("Published: %s" % article["published"])
logger.debug("EOM")
def handleOptions():
global debug
try:
opts, args = getopt.getopt(sys.argv[1:], "d", ["debug"])
except:
return
for opt, arg in opts:
if opt in ("-d", "--debug"):
debug = True
if debug:
logger.setLevel(logging.DEBUG)
#
################
# Class to strip HTML clean
#
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
#
################
if __name__ == "__main__":
handleOptions()
logger.debug("===> In the beginning...")
initializeStuff()
getNewNews()
article_index = chooseArticle()
closeUpShop(article_index)
logger.debug("===> All done!")