forked from marado/hattai-fortune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hattai-fortune.py
executable file
·217 lines (173 loc) · 5.36 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# hattai-fortune
#
# Original author: Mindbooster Noori
# Author of python version: Nuno Nunes <[email protected]>
import sys
sys.path.append("./feedparser")
import feedparser
import pickle
import traceback
from HTMLParser import HTMLParser
# Configuration
#
debug = False
feed_url = "http://feeds.feedburner.com/publicoRSS"
title_file_name = "title"
link_file_name = "link"
memory_file_name = "memory"
max_memory_size = 20
bad_words = [ "olhanense", "psilon", "benfic", "assinant", "sporting",
"chelsea", "arsenal", "derby", "golo", "djokovic", "jogo",
"ronaldo" ]
substitute_chars = { '“': '"', '”': '"' }
#
################
# Global variables
#
memory = []
#
################
# Functions
#
def getNewNews():
"""Read the RSS feed and fetch new articles."""
global memory
seen_titles = [ article["title"] for article in memory ]
if debug:
print "===> Parsing feed"
new_memories = []
feed = feedparser.parse(feed_url)
for post in feed.entries:
post.title = post.title.encode("utf-8")
post.link = post.link.encode("utf-8")
if debug:
print "\"" + post.title + "\""
if post.title in seen_titles:
if debug:
print "Already seen this title, ignoring"
continue
if post.title == "":
if debug:
print "Empty title, ignoring"
continue
has_bad_words = False
for bad_word in bad_words:
if bad_word in post.title.lower():
has_bad_words = True
if debug:
print "Title has bad word \"" + bad_word + "\" ignoring"
continue
if has_bad_words:
continue
new_memories.append( { "title": post.title, "link": post.link, "published": post.published, "used": 0 } )
memory = new_memories + memory
memory = memory[:max_memory_size]
if debug:
print "Memory contents:"
__dump_memory__()
print
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
if debug:
print "===> Choosing the best article"
print "Memory has " + str(len(memory)) + " articles"
for article in memory:
if debug:
print "Analizyng article \"" + article["title"] + "\" (" \
+ str(article["used"]) + ")"
if article["used"] < best_used:
best_used = article["used"]
best_index = i
if debug:
print "Best so far"
i += 1
if debug:
print "Memory contents:"
__dump_memory__()
print
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 = []
if debug:
print "===> Initializing"
print "Found " + str(len(memory)) + " articles on disk:"
__dump_memory__()
print
def closeUpShop( chosen_article_index ):
"""Commit memory to file, write title and link to files and reply with
the chosen title."""
if debug:
print "===> 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" )
title = memory[chosen_article_index]["title"]
title = clean_string( title )
title_file.write( title )
link_file.write( memory[chosen_article_index]["link"] )
print title
memory[chosen_article_index]["used"] += 1
pickle.dump( memory, memory_file )
title_file.close()
link_file.close()
memory_file.close()
except:
print "BORK! : " + traceback.format_exc()
if debug:
print "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()
s.feed(html)
return s.get_data()
def __substitute_weird_chars__( string ):
clean_string = string
for char, subst in substitute_chars.iteritems():
clean_string = clean_string.replace( char, subst )
return clean_string
def __dump_memory__():
"""Print the memory contents in a pretty way."""
for article in memory:
print "---------- (" + str(article["used"]) + ") Title: \"" + article["title"] + "\""
print "Link: \"" + article["link"] + "\""
print "Published: " + article["published"]
#
################
# Class to strip HTML clean
#
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
#
################
if __name__ == "__main__":
if debug:
print "===> In the beginning..."
initializeStuff()
getNewNews()
article_index = chooseArticle()
closeUpShop( article_index )
if debug:
print "===> All done!"