forked from snarfed/bridgy
/
util.py
623 lines (491 loc) · 21.1 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# coding=utf-8
"""Misc utility constants and classes.
"""
import collections
import Cookie
import contextlib
import datetime
import json
import re
import time
import urllib
import urlparse
import webapp2
from appengine_config import DEBUG
import bs4
from granary import source as gr_source
import mf2py
from oauth_dropins.webutil import handlers as webutil_handlers
from oauth_dropins.webutil.models import StringIdModel
from oauth_dropins.webutil import util
from oauth_dropins.webutil.util import *
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import ndb
# when running in dev_appserver, replace these domains in links with localhost
LOCALHOST_TEST_DOMAINS = frozenset([
('snarfed.org', 'localhost'),
('kylewm.com', 'redwind.dev'),
])
EPOCH = datetime.datetime.utcfromtimestamp(0)
EPOCH_ISO = EPOCH.isoformat()
POLL_TASK_DATETIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
# rate limiting errors. twitter returns 429, instagram 503, google+ 403.
# TODO: facebook. it returns 200 and reports the error in the response.
# https://developers.facebook.com/docs/reference/ads-api/api-rate-limiting/
HTTP_RATE_LIMIT_CODES = frozenset(('403', '429', '503'))
USER_AGENT_HEADER = {'User-Agent': 'Bridgy (https://brid.gy/about)'}
# alias allows unit tests to mock the function
now_fn = datetime.datetime.now
# Domains that don't support webmentions. Mainly just the silos.
# Subdomains are automatically blacklisted too.
#
# We also check this when a user sign up and we extract the web site links from
# their profile. We automatically omit links to these domains.
with open('domain_blacklist.txt') as f:
BLACKLIST = util.load_file_lines(f)
# Individual URLs that we shouldn't fetch. Started because of
# https://github.com/snarfed/bridgy/issues/525 . Hopefully temporary and can be
# removed once https://github.com/idno/Known/issues/1088 is fixed!
URL_BLACKLIST = frozenset((
'http://www.evdemon.org/2015/learning-more-about-quill',
))
# URL paths of users who opt into testing new "beta" features and changes
# before we roll them out to everyone.
with open('beta_users.txt') as f:
BETA_USER_PATHS = util.load_file_lines(f)
# Average HTML page size as of 2015-10-15 is 56K, so this is very generous and
# conservative.
# http://www.sitepoint.com/average-page-weight-increases-15-2014/
# http://httparchive.org/interesting.php#bytesperpage
MAX_HTTP_RESPONSE_SIZE = 500000
# Returned as the HTTP status code when we refuse to make or finish a request.
HTTP_REQUEST_REFUSED_STATUS_CODE = 599
# Unpacked representation of logged in account in the logins cookie.
Login = collections.namedtuple('Login', ('site', 'name', 'path'))
canonicalize_domain = webutil_handlers.redirect(
('brid-gy.appspot.com', 'www.brid.gy'), 'brid.gy')
def add_poll_task(source, now=False, **kwargs):
"""Adds a poll task for the given source entity.
Pass now=True to insert a poll-now task.
Tasks inserted from a backend (e.g. twitter_streaming) are sent to that
backend by default, which doesn't work in the dev_appserver. Setting the
target version to 'default' in queue.yaml doesn't work either, but setting it
here does.
Note the constant. The string 'default' works in dev_appserver, but routes to
default.brid-gy.appspot.com in prod instead of brid.gy, which breaks SSL
because appspot.com doesn't have a third-level wildcard cert.
"""
last_polled_str = source.last_polled.strftime(POLL_TASK_DATETIME_FORMAT)
queue = 'poll-now' if now else 'poll'
task = taskqueue.add(queue_name=queue,
params={'source_key': source.key.urlsafe(),
'last_polled': last_polled_str},
**kwargs)
logging.info('Added %s task %s with args %s', queue, task.name, kwargs)
def add_propagate_task(entity, **kwargs):
"""Adds a propagate task for the given response entity.
"""
task = taskqueue.add(queue_name='propagate',
params={'response_key': entity.key.urlsafe()},
target=taskqueue.DEFAULT_APP_VERSION,
**kwargs)
logging.info('Added propagate task: %s', task.name)
def add_propagate_blogpost_task(entity, **kwargs):
"""Adds a propagate-blogpost task for the given response entity.
"""
task = taskqueue.add(queue_name='propagate-blogpost',
params={'key': entity.key.urlsafe()},
target=taskqueue.DEFAULT_APP_VERSION,
**kwargs)
logging.info('Added propagate-blogpost task: %s', task.name)
def webmention_endpoint_cache_key(url):
"""Returns memcache key for a cached webmention endpoint for a given URL.
Example: 'W https snarfed.org'
"""
domain = util.domain_from_link(url)
scheme = urlparse.urlparse(url).scheme
return ' '.join(('W', scheme, domain))
def email_me(**kwargs):
"""Thin wrapper around mail.send_mail() that handles errors."""
try:
mail.send_mail(sender='admin@brid-gy.appspotmail.com',
to='webmaster@brid.gy', **kwargs)
except BaseException:
logging.warning('Error sending notification email', exc_info=True)
def requests_get(url, **kwargs):
"""Wraps requests.get with extra semantics and our user agent.
If a server tells us a response will be too big (based on Content-Length), we
hijack the response and return 599 and an error response body instead. We pass
stream=True to requests.get so that it doesn't fetch the response body until
we access response.content (or .text).
http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow
"""
if url in URL_BLACKLIST:
resp = requests.Response()
resp.status_code = HTTP_REQUEST_REFUSED_STATUS_CODE
resp._text = resp._content = 'Sorry, Bridgy has blacklisted this URL.'
return resp
kwargs.setdefault('headers', {}).update(USER_AGENT_HEADER)
resp = util.requests_get(url, stream=True, **kwargs)
length = resp.headers.get('Content-Length', 0)
if util.is_int(length) and int(length) > MAX_HTTP_RESPONSE_SIZE:
resp.status_code = HTTP_REQUEST_REFUSED_STATUS_CODE
resp._text = resp._content = ('Content-Length %s is larger than our limit %s.' %
(length, MAX_HTTP_RESPONSE_SIZE))
return resp
def follow_redirects(url, cache=True):
"""Wraps granary.source.follow_redirects and injects our settings.
...specifically memcache and USER_AGENT_HEADER.
"""
return util.follow_redirects(url, cache=memcache if cache else None,
headers=USER_AGENT_HEADER)
def get_webmention_target(url, resolve=True, replace_test_domains=True):
"""Resolves a URL and decides whether we should try to send it a webmention.
Note that this ignores failed HTTP requests, ie the boolean in the returned
tuple will be true! TODO: check callers and reconsider this.
Args:
url: string
resolve: whether to follow redirects
replace_test_domains: whether to replace test user domains with localhost
Returns: (string url, string pretty domain, boolean) tuple. The boolean is
True if we should send a webmention, False otherwise, e.g. if it's a bad
URL, not text/html, or in the blacklist.
"""
url = util.clean_url(url)
try:
domain = domain_from_link(url).lower()
except BaseException:
logging.info('Dropping bad URL %s.', url)
return url, None, False
send = True
if resolve:
# this follows *all* redirects, until the end
resolved = follow_redirects(url, cache=memcache)
send = resolved.headers.get('content-type', '').startswith('text/html')
url, domain, _ = get_webmention_target(
resolved.url, resolve=False, replace_test_domains=replace_test_domains)
send = send and domain and not in_webmention_blacklist(domain)
if replace_test_domains:
url = replace_test_domains_with_localhost(url)
return url, domain, send
def in_webmention_blacklist(domain):
"""Returns True if the domain or its root domain is in BLACKLIST."""
return util.domain_or_parent_in(domain.lower(), BLACKLIST)
def prune_activity(activity, source):
"""Prunes an activity down to just id, url, content, to, and object, in place.
If the object field exists, it's pruned down to the same fields. Any fields
duplicated in both the activity and the object are removed from the object.
Note that this only prunes the to field if it says the activity is public,
since granary's Source.is_public() defaults to saying an activity is
public if the to field is missing. If that ever changes, we'll need to
start preserving the to field here.
Args:
activity: ActivityStreams activity dict
Returns: pruned activity dict
"""
keep = ['id', 'url', 'content', 'fb_id', 'fb_object_id', 'fb_object_type']
if not source.is_activity_public(activity):
keep += ['to']
pruned = {f: activity.get(f) for f in keep}
obj = activity.get('object')
if obj:
obj = pruned['object'] = prune_activity(obj, source)
for k, v in obj.items():
if pruned.get(k) == v:
del obj[k]
return trim_nulls(pruned)
def prune_response(response):
"""Returns a response object dict with a few fields removed.
Args:
response: ActivityStreams response object
Returns: pruned response object
"""
obj = response.get('object')
if obj:
response['object'] = prune_response(obj)
drop = ['activity', 'mentions', 'originals', 'replies', 'tags']
return trim_nulls({k: v for k, v in response.items() if k not in drop})
def replace_test_domains_with_localhost(url):
"""Replace domains in LOCALHOST_TEST_DOMAINS with localhost for local
testing when in DEBUG mode.
Args:
url: a string
Returns: a string with certain well-known domains replaced by localhost
"""
if url and DEBUG:
for test_domain, local_domain in LOCALHOST_TEST_DOMAINS:
url = re.sub('https?://' + test_domain,
'http://' + local_domain, url)
return url
class Handler(webapp2.RequestHandler):
"""Includes misc request handler utilities.
Attributes:
messages: list of notification messages to be rendered in this page or
wherever it redirects
"""
def __init__(self, *args, **kwargs):
super(Handler, self).__init__(*args, **kwargs)
self.messages = set()
def redirect(self, uri, **kwargs):
"""Adds self.messages to the fragment, separated by newlines.
"""
parts = list(urlparse.urlparse(uri))
if self.messages and not parts[5]: # parts[5] is fragment
parts[5] = '!' + urllib.quote('\n'.join(self.messages).encode('utf-8'))
uri = urlparse.urlunparse(parts)
super(Handler, self).redirect(uri, **kwargs)
def maybe_add_or_delete_source(self, source_cls, auth_entity, state, **kwargs):
"""Adds or deletes a source if auth_entity is not None.
Used in each source's oauth-dropins CallbackHandler finish() and get()
methods, respectively.
Args:
source_cls: source class, e.g. Instagram
auth_entity: ouath-dropins auth entity
state: string, OAuth callback state parameter. a JSON serialized dict
with operation, feature, and an optional callback URL. For deletes,
it will also include the source key
kwargs: passed through to the source_cls constructor
Returns:
source entity if it was created or updated, otherwise None
"""
state_obj = self.decode_state_parameter(state)
operation = state_obj.get('operation', 'add')
feature = state_obj.get('feature')
callback = state_obj.get('callback')
user_url = state_obj.get('user_url')
logging.debug(
'maybe_add_or_delete_source with operation=%s, feature=%s, callback=%s',
operation, feature, callback)
if operation == 'add': # this is an add/update
if not auth_entity:
if not self.messages:
self.messages.add("OK, you're not signed up. Hope you reconsider!")
if callback:
callback = util.add_query_params(callback, {'result': 'declined'})
logging.debug(
'user declined adding source, redirect to external callback %s',
callback)
# call super.redirect so the callback url is unmodified
super(Handler, self).redirect(callback.encode('utf-8'))
else:
self.redirect('/')
return
CachedPage.invalidate('/users')
logging.info('%s.create_new with %s', source_cls.__class__.__name__,
(auth_entity.key, state, kwargs))
source = source_cls.create_new(self, auth_entity=auth_entity,
features=feature.split(',') if feature else [],
user_url=user_url, **kwargs)
if source:
# add to login cookie
logins = self.get_logins()
logins.append(Login(path=source.bridgy_path(), site=source.SHORT_NAME,
name=source.label_name()))
self.set_logins(logins)
if callback:
callback = util.add_query_params(callback, {
'result': 'success',
'user': source.bridgy_url(self),
'key': source.key.urlsafe(),
} if source else {'result': 'failure'})
logging.debug(
'finished adding source, redirect to external callback %s', callback)
# call super.redirect so the callback url is unmodified
super(Handler, self).redirect(callback.encode('utf-8'))
else:
self.redirect(source.bridgy_url(self) if source else '/')
return source
else: # this is a delete
if auth_entity:
self.redirect('/delete/finish?auth_entity=%s&state=%s' %
(auth_entity.key.urlsafe(), state))
else:
self.messages.add('If you want to disable, please approve the %s prompt.' %
source_cls.GR_CLASS.NAME)
self.redirect_home_or_user_page(state)
def construct_state_param_for_add(self, state=None, **kwargs):
"""Construct the state parameter if one isn't explicitly passed in.
"""
state_obj = self.decode_state_parameter(state)
if not state_obj:
state_obj = {field: self.request.get(field) for field in
('callback', 'feature', 'id', 'user_url')}
state_obj['operation'] = 'add'
if kwargs:
state_obj.update(kwargs)
return self.encode_state_parameter(state_obj)
def encode_state_parameter(self, obj):
"""The state parameter is passed to various source authorization
endpoints and returned in a callback. This encodes a JSON object
so that it can be safely included as a query string parameter.
The following keys are common:
- operation: 'add' or 'delete'
- feature: 'listen', 'publish', or 'webmention'
- callback: an optional external callback, that we will redirect to at
the end of the authorization handshake
- source: the source key, only applicable to deletes
Args:
obj: a JSON-serializable dict
Returns: a string
"""
# pass in custom separators to cut down on whitespace, and sort keys for
# unit test consistency
return urllib.quote_plus(json.dumps(trim_nulls(obj), separators=(',', ':'),
sort_keys=True))
def decode_state_parameter(self, state):
"""Decodes a state parameter encoded by encode_state_parameter.
See encode_state_parameter for a list of common state parameter keys.
Args:
state: a string (JSON-serialized dict)
Returns: a dict containing operation, feature, and possibly other fields
"""
logging.debug('decoding state "%s"' % state)
obj = json.loads(urllib.unquote_plus(state)) if state else {}
if not isinstance(obj, dict):
logging.error('got a non-dict state parameter %s', state)
return {}
return obj
def get_logins(self):
"""Extracts the current user page paths from the logins cookie.
Returns: list of Login objects
"""
cookie = self.request.headers.get('Cookie', '')
if cookie:
logging.info('Cookie: %s', cookie)
try:
logins_str = Cookie.SimpleCookie(cookie).get('logins')
except Cookie.CookieError, e:
logging.warning("Bad cookie: %s", e)
return []
if not logins_str or not logins_str.value:
return []
logins = []
for val in set(urllib.unquote_plus(logins_str.value).decode('utf-8').split('|')):
path, name = val.split('?')
site, _ = path.strip('/').split('/')
logins.append(Login(path=path, site=site, name=name))
return logins
def set_logins(self, logins):
"""Sets a logins cookie.
Args:
logins: sequence of Login objects
"""
# cookie docs: http://curl.haxx.se/rfc/cookie_spec.html
cookie = Cookie.SimpleCookie()
cookie['logins'] = '|'.join(sorted(set(
'%s?%s' % (login.path, urllib.quote_plus(login.name.encode('utf-8')))
for login in logins)))
cookie['logins']['path'] = '/'
cookie['logins']['expires'] = now_fn() + datetime.timedelta(days=365 * 2)
header = cookie['logins'].OutputString()
logging.info('Set-Cookie: %s', header)
self.response.headers['Set-Cookie'] = header
def redirect_home_or_user_page(self, state):
redirect_to = '/'
split = state.split('-', 1)
if len(split) >= 2:
source = ndb.Key(urlsafe=split[1]).get()
if source:
redirect_to = source.bridgy_url(self)
self.redirect(redirect_to)
def preprocess_source(self, source):
"""Prepares a source entity for rendering in the source.html template.
- use id as name if name isn't provided
- convert image URLs to https if we're serving over SSL
- set 'website_links' attr to list of pretty HTML links to domain_urls
Args:
source: Source entity
"""
if not source.name:
source.name = source.key.string_id()
if source.picture:
source.picture = util.update_scheme(source.picture, self)
source.website_links = [
util.pretty_link(url, attrs={'rel': 'me', 'class': 'u-url'})
for url in source.domain_urls]
return source
def oauth_starter(oauth_start_handler, **kwargs):
"""Returns an oauth-dropins start handler that injects the state param.
Args:
oauth_start_handler: oauth-dropins StartHandler to use,
e.g. oauth_dropins.twitter.StartHandler.
kwargs: passed to construct_state_param_for_add()
"""
class StartHandler(oauth_start_handler, Handler):
def redirect_url(self, state=None):
return super(StartHandler, self).redirect_url(
self.construct_state_param_for_add(state, **kwargs))
return StartHandler
class CachedPage(StringIdModel):
"""Cached HTML for pages that changes rarely. Key id is path.
Stored in the datastore since datastore entities in memcache (mostly
Responses) are requested way more often, so it would get evicted
out of memcache easily.
Keys, useful for deleting from memcache:
/: aglzfmJyaWQtZ3lyEQsSCkNhY2hlZFBhZ2UiAS8M
/users: aglzfmJyaWQtZ3lyFgsSCkNhY2hlZFBhZ2UiBi91c2Vycww
"""
html = ndb.TextProperty()
expires = ndb.DateTimeProperty()
@classmethod
def load(cls, path):
cached = CachedPage.get_by_id(path)
if cached:
if cached.expires and now_fn() > cached.expires:
logging.info('Deleting expired cached page for %s', path)
cached.key.delete()
return None
else:
logging.info('Found cached page for %s', path)
return cached
@classmethod
def store(cls, path, html, expires=None):
"""path and html are strings, expires is a datetime.timedelta."""
logging.info('Storing new page in cache for %s', path)
if expires is not None:
logging.info(' (expires in %s)', expires)
expires = now_fn() + expires
CachedPage(id=path, html=html, expires=expires).put()
@classmethod
def invalidate(cls, path):
logging.info('Deleting cached page for %s', path)
CachedPage(id=path).key.delete()
def unwrap_t_umblr_com(url):
"""If url is a t.umblr.com short link, extract its destination URL.
Otherwise, return url unchanged.
Not in tumblr.py since models imports superfeedr, so it would be a circular
import.
Background: https://github.com/snarfed/bridgy/issues/609
"""
parsed = urlparse.urlparse(url)
return (urlparse.parse_qs(parsed.query).get('z', [''])[0]
if parsed.netloc == 't.umblr.com'
else url)
@contextlib.contextmanager
def cache_time(label, size=None):
"""Times a block of code, logs the time, and aggregates it in memcache."""
start = int(time.clock() * 1000)
yield
elapsed = int(time.clock() * 1000) - start
logging.info('Parse time for %s: %dms', label, elapsed)
memcache.incr('timed %s' % label, elapsed, initial_value=0)
if size:
memcache.incr('timed %s size' % label, size, initial_value=0)
def beautifulsoup_parse(html):
"""Parses an HTML string with BeautifulSoup. Centralizes our parsing config.
We currently let BeautifulSoup default to lxml, which is the fastest option.
http://www.crummy.com/software/BeautifulSoup/bs4/doc/#specifying-the-parser-to-use
We use App Engine's lxml by declaring it in app.yaml.
"""
# instrumenting, disabled for now:
# with cache_time('beautifulsoup', len(html)):
return bs4.BeautifulSoup(html)
def mf2py_parse(input, url):
"""Uses mf2py to parse an input HTML string or BeautifulSoup input."""
if isinstance(input, basestring):
input = beautifulsoup_parse(input)
# instrumenting, disabled for now:
# with cache_time('mf2py', 1):
return mf2py.parse(url=url, doc=input)