-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpublish.py
594 lines (486 loc) · 20.7 KB
/
publish.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
"""Publishes webmentions into the silos.
Webmention spec: http://webmention.org/
Bridgy request and response details: https://brid.gy/about#response
Example request:
POST /webmention HTTP/1.1
Host: brid.gy
Content-Type: application/x-www-url-form-encoded
source=http://bob.host/post-by-bob&
target=http://facebook.com/123
Example response:
HTTP/1.1 201 Created
Location: http://facebook.com/456_789
{
"url": "http://facebook.com/456_789",
"type": "post",
"id": "456_789"
}
"""
__author__ = ['Ryan Barrett <bridgy@ryanb.org>']
import collections
import logging
import json
import mf2py
import pprint
import urlparse
import appengine_config
from appengine_config import HTTP_TIMEOUT
from bs4 import BeautifulSoup
from facebook import FacebookPage
from flickr import Flickr
from googleplus import GooglePlusPage
from granary import microformats2
from granary import source as gr_source
from instagram import Instagram
from models import Publish, PublishedPage
from oauth_dropins import facebook as oauth_facebook
from oauth_dropins import flickr as oauth_flickr
from oauth_dropins import instagram as oauth_instagram
from oauth_dropins import twitter as oauth_twitter
from twitter import Twitter
import models
import requests
import util
import webapp2
import webmention
from google.appengine.ext import ndb
from google.appengine.ext.webapp import template
SOURCES = (FacebookPage, Flickr, Twitter, Instagram, GooglePlusPage)
SOURCE_NAMES = {cls.SHORT_NAME: cls for cls in SOURCES}
SOURCE_DOMAINS = {cls.GR_CLASS.DOMAIN: cls for cls in SOURCES}
class Handler(webmention.WebmentionHandler):
"""Base handler for both previews and publishes.
Subclasses must set the PREVIEW attribute to True or False. They may also
override other methods.
Attributes:
fetched: requests.Response from fetching source_url
shortlink: rel-shortlink found in the original post, if any
"""
PREVIEW = None
shortlink = None
def authorize(self):
"""Returns True if the current user is authorized for this request.
Otherwise, should call self.error() to provide an appropriate error message.
"""
return True
def source_url(self):
return util.get_required_param(self, 'source')
def target_url(self):
return util.get_required_param(self, 'target')
def omit_link(self, item):
return self._bool_option('bridgy_omit_link', item)
def ignore_formatting(self, item):
return self._bool_option('bridgy_ignore_formatting', item)
def _bool_option(self, param, item):
val = self.request.get(param, None)
return (val.lower() in ('', 'true') if val is not None
else param.replace('_', '-') in item.get('properties', {}))
def maybe_inject_silo_content(self, item):
props = item.setdefault('properties', {})
silo_content = props.get('bridgy-%s-content' % self.source.SHORT_NAME, [])
if silo_content:
props['content'] = silo_content
props.pop('name', None)
props.pop('summary', None)
def _run(self):
"""Returns CreationResult on success, None otherwise."""
logging.info('Params: %s', self.request.params.items())
assert self.PREVIEW in (True, False)
# parse and validate target URL
try:
parsed = urlparse.urlparse(self.target_url())
except BaseException:
return self.error('Could not parse target URL %s' % self.target_url())
domain = parsed.netloc
path_parts = parsed.path.rsplit('/', 1)
source_cls = SOURCE_NAMES.get(path_parts[-1])
if (domain not in ('brid.gy', 'www.brid.gy', 'localhost:8080') or
len(path_parts) != 2 or path_parts[0] != '/publish' or not source_cls):
return self.error(
'Target must be brid.gy/publish/{facebook,flickr,twitter,instagram}')
elif source_cls == GooglePlusPage:
return self.error('Sorry, %s is not yet supported.' %
source_cls.GR_CLASS.NAME)
# resolve source URL
url, domain, ok = util.get_webmention_target(self.source_url())
# show nice error message if they're trying to publish a silo post
if domain in SOURCE_DOMAINS:
return self.error(
"Looks like that's a %s URL. Try one from your web site instead!" %
SOURCE_DOMAINS[domain].GR_CLASS.NAME)
elif not ok:
return self.error('Unsupported source URL %s' % url)
elif not domain:
return self.error('Could not parse source URL %s' % url)
# look up source by domain
domain = domain.lower()
sources = source_cls.query().filter(source_cls.domains == domain).fetch(100)
if not sources:
return self.error("Could not find <b>%(type)s</b> account for <b>%(domain)s</b>. Check that your %(type)s profile has %(domain)s in its <em>web site</em> or <em>link</em> field, then try signing up again." %
{'type': source_cls.GR_CLASS.NAME, 'domain': domain})
for source in sources:
logging.info('Source: %s , features %s, status %s, po
8000
ll status %s',
source.bridgy_url(self), source.features, source.status,
source.poll_status)
if source.status != 'disabled' and 'publish' in source.features:
self.source = source
break
else:
return self.error(
'Publish is not enabled for your account(s). Please visit %s and sign up!' %
' or '.join(s.bridgy_url(self) for s in sources))
content_param = 'bridgy_%s_content' % self.source.SHORT_NAME
if content_param in self.request.params:
return self.error('The %s parameter is not supported' % content_param)
# show nice error message if they're trying to publish their home page
for domain_url in self.source.domain_urls:
domain_url_parts = urlparse.urlparse(domain_url)
source_url_parts = urlparse.urlparse(self.source_url())
if (source_url_parts.netloc == domain_url_parts.netloc and
source_url_parts.path.strip('/') == domain_url_parts.path.strip('/') and
not source_url_parts.query):
return self.error(
"Looks like that's your home page. Try one of your posts instead!")
# done with the sanity checks, ready to fetch the source url. create the
# Publish entity so we can store the result.
entity = self.get_or_add_publish_entity(url)
if (entity.status == 'complete' and entity.type != 'preview' and
not self.PREVIEW and not appengine_config.DEBUG):
return self.error("Sorry, you've already published that page, and Bridgy Publish doesn't yet support updating or deleting existing posts. Ping Ryan if you want that feature!")
self.entity = entity
# fetch source page
resp = self.fetch_mf2(url)
if not resp:
return
self.fetched, data = resp
# find rel-shortlink, if any
# http://microformats.org/wiki/rel-shortlink
# https://github.com/snarfed/bridgy/issues/173
soup = BeautifulSoup(self.fetched.text)
shortlinks = (soup.find_all('link', rel='shortlink') +
soup.find_all('a', rel='shortlink') +
soup.find_all('a', class_='shortlink'))
if shortlinks:
self.shortlink = shortlinks[0]['href']
# loop through each item and its children and try to preview/create it. if
# it fails, try the next one. break after the first one that works.
resp = None
types = set()
queue = collections.deque(data.get('items', []))
while queue:
item = queue.popleft()
item_types = set(item.get('type'))
if 'h-feed' in item_types and 'h-entry' not in item_types:
queue.extend(item.get('children', []))
continue
try:
result = self.attempt_single_item(item)
if self.entity.published:
break
if result.abort:
if result.error_plain:
self.error(result.error_plain, html=result.error_html, data=item)
return
# try the next item
for embedded in ('rsvp', 'invitee', 'repost', 'repost-of', 'like',
'like-of', 'in-reply-to'):
if embedded in item.get('properties', []):
item_types.add(embedded)
logging.info(
'Object type(s) %s not supported; error=%s; trying next.',
item_types, result.error_plain)
types = types.union(item_types)
queue.extend(item.get('children', []))
except BaseException, e:
code, body = util.interpret_http_exception(e)
return self.error('Error: %s %s' % (body or '', e), status=code or 500,
mail=True)
if not self.entity.published: # tried all the items
types.discard('h-entry')
types.discard('h-note')
if types:
msg = ("%s doesn't support type(s) %s, or no content was found." %
(source_cls.GR_CLASS.NAME, ' + '.join(types)))
else:
msg = 'Could not find content in <a href="http://microformats.org/wiki/h-entry">h-entry</a> or any other element!'
return self.error(msg, data=data)
# write results to datastore
self.entity.status = 'complete'
self.entity.put()
return result
def attempt_single_item(self, item):
"""Attempts to preview or publish a single mf2 item.
Args:
item: mf2 item dict from mf2py
Returns:
CreationResult
"""
self.maybe_inject_silo_content(item)
obj = microformats2.json_to_object(item)
ignore_formatting = self.ignore_formatting(item)
if ignore_formatting:
prop = microformats2.first_props(item.get('properties', {}))
content = microformats2.get_text(prop.get('content'))
if content:
obj['content'] = content.strip()
# which original post URL to include? in order of preference:
# 1. rel-shortlink (background: https://github.com/snarfed/bridgy/issues/173)
# 2. original user-provided URL if it redirected
# 3. u-url if available
# 4. actual final fetched URL
if self.shortlink:
obj['url'] = self.shortlink
elif self.source_url() != self.fetched.url:
obj['url'] = self.source_url()
elif 'url' not in obj:
obj['url'] = self.fetched.url
logging.debug('Converted to ActivityStreams object: %s', json.dumps(obj, indent=2))
# posts and comments need content
obj_type = obj.get('objectType')
if obj_type in ('note', 'article', 'comment'):
if (not obj.get('content') and not obj.get('summary') and
not obj.get('displayName')):
return gr_source.creation_result(
abort=False,
error_plain='Could not find content in %s' % self.fetched.url,
error_html='Could not find <a href="http://microformats.org/">content</a> in %s' % self.fetched.url)
self.preprocess(obj)
omit_link = self.omit_link(item)
if not self.authorize():
return gr_source.creation_result(abort=True)
# RIP Facebook comments/likes. https://github.com/snarfed/bridgy/issues/350
if (isinstance(self.source, FacebookPage) and
(obj_type == 'comment' or obj.get('verb') == 'like')):
return gr_source.creation_result(
abort=True,
error_plain='Facebook comments and likes are no longer supported. :(',
error_html='<a href="https://github.com/snarfed/bridgy/issues/350">'
'Facebook comments and likes are no longer supported.</a> :(')
if self.PREVIEW:
result = self.source.gr_source.preview_create(
obj, include_link=not omit_link, ignore_formatting=ignore_formatting)
self.entity.published = result.content or result.description
if not self.entity.published:
return result # there was an error
state = {
'source_key': self.source.key.urlsafe(),
'source_url': self.source_url(),
'target_url': self.target_url(),
'bridgy_omit_link': omit_link,
}
vars = {'source': self.preprocess_source(self.source),
'preview': result.content,
'description': result.description,
'webmention_endpoint': self.request.host_url + '/publish/webmention',
'state': self.encode_state_parameter(state),
}
vars.update(state)
logging.info('Rendering preview with template vars %s', pprint.pformat(vars))
return gr_source.creation_result(
template.render('templates/preview.html', vars))
else:
result = self.source.gr_source.create(obj, include_link=not omit_link,
ignore_formatting=ignore_formatting)
self.entity.published = result.content
if not result.content:
return result # there was an error
if 'url' not in self.entity.published:
self.entity.published['url'] = obj.get('url')
self.entity.type = self.entity.published.get('type') or models.get_type(obj)
self.entity.type_label = self.source.TYPE_LABELS.get(self.entity.type)
self.response.headers['Content-Type'] = 'application/json'
logging.info('Returning %s', json.dumps(self.entity.published, indent=2))
self.response.headers['Location'] = self.entity.published['url'].encode('utf-8')
self.response.status = 201
return gr_source.creation_result(
json.dumps(self.entity.published, indent=2))
def preprocess(self, activity):
"""Preprocesses an item before trying to publish it.
Specifically:
* Expands inReplyTo/object URLs with rel=syndication URLs.
Args:
activity: an ActivityStreams activity or object being published
"""
self.source.preprocess_for_publish(activity)
self.expand_target_urls(activity)
def expand_target_urls(self, activity):
"""Expand the inReplyTo or object fields of an ActivityStreams object
by fetching the original and looking for rel=syndication URLs.
This method modifies the dict in place.
Args:
activity: an ActivityStreams dict of the activity being published
"""
for field in ('inReplyTo', 'object'):
# microformats2.json_to_object de-dupes, no need to do it here
objs = activity.get(field)
if not objs:
continue
if isinstance(objs, dict):
objs = [objs]
augmented = list(objs)
for obj in objs:
url = obj.get('url')
if not url:
continue
# get_webmention_target weeds out silos and non-HTML targets
# that we wouldn't want to download and parse
url, _, ok = util.get_webmention_target(url)
if not ok:
continue
# fetch_mf2 raises a fuss if it can't fetch a mf2 document;
# easier to just grab this ourselves than add a bunch of
# special-cases to that method
logging.debug('expand_target_urls fetching field=%s, url=%s', field, url)
try:
resp = util.requests_get(url)
resp.raise_for_status()
data = mf2py.Parser(url=url, doc=resp.text).to_dict()
except AssertionError:
raise # for unit tests
except BaseException:
# it's not a big deal if we can't fetch an in-reply-to url
logging.warning('expand_target_urls could not fetch field=%s, url=%s',
field, url, exc_info=True)
continue
synd_urls = data.get('rels', {}).get('syndication', [])
# look for syndication urls in the first h-entry
queue = collections.deque(data.get('items', []))
while queue:
item = queue.popleft()
item_types = set(item.get('type', []))
if 'h-feed' in item_types and 'h-entry' not in item_types:
queue.extend(item.get('children', []))
continue
# these can be urls or h-cites
synd_urls += microformats2.get_string_urls(
item.get('properties', {}).get('syndication', []))
logging.debug('expand_target_urls found rel=syndication for url=%s: %r', url, synd_urls)
augmented += [{'url': u} for u in synd_urls]
activity[field] = augmented
@ndb.transactional
def get_or_add_publish_entity(self, source_url):
"""Creates and stores Publish and (if necessary) PublishedPage entities.
Args:
source_url: string
"""
page = PublishedPage.get_or_insert(source_url)
entity = Publish.query(
Publish.status == 'complete', Publish.type != 'preview',
Publish.source == self.source.key,
ancestor=page.key).get()
if entity is None:
entity = Publish(parent=page.key, source=self.source.key)
if self.PREVIEW:
entity.type = 'preview'
entity.put()
logging.debug('Publish entity: %s', entity.key.urlsafe())
return entity
class PreviewHandler(Handler):
"""Renders a preview HTML snippet of how a webmention would be handled.
"""
PREVIEW = True
def post(self):
result = self._run()
if result and result.content:
self.response.write(result.content)
def authorize(self):
from_source = ndb.Key(urlsafe=util.get_required_param(self, 'source_key'))
if from_source != self.source.key:
self.error('Try publishing that page from <a href="%s">%s</a> instead.' %
(self.source.bridgy_path(), self.source.label()))
return False
return True
def omit_link(self, item):
# always use query param because there's a checkbox in the UI
return self.request.get('bridgy_omit_link') in ('', 'true')
def error(self, error, html=None, status=400, data=None, mail=False):
logging.warning(error, exc_info=True)
self.response.set_status(status)
error = html if html else util.linkify(error)
self.response.write(error)
if mail:
self.mail_me(error)
class SendHandler(Handler):
"""Interactive publish handler. Redirected to after each silo's OAuth dance.
Note that this is GET, not POST, since HTTP redirects always GET.
"""
PREVIEW = False
def finish(self, auth_entity, state=None):
self.state = self.decode_state_parameter(state)
if not state:
self.error('If you want to publish or preview, please approve the prompt.')
return self.redirect('/')
source = ndb.Key(urlsafe=self.state['source_key']).get()
if auth_entity is None:
self.error('If you want to publish or preview, please approve the prompt.')
elif auth_entity.key != source.auth_entity:
self.error('Please log into %s as %s to publish that page.' %
(source.GR_CLASS.NAME, source.name))
else:
result = self._run()
if result and result.content:
self.messages.add('Done! <a href="%s">Click here to view.</a>' %
self.entity.published.get('url'))
# otherwise error() added an error message
return self.redirect(source.bridgy_url(self))
def source_url(self):
return self.state['source_url']
def target_url(self):
return self.state['target_url']
def omit_link(self, item):
return self.state['bridgy_omit_link']
def error(self, error, html=None, status=400, data=None, mail=False):
logging.warning(error, exc_info=True)
error = html if html else util.linkify(error)
self.messages.add('%s' % error)
if mail:
self.mail_me(error)
# We want CallbackHandler.get() and SendHandler.finish(), so put
# CallbackHandler first and override finish.
class FacebookSendHandler(oauth_facebook.CallbackHandler, SendHandler):
finish = SendHandler.finish
class FlickrSendHandler(oauth_flickr.CallbackHandler, SendHandler):
finish = SendHandler.finish
class TwitterSendHandler(oauth_twitter.CallbackHandler, SendHandler):
finish = SendHandler.finish
# Instagram only allows a single OAuth callback URL, so that's handled in
# instagram.py and it redirects here for publishes.
class InstagramSendHandler(SendHandler):
def get(self):
auth_entity = self.request.get('auth_entity') or None
if auth_entity:
auth_entity = ndb.Key(urlsafe=auth_entity).get()
return self.finish(auth_entity, self.request.get('state'))
class WebmentionHandler(Handler):
"""Accepts webmentions and translates them to publish requests.
"""
PREVIEW = False
def post(self):
result = self._run()
if result:
self.response.write(result.content)
def authorize(self):
"""Check for a backlink to brid.gy/publish/SILO."""
bases = set()
if util.domain_from_link(self.request.host_url) == 'brid.gy':
bases.add('brid.gy')
bases.add('www.brid.gy') # also accept www
else:
bases.add(self.request.host_url)
expected = ['%s/publish/%s' % (base, self.source.SHORT_NAME) for base in bases]
if self.entity.html:
for url in expected:
if url in self.entity.html:
return True
self.error("Couldn't find link to %s" % expected[0])
return False
application = webapp2.WSGIApplication([
('/publish/preview', PreviewHandler),
('/publish/webmention', WebmentionHandler),
('/publish/(facebook|twitter|instagram)', webmention.WebmentionGetHandler),
('/publish/facebook/finish', FacebookSendHandler),
('/publish/flickr/finish', FlickrSendHandler),
('/publish/instagram/finish', InstagramSendHandler),
('/publish/twitter/finish', TwitterSendHandler),
],
debug=appengine_config.DEBUG)