How to use the granary.microformats2.get_string_urls 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 / handlers.py View on Github external
url = image.get('url')
    if url:
      image['url'] = util.update_scheme(url, self)

    mf2_json = microformats2.object_to_json(obj, synthesize_content=False)

    # try to include the author's silo profile url
    author = first_props(mf2_json.get('properties', {})).get('author', {})
    author_uid = first_props(author.get('properties', {})).get('uid', '')
    if author_uid:
      parsed = util.parse_tag_uri(author_uid)
      if parsed:
        urls = author.get('properties', {}).setdefault('url', [])
        try:
          silo_url = self.source.gr_source.user_url(parsed[1])
          if silo_url not in microformats2.get_string_urls(urls):
            urls.append(silo_url)
        except NotImplementedError:  # from gr_source.user_url()
          pass

    # write the response!
    self.response.headers['Access-Control-Allow-Origin'] = '*'
    if format == 'html':
      self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
      url = obj.get('url', '')
      self.response.out.write(TEMPLATE.substitute({
        'refresh': (('' % url)
                    if url else ''),
        'url': url,
        'body': microformats2.json_to_html(mf2_json),
        'title': self.get_title(obj),
      }))
github snarfed / bridgy / publish.py View on Github external
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
github snarfed / ownyourresponses / app.py View on Github external
# http://indiewebcamp.com/micropub
      #
      # include access token in both header and post body for compatibility
      # with servers that only support one or the other (for whatever reason).
      headers = {'Authorization': 'Bearer ' + MICROPUB_ACCESS_TOKEN}
      data = {
        'access_token': MICROPUB_ACCESS_TOKEN,
        'h': 'entry',
        'category[]': CATEGORIES.get(type),
        'content[html]': self.render(source, activity, base),
        'name': base.get('content') or base.get('object', {}).get('content'),
      }
      for key in 'in-reply-to', 'like-of', 'repost-of', 'published', 'updated':
        val = mf2_props.get(key)
        if val:
          data[key] = microformats2.get_string_urls([val])[0]

      try:
        result = self.urlopen(MICROPUB_ENDPOINT, headers=headers,
                              data=util.trim_nulls(data))
      except urllib2.HTTPError as exception:
        logging.exception('%s %s', exception.reason, exception.read())
        continue
      except urllib2.URLError as exception:
        logging.exception(exception.reason)
        continue

      resp.post_url = result.info().get('Location')
      logging.info('Created new post: %s', resp.post_url)
      resp.response_body = result.read()
      logging.info('Response body: %s', resp.response_body)
github snarfed / bridgy / publish.py View on Github external
field, url, stack_info=True)
          continue

        synd_urls = mf2['rels'].get('syndication', [])

        # look for syndication urls in the first h-entry
        queue = collections.deque(mf2.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
github snarfed / bridgy / blog_webmention.py View on Github external
items: sequence of mf2 item dicts

    Returns:
      mf2 item dict or None
    """
    # find target URL in source
    for item in items:
      props = item.setdefault('properties', {})

      # find first non-empty content element
      content = props.setdefault('content', [{}])[0]
      text = content.get('html') or content.get('value')

      for type in 'in-reply-to', 'like', 'like-of', 'repost', 'repost-of':
        urls = [urllib.parse.urldefrag(u)[0] for u in
                microformats2.get_string_urls(props.get(type, []))]
        if self.any_target_in(urls):
          break
      else:
        if text and self.any_target_in(text):
          type = 'post'
          url = first_value(props, 'url') or self.source_url
          name = first_value(props, 'name') or first_value(props, 'summary')
          text = content['html'] = ('mentioned this in %s.' %
                                    util.pretty_link(url, text=name, max_length=280))
        else:
          type = None

      if type:
        # found the target!
        rsvp = first_value(props, 'rsvp')
        if rsvp: