How to use the granary.source.INCLUDE_LINK function in granary

To help you get started, we’ve selected a few granary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github snarfed / bridgy / tests / test_publish.py View on Github external
def setUp(self):
    super(PublishTest, self).setUp()
    publish.SOURCE_NAMES['fake'] = testutil.FakeSource
    publish.SOURCE_DOMAINS['fa.ke'] = testutil.FakeSource

    self.auth_entity = testutil.FakeAuthEntity(id='0123456789')
    self.source = testutil.FakeSource(
      id='foo.com', features=['publish'], domains=['foo.com'],
      domain_urls=['http://foo.com/'], auth_entity=self.auth_entity.key)
    self.source.put()

    self.oauth_state = {
      'source_url': 'http://foo.com/bar',
      'target_url': 'https://brid.gy/publish/fake',
      'source_key': self.source.key.urlsafe().decode(),
      'include_link': gr_source.INCLUDE_LINK,
    }
    self.post_html = '<article class="h-entry"><p class="e-content">%s</p></article>'
    self.backlink = '\n<a href="http://localhost/publish/fake"></a>'
github snarfed / bridgy / tests / testutil.py View on Github external
def preview_create(self, obj, include_link=gr_source.OMIT_LINK,
                     ignore_formatting=False):
    if obj.get('verb') == 'like':
      return gr_source.creation_result(
        abort=True, error_plain='Cannot publish likes',
        error_html='Cannot publish likes')

    content = self._content_for_create(obj, ignore_formatting=ignore_formatting)
    if include_link == gr_source.INCLUDE_LINK:
        content += ' - %s' % obj['url']

    content = 'preview of ' + content

    images = self._images(obj)
    if images:
      content += ' with images %s' % ','.join(images)

    return gr_source.creation_result(description=content)
github snarfed / bridgy / tests / testutil.py View on Github external
error_html='Cannot publish likes')
    if 'content' not in obj:
      return gr_source.creation_result(
        abort=False, error_plain='No content',
        error_html='No content')

    if type == 'comment':
      base_url = self.base_object(obj).get('url')
      if not base_url:
        return gr_source.creation_result(
          abort=True,
          error_plain='no %s url to reply to' % self.DOMAIN,
          error_html='no %s url to reply to' % self.DOMAIN)

    content = self._content_for_create(obj, ignore_formatting=ignore_formatting)
    if include_link == gr_source.INCLUDE_LINK:
        content += ' - %s' % obj['url']
    ret = {
      'id': 'fake id',
      'url': 'http://fake/url',
      'content': content,
      'granary_message': 'granary message',
    }
    if verb == 'rsvp-yes':
      ret['type'] = 'post'

    images = self._images(obj)
    if images:
      ret['images'] = images

    return gr_source.creation_result(ret)
github snarfed / bridgy / tests / test_publish.py View on Github external
'PreviewHandler preview new'):
      error_reporting_client.report(subject, http_context=mox.IgnoreArg(),
                                    user=u'http://localhost/fake/foo.com')

    self.mox.StubOutWithMock(self.source.gr_source, 'create',
                             use_mock_anything=True)
    err = exc.HTTPPaymentRequired('fooey')
    self.source.gr_source.create(mox.IgnoreArg(),
                                 include_link=gr_source.INCLUDE_LINK,
                                 ignore_formatting=False
                                 ).AndRaise(err)

    self.mox.StubOutWithMock(self.source.gr_source, 'preview_create',
                             use_mock_anything=True)
    self.source.gr_source.preview_create(mox.IgnoreArg(),
                                         include_link=gr_source.INCLUDE_LINK,
                                         ignore_formatting=False
                                         ).AndRaise(err)

    self.mox.ReplayAll()
    self.assert_error('fooey', status=402)
    self.assertEqual(402, self.get_response(preview=True).status_int)
github snarfed / granary / granary / github.py View on Github external
'like', 'tag'):
      return source.creation_result(
        abort=False, error_plain='Cannot publish %s to GitHub' % type)

    base_obj = self.base_object(obj)
    base_url = base_obj.get('url')
    if not base_url:
      return source.creation_result(
        abort=True,
        error_plain='You need an in-reply-to GitHub repo, issue, PR, or comment URL.')

    content = orig_content = replace_code_placeholders(html.escape(
      self._content_for_create(obj, ignore_formatting=ignore_formatting),
      quote=False))
    url = obj.get('url')
    if include_link == source.INCLUDE_LINK and url:
      content += '\n\n(Originally published at: %s)' % url

    parsed = urllib.parse.urlparse(base_url)
    path = parsed.path.strip('/').split('/')
    owner, repo = path[:2]
    if len(path) == 4:
      number = path[3]

    comment_id = re.match(r'^issuecomment-([0-9]+)$', parsed.fragment)
    if comment_id:
      comment_id = comment_id.group(1)
    elif parsed.fragment:
      return source.creation_result(
        abort=True,
        error_plain='Please remove the fragment #%s from your in-reply-to URL.' %
          parsed.fragment)
github snarfed / bridgy / publish.py View on Github external
def include_link(self, item):
    val = self.request.get('bridgy_omit_link', None)

    if val is None:
      # _run has already parsed and validated the target URL
      vals = urlparse.parse_qs(urlparse.urlparse(self.target_url()).query)\
                     .get('bridgy_omit_link')
      val = vals[0] if vals else None

    if val is None:
      vals = item.get('properties', {}).get('bridgy-omit-link')
      val = vals[0] if vals else None

    result = (gr_source.INCLUDE_LINK if val is None or val.lower() == 'false'
              else gr_source.INCLUDE_IF_TRUNCATED if val.lower() == 'maybe'
              else gr_source.OMIT_LINK)

    return result
github snarfed / granary / granary / facebook.py View on Github external
if type == 'activity':
        content = verb
      else:
        return source.creation_result(
          abort=False,  # keep looking for things to post
          error_plain='No content text found.',
          error_html='No content text found.')

    name = obj.get('displayName')
    if name and mf2util.is_name_a_title(name, content):
        content = name + u"\n\n" + content

    people = self._get_person_tags(obj)

    url = obj.get('url')
    if include_link == source.INCLUDE_LINK and url:
      content += '\n\n(Originally published at: %s)' % url
    preview_content = util.linkify(content)
    if video_url:
      preview_content += ('<br><br><video src="%s" controls=""><a href="%s">'
                          'this video</a></video>' % (video_url, video_url))
    elif image_url:
      preview_content += '<br><br><img src="%s">' % image_url
    if people:
      preview_content += '<br><br><em>with %s</em>' % ', '.join(
        '<a href="%s">%s</a>' % (
          tag.get('url'), tag.get('displayName') or 'User %s' % tag['id'])
        for tag in people)
    msg_data = collections.OrderedDict({'message': content.encode('utf-8')})

    if type == 'comment':
      if not base_url:
github snarfed / bridgy / publish.py View on Github external
def include_link(self, item):
    val = self.request.get('bridgy_omit_link', None)

    if val is None:
      # _run has already parsed and validated the target URL
      vals = urllib.parse.parse_qs(urllib.parse.urlparse(self.target_url()).query)\
                     .get('bridgy_omit_link')
      val = vals[0] if vals else None

    if val is None:
      vals = item.get('properties', {}).get('bridgy-omit-link')
      val = vals[0] if vals else None

    result = (gr_source.INCLUDE_LINK if val is None or val.lower() == 'false'
              else gr_source.INCLUDE_IF_TRUNCATED if val.lower() == 'maybe'
              else gr_source.OMIT_LINK)

    return result
github snarfed / granary / granary / twitter.py View on Github external
"""
    if type == 'article':
      format = brevity.FORMAT_ARTICLE
    else:
      format = brevity.FORMAT_NOTE

    target_length = MAX_TWEET_LENGTH
    if quote_tweet:
      target_length -= (TCO_LENGTH + 1)

    truncated = brevity.shorten(
      content,
      # permalink is included only when the text is truncated
      permalink=url if include_link != source.OMIT_LINK else None,
      # permashortlink is always included
      permashortlink=url if include_link == source.INCLUDE_LINK else None,
      target_length=target_length, link_length=TCO_LENGTH, format=format)

    if quote_tweet:
      truncated += ' ' + quote_tweet

    return truncated