This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting.py
301 lines (260 loc) · 9.63 KB
/
formatting.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import re
import logging
from urllib import urlencode
import httplib
from misc import quote
import stock
import yfrog
import ytembed
import flickrembed
import constants
import hash_bucket
from google.appengine.api import memcache
URLS_NAMESPACE = 'URLS'
URLS_CACHE_EXPIRATION = 3600 * 48
URLRX = re.compile(r'((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)', re.U)
STOCK_URLX = re.compile(r'\$([A-Z]+(\.[A-Z]+)?)(([\s,\.!\?\-\:]+)|$)', re.U)
def mobypictureMapper(m):
url = m.group(0)
th_url = "http://api.mobypicture.com?%s" % \
(urlencode([('t', url),
('s', 'small'),
('k',constants.MOBYPIC_DEV_KEY),
('format','plain')]))
res = '<a href="%s"><img class="tweet-image" src="%s"/></a>' % (url, th_url)
if m.start() != 0:
res = '<br />' + res
return res
def stockMapper(m):
symbol = str(m.group(1))
ws = str(m.group(3))
try:
valid = stock.isStockSymbol(symbol)
except:
logging.exception("Error looking up stock symbol '%s'" % symbol)
valid = False
if valid:
return '<a href="http://stocktwits.com/t/%s" target="_blank">$%s</a>%s' % \
(quote(symbol), symbol, ws);
else:
# unknown stock symbol. Leave as is.
return m.group(0)
def youtubeMapper(m):
media_id = m.group(3)
embed = None
try:
embed = ytembed.getEmbed(media_id)
except:
logging.exception("Error getting youtube embed for id '%s'" % media_id)
if embed:
return embed
else:
url = m.group(0)
return '<a href="%s">%s</a>' % (url, url)
def mailtoMapper(m):
email = m.group(2)
return '<a href="mailto:%s">%s</a>' % (email, email)
def defaultMapper(m):
url = m.group(1)
return '<a href="%s" class="user-link">%s</a>' % (url, url)
def yfrogMapper(m):
url = m.group(0)
logging.debug(url)
domain = m.group(1)
media_id = m.group(4)
if domain=="us" or \
media_id.endswith('z') or \
media_id.endswith('f'):
# Video file, try to get embed
try:
embed = yfrog.getEmbed(media_id)
if embed!=None:
return embed
except:
logging.exception("Error getting emebed for yfrog media '%s'" % media_id)
if media_id.endswith('x'):
return '<a href="%s">%s</a>' % (url, url)
res = '<a class="tweet-image yfrog-image" href="%s"><img src="%s.th.jpg" onerror="$(this).attr(\'src\',\'%s.jpg\')"/></a>' % (url, url, url)
if m.start() != 0:
res = '<br />' + res
return res
def yfrogMapper2(m):
server = m.group(2)
basename = m.group(3)
ext = m.group(4)
logging.debug(server + ' '+ basename + ' '+ ext)
bucket = hash_bucket.bucket(basename + '.' + ext)
logging.debug('2' + server + ' '+ basename + ' '+ ext)
if ext in ('flv', 'mp4'):
url = 'http://%s.imageshack.us/%s/%s/%s.%s.th.jpg' % (server, server, bucket, basename, ext)
else:
url = 'http://%s.imageshack.us/%s/%s/%s.th.%s' % (server, server, bucket, basename, ext)
logging.debug('Formated %s as %s' % (m.group(1), url))
return '<a href="%s" class="tweet-image yfrog2-image"><img src="%s"></a>' % (m.group(0), url)
def twitpicMapper(m):
url = m.group(0)
media_id = m.group(3)
return '<a href="%s"><img class="tweet-image" src="http://twitpic.com/show/thumb/%s"/ title="Preview"></a>' % (url, media_id)
def flickrMapper(m):
url = m.group(0)
photo_id = m.group(2)
logging.debug('flickrMapper starting for url %s photoid %s' % (url, photo_id))
try:
embed = flickrembed.getEmbed(url, photo_id)
except:
logging.exception("Error getting flickr embed for id %s" % photo_id)
if embed:
if m.start() != 0:
return '<br />' + embed
else:
return embed
else: return '<a href="%s">%s</a>' % (url, url)
def tweetphotoMapper(m):
url = m.group(0)
return '<a href="%s" alt="thumbnail" title="Preview"><img class="tweet-image" src="http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=thumbnail&url=%s"></a>' % (url, url)
def urlResolver(match):
url = match.group(0)
site = match.group(2)
short = match.group(3)
cached = memcache.get(unicode(url), namespace = URLS_NAMESPACE)
if cached: return cached
try:
conn = httplib.HTTPConnection(site)
conn.request('HEAD', short)
resp = conn.getresponse()
except e:
logging.error('Could not get responce from %s' % site)
return None
if resp.status >= 300 and resp.status < 400:
longurl = unicode(resp.getheader('Location'), 'utf-8')
logging.debug(u'Resolved %s as %s' % (url, longurl))
memcache.set(url, longurl,
time = URLS_CACHE_EXPIRATION,
namespace = URLS_NAMESPACE)
return longurl
else:
return None
SHORTLINK_WEBSITES = ['bit.ly',
'tinyurl.com',
'su.pr',
'ow.ly',
'is.gd',
'tr.im',
'ff.im']
# this is good until there is a shortlink website which does not return
# target url in location header
URLSOLVERS = [ (re.compile('http://(www.)?(%s)(/.*)' % site, re.U), urlResolver) \
for site in SHORTLINK_WEBSITES ]
MAPPERS = [
(re.compile(r'(^mailto:([^ ]+))$'), mailtoMapper),
(re.compile(r'^http://((www\.)?yfrog\.(com|ru|es|fr|us|org|it|pl|eu|com\.pl|com\.tr|co\.uk|co\.il))/([^./\:\?]+)$'), yfrogMapper),
(re.compile(r'http://((img\d+).yfrog.com|ru|es|fr|us|org|it|pl|eu|com\.pl|com\.tr|co\.uk|co\.il)/i/(\w+).(\w{1,4})/?$'), yfrogMapper2),
(re.compile(r'^http://((www\.)?twitpic\.com)/([^/\?]+)$'), twitpicMapper),
(re.compile(r'^http://((www\.)?youtube\.com)/v/([^/\?]+)$'), youtubeMapper),
(re.compile(r'^http://((www\.)?youtube\.com)/watch\?v=([^/\?]+)$'), youtubeMapper),
(re.compile(r'^http://((www\.)?mobypicture\.com)/\?([^/\?]+)$'), mobypictureMapper),
(re.compile(r'^http://(www\.)?flickr.com/photos/[\w\@]+/(\d+)'), flickrMapper),
(re.compile(r'^http://(www\.)?tweetphoto.com/\w+'), tweetphotoMapper),
(re.compile(r'^http://((www\.)?pic.gd/\w+)'), tweetphotoMapper),
(re.compile(r'^(.+)$'), defaultMapper)
]
def itemHTML(e, decorate = True):
""" Format tweet as HTML """
tweet = e.text
# URLs
res = ''
prev = 0
def urlSolver(match):
url = match.group(0)
for (regex, handler) in URLSOLVERS:
match = regex.match(url)
if match:
try:
res = handler(match)
return res if res else url
except:
return url
return url
def urlMapper(match):
url = match.group(0)
for (regex, handler) in MAPPERS:
match = regex.match(url)
if match:
try: res = handler(match)
except: res = None
if res: return res
else: return url
return url
tweet = URLRX.subn(urlSolver, tweet)[0]
tweet = URLRX.subn(urlMapper, tweet)[0]
# This is old function. I don't see any reason not to use re.subn
#for m in URLRX.finditer(tweet):
#(fro, to) = m.span()
#url = m.group(1)
#res += tweet[prev:fro]
#prev = to
#for (regex, handler) in URLSOLVERS:
#match = regex.match(url)
#if match:
#res += handler(match)
#break
#for (mo,mf) in MAPPERS:
#mx = mo.match(url)
#if mx:
#res += mf(mx)
#break
#tweet = res + tweet[prev:]
# stock symbols
res = ''
prev = 0
for m in STOCK_URLX.finditer(tweet):
logging.debug("Found stock ")
(fro, to) = m.span()
res += tweet[prev:fro]
prev = to
res += stockMapper(m)
tweet = res + tweet[prev:]
# @usernames
tweet = re.sub(r'(\A|\s)@(\w+)', r'\1<a href="http://www.twitter.com/\2">@\2</a>', tweet)
# #hashtags
tweet = re.sub(r'(\A|\s)#(\w+)', r'\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>', tweet)
# link to sender
if hasattr(e, 'from_friend'):
name = e.from_friend.screen_name
elif hasattr(e, 'to'):
name = e.author
if decorate:
tweet = '<a href="http://twitter.com/%s">%s</a>: %s\n<br><hr>%s' % (
name,
name,
tweet,
footer(e)
)
return tweet
def _icon_embed(name, link, alt):
return '<a href="%s"><img src="%s%s%s?ver=1" alt="%s" title="%s"/></a>' % \
(link,
constants.SITE_BASE_URL,
constants.ICONS_PATH,
name,
alt,alt)
def footer(e):
if hasattr(e, 'from_friend'):
name = e.from_friend.screen_name
elif hasattr(e, 'to'):
name = e.author
reply_link = "http://twitter.com/home?status=@%s%%20&in_reply_to_status_id=%d&in_reply_to=%s" % \
(name, e.id, name)
rt_link = "http://twitter.com/home?status=RT%%20@%s:%%20%s&in_reply_to_status_id=%d&in_reply_to=%s" % \
(name,
quote(e.text),
e.id,
name)
msg_link = "http://twitter.com/direct_messages/create/%s" % \
(name)
return \
_icon_embed('reply.png',reply_link,"Reply") + \
" " +\
_icon_embed('retweet.png',rt_link,"Re-tweet") + \
" " +\
_icon_embed('direct_msg.png',msg_link,"Direct message")