forked from fogleman/FeedNotifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
266 lines (241 loc) · 7.5 KB
/
util.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
255
256
257
258
259
260
261
262
263
264
265
266
import wx
import os
import re
import time
import base64
import calendar
import urllib2
import urlparse
import threading
import feedparser
from htmlentitydefs import name2codepoint
from settings import settings
import ssl
def set_icon(window):
bundle = wx.IconBundle()
bundle.AddIcon(wx.Icon('icons/16.png', wx.BITMAP_TYPE_PNG))
bundle.AddIcon(wx.Icon('icons/24.png', wx.BITMAP_TYPE_PNG))
bundle.AddIcon(wx.Icon('icons/32.png', wx.BITMAP_TYPE_PNG))
bundle.AddIcon(wx.Icon('icons/48.png', wx.BITMAP_TYPE_PNG))
bundle.AddIcon(wx.Icon('icons/256.png', wx.BITMAP_TYPE_PNG))
window.SetIcons(bundle)
def start_thread(func, *args):
thread = threading.Thread(target=func, args=args)
thread.setDaemon(True)
thread.start()
return thread
def scale_bitmap(bitmap, width, height, color):
bw, bh = bitmap.GetWidth(), bitmap.GetHeight()
if bw == width and bh == height:
return bitmap
if width < 0:
width = bw
if height < 0:
height = bh
buffer = wx.EmptyBitmap(bw, bh)
dc = wx.MemoryDC(buffer)
dc.SetBackground(wx.Brush(color))
dc.Clear()
dc.DrawBitmap(bitmap, 0, 0, True)
image = wx.ImageFromBitmap(buffer)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.BitmapFromImage(image)
return result
def menu_item(menu, label, func, icon=None, kind=wx.ITEM_NORMAL):
item = wx.MenuItem(menu, -1, label, kind=kind)
if func:
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
if icon:
item.SetBitmap(wx.Bitmap(icon))
menu.AppendItem(item)
return item
def select_choice(choice, data):
for index in range(choice.GetCount()):
if choice.GetClientData(index) == data:
choice.Select(index)
return
choice.Select(wx.NOT_FOUND)
def get_top_window(window):
result = None
while window:
result = window
window = window.GetParent()
return result
def get(obj, key, default):
value = obj.get(key, None)
return value or default
def abspath(path):
path = os.path.abspath(path)
path = 'file:///%s' % path.replace('\\', '/')
return path
def parse(url, username=None, password=None, etag=None, modified=None):
agent = settings.USER_AGENT
handlers = [get_proxy()]
if username and password:
url = insert_credentials(url, username, password)
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
return feedparser.parse(url, etag=etag, modified=modified, agent=agent, handlers=handlers)
def is_valid_feed(data):
entries = get(data, 'entries', [])
title = get(data.feed, 'title', '')
link = get(data.feed, 'link', '')
return entries or title or link
def insert_credentials(url, username, password):
parts = urlparse.urlsplit(url)
netloc = parts.netloc
if '@' in netloc:
netloc = netloc[netloc.index('@')+1:]
netloc = '%s:%s@%s' % (username, password, netloc)
parts = list(parts)
parts[1] = netloc
return urlparse.urlunsplit(tuple(parts))
def encode_password(password):
return base64.b64encode(password) if password else None
def decode_password(password):
try:
return base64.b64decode(password) if password else None
except Exception:
return None
def get_proxy():
if settings.USE_PROXY:
url = decode_password(settings.PROXY_URL)
if url:
# User-configured Proxy
map = {
'http': url,
'https': url,
}
proxy = urllib2.ProxyHandler(map)
else:
# Windows-configured Proxy
proxy = urllib2.ProxyHandler()
else:
# No Proxy
proxy = urllib2.ProxyHandler({})
return proxy
def find_themes():
return ['default'] # TODO: more themes!
result = []
names = os.listdir('themes')
for name in names:
if name.startswith('.'):
continue
path = os.path.join('themes', name)
if os.path.isdir(path):
result.append(name)
return result
def guess_polling_interval(entries):
if len(entries) < 2:
return settings.DEFAULT_POLLING_INTERVAL
timestamps = []
for entry in entries:
timestamp = calendar.timegm(get(entry, 'date_parsed', time.gmtime()))
timestamps.append(timestamp)
timestamps.sort()
durations = [b - a for a, b in zip(timestamps, timestamps[1:])]
mean = sum(durations) / len(durations)
choices = [
60,
60*5,
60*10,
60*15,
60*30,
60*60,
60*60*2,
60*60*4,
60*60*8,
60*60*12,
60*60*24,
]
desired = mean / 2
if desired == 0:
interval = settings.DEFAULT_POLLING_INTERVAL
elif desired < choices[0]:
interval = choices[0]
else:
interval = max(choice for choice in choices if choice <= desired)
return interval
def time_since(t):
t = int(t)
now = int(time.time())
seconds = max(now - t, 0)
if seconds == 1:
return '1 second'
if seconds < 60:
return '%d seconds' % seconds
minutes = seconds / 60
if minutes == 1:
return '1 minute'
if minutes < 60:
return '%d minutes' % minutes
hours = minutes / 60
if hours == 1:
return '1 hour'
if hours < 24:
return '%d hours' % hours
days = hours / 24
if days == 1:
return '1 day'
return '%d days' % days
def split_time(seconds):
if seconds < 60:
return seconds, 0
minutes = seconds / 60
if minutes < 60:
return minutes, 1
hours = minutes / 60
days = hours / 24
if days and hours % 24 == 0:
return days, 3
return hours, 2
def split_time_str(seconds):
interval, units = split_time(seconds)
strings = ['second', 'minute', 'hour', 'day']
string = strings[units]
if interval != 1:
string += 's'
return '%d %s' % (interval, string)
def pretty_name(name):
name = ' '.join(s.title() for s in name.split('_'))
last = '0'
result = ''
for c in name:
if c.isdigit() and not last.isdigit():
result += ' '
result += c
last = c
return result
def replace_entities1(text):
entity = re.compile(r'&#(\d+);')
def func(match):
try:
return unichr(int(match.group(1)))
except Exception:
return match.group(0)
return entity.sub(func, text)
def replace_entities2(text):
entity = re.compile(r'&([a-zA-Z]+);')
def func(match):
try:
return unichr(name2codepoint[match.group(1)])
except Exception:
return match.group(0)
return entity.sub(func, text)
def remove_markup(text):
html = re.compile(r'<[^>]+>')
return html.sub(' ', text)
def format(text, max_length=400):
previous = ''
while text != previous:
previous = text
text = replace_entities1(text)
text = replace_entities2(text)
text = remove_markup(text)
text = ' '.join(text.split())
if len(text) > max_length:
text = text[:max_length].strip()
text = text.split()[:-1]
text.append('[...]')
text = ' '.join(text)
return text