Example #1
0
    def generate_slug(self):
        if self.title:
            title = self.title
            if self.post_type == 'event' and self.start:
                title = self.start.strftime('%b %d') + ' ' + title
            return util.slugify(title)

        if self.item:
            item_name = self.item.get('name')
            if item_name:
                if 'author' in self.item:
                    item_name += ' by ' + self.item.get('author')
                return util.slugify('review of ' + item_name)

        content_plain = util.format_as_text(self.content or '', None)
        if content_plain:
            return util.slugify(content_plain, 48) or 'untitled'

        if self.post_type == 'checkin' and self.venue:
            return util.slugify('checked into ' + self.venue.name + ' '
                                + content_plain, 48)

        for ctxs, prefix in ((self.bookmark_contexts, 'bookmark-of-'),
                             (self.like_contexts, 'like-of-'),
                             (self.repost_contexts, 'repost-of-'),
                             (self.reply_contexts, 'reply-to-')):
            if ctxs:
                return util.slugify(prefix + ctxs[0].get_slugify_target(), 48)

        return 'untitled'
Example #2
0
    def title_or_fallback(self):
        """Feeds and <title> attributes require a human-readable
        title, even for posts that do not have an explicit title. Try
        here to create a reasonable one.
        """
        def format_context(ctx):
            if ctx.title and ctx.author_name:
                return '“{}” by {}'.format(ctx.title, ctx.author_name)
            if ctx.title:
                return ctx.title
            if ctx.author_name:
                return 'a post by {}'.format(ctx.author_name)
            return util.prettify_url(ctx.permalink)

        if self.title:
            return self.title
        if self.post_type == 'checkin' and self.venue:
            return 'Checked in to {}'.format(self.venue.name)
        if self.repost_contexts:
            return 'Shared {}'.format(', '.join(
                map(format_context, self.repost_contexts)))
        if self.like_contexts:
            return 'Liked {}'.format(', '.join(
                map(format_context, self.like_contexts)))
        if self.bookmark_contexts:
            return 'Bookmarked {}'.format(', '.join(
                map(format_context, self.bookmark_contexts)))
        if self.content:
            return util.format_as_text(self.content)
        return 'A {} from {}'.format(self.post_type, self.published)
Example #3
0
    def title_or_fallback(self):
        """Feeds and <title> attributes require a human-readable
        title, even for posts that do not have an explicit title. Try
        here to create a reasonable one.
        """
        def format_context(ctx):
            if ctx.title and ctx.author_name:
                return '“{}” by {}'.format(ctx.title, ctx.author_name)
            if ctx.title:
                return ctx.title
            if ctx.author_name:
                return 'a post by {}'.format(ctx.author_name)
            return util.prettify_url(ctx.permalink)

        if self.title:
            return self.title
        if self.post_type == 'checkin' and self.venue:
            return 'Checked in to {}'.format(self.venue.name)
        if self.repost_contexts:
            return 'Shared {}'.format(', '.join(map(format_context, self.repost_contexts)))
        if self.like_contexts:
            return 'Liked {}'.format(', '.join(map(format_context, self.like_contexts)))
        if self.bookmark_contexts:
            return 'Bookmarked {}'.format(', '.join(map(format_context, self.bookmark_contexts)))
        if self.content:
            return util.format_as_text(self.content)
        return 'A {} from {}'.format(self.post_type, self.published)
Example #4
0
    def generate_slug(self):
        if self.title:
            title = self.title
            if self.post_type == 'event' and self.start:
                title = self.start.strftime('%b %d') + ' ' + title
            return util.slugify(title)

        if self.item:
            item_name = self.item.get('name')
            if item_name:
                if 'author' in self.item:
                    item_name += ' by ' + self.item.get('author')
                return util.slugify('review of ' + item_name)

        content_plain = util.format_as_text(self.content or '', None)
        if content_plain:
            return util.slugify(content_plain, 48) or 'untitled'

        if self.post_type == 'checkin' and self.venue:
            return util.slugify(
                'checked into ' + self.venue.name + ' ' + content_plain, 48)

        for ctxs, prefix in ((self.bookmark_contexts, 'bookmark-of-'),
                             (self.like_contexts, 'like-of-'),
                             (self.repost_contexts, 'repost-of-'),
                             (self.reply_contexts, 'reply-to-')):
            if ctxs:
                return util.slugify(prefix + ctxs[0].get_slugify_target(), 48)

        return 'untitled'
Example #5
0
def guess_content(post):
    name = None
    picture = None
    link = None
    message = ""

    if post.title:
        message += post.title + "\n\n"

    html = util.autolink(util.markdown_filter(post.content))

    message += util.format_as_text(html)

    if post.post_type != "article":
        message += " (" + post.shortlink + ")"

    if post.post_type == "share":
        link = next((s.url for s in post.repost_contexts), None)

    elif post.post_type == "article":
        name = post.title
        link = post.permalink

    elif post.post_type == "photo" and post.attachments:
        picture = post.attachments[0].url

    else:
        # first link becomes the target
        soup = BeautifulSoup(html)

        # filter out hashtags
        link = next(filter(lambda h: h and not h.startswith("#"), (a.get("href") for a in soup.find_all("a"))), None)

    return message, link, name, picture
Example #6
0
def format_markdown_as_tweet(data):
    def to_twitter_handle(contact, nick):
        """Attempt to replace friendly @name with the official @twitter
        username
        """
        if contact and contact.social:
            nick = contact.social.get('twitter') or nick
        return '@' + nick

    html = util.markdown_filter(data)
    html = util.process_people(to_twitter_handle, html)
    return util.format_as_text(html)
Example #7
0
def format_markdown_as_tweet(data):
    def to_twitter_handle(contact, nick):
        """Attempt to replace friendly @name with the official @twitter
        username
        """
        if contact:
            for url in contact.social:
                m = util.TWITTER_PROFILE_RE.match(url)
                if m:
                    nick = m.group(1)
                    break
        return '@' + nick

    html = util.markdown_filter(data)
    html = util.process_people(to_twitter_handle, html)
    return util.format_as_text(html)
Example #8
0
def format_markdown_as_tweet(data):
    def to_twitter_handle(contact, nick):
        """Attempt to replace friendly @name with the official @twitter
        username
        """
        if contact:
            for url in contact.social:
                m = util.TWITTER_PROFILE_RE.match(url)
                if m:
                    nick = m.group(1)
                    break
        return '@' + nick

    html = util.markdown_filter(data)
    html = util.process_people(to_twitter_handle, html)
    return util.format_as_text(html)
Example #9
0
    def generate_slug(self):
        if self.title:
            return util.slugify(self.title)

        content_plain = util.format_as_text(self.content or '', lambda a: a)
        if content_plain:
            return util.slugify(content_plain, 48) or 'untitled'

        if self.post_type == 'checkin' and self.venue:
            return util.slugify('checked into ' + self.venue.name + ' '
                                + content_plain, 48)

        for ctxs, prefix in ((self.bookmark_contexts, 'bookmark-of-'),
                             (self.like_contexts, 'like-of-'),
                             (self.repost_contexts, 'repost-of-'),
                             (self.reply_contexts, 'reply-to-')):
            if ctxs:
                return util.slugify(prefix + ctxs[0].get_slugify_target(), 48)

        return 'untitled'
Example #10
0
def guess_content(post):
    name = None
    picture = None
    link = None
    message = ''

    if post.title:
        message += post.title + '\n\n'

    html = util.autolink(
        util.markdown_filter(post.content))

    message += util.format_as_text(html)

    if post.post_type != 'article':
        message += ' (' + post.shortlink + ')'

    if post.post_type == 'share':
        link = next((s.url for s in post.repost_contexts), None)

    elif post.post_type == 'article':
        name = post.title
        link = post.permalink

    elif post.post_type == 'photo' and post.attachments:
        picture = post.attachments[0].url

    else:
        # first link becomes the target
        soup = BeautifulSoup(html)

        # filter out hashtags
        link = next(filter(lambda h: h and not h.startswith('#'),
                           (a.get('href') for a in soup.find_all('a'))), None)

    return message, link, name, picture
Example #11
0
def guess_content(post):
    name = None
    picture = None
    link = None
    message = ''

    if post.title:
        message += post.title + '\n\n'

    html = util.autolink(
        util.markdown_filter(post.content))

    message += util.format_as_text(html)

    if post.post_type != 'article':
        message += ' (' + post.shortlink + ')'

    if post.post_type == 'share':
        link = next((s.url for s in post.repost_contexts), None)

    elif post.post_type == 'article':
        name = post.title
        link = post.permalink

    elif post.post_type == 'photo' and post.attachments:
        picture = post.attachments[0].url

    else:
        # first link becomes the target
        soup = BeautifulSoup(html)

        # filter out hashtags
        link = next(filter(lambda h: h and not h.startswith('#'),
                           (a.get('href') for a in soup.find_all('a'))), None)

    return message, link, name, picture
Example #12
0
def format_markdown_as_facebook(data):
    return util.format_as_text(util.markdown_filter(data))
Example #13
0
def create_mentions(post, url, source_response):
    target_urls = []
    if post:
        base_target_urls = [post.permalink]

        for base_url in base_target_urls:
            target_urls.append(base_url)
            target_urls.append(
                base_url.replace("https://", "http://")
                if base_url.startswith("https://")
                else base_url.replace("http://", "https://")
            )

    blob = mf2py.parse(doc=source_response.text, url=url)
    if not blob:
        current_app.logger.debug("create_mention: no mf2 in source_response")
        return
    entry = mf2util.interpret_comment(blob, url, target_urls)
    current_app.logger.debug("interpreted comment: %r", entry)

    if not entry:
        current_app.logger.debug("create_mention: mf2util found no comment entry")
        return
    comment_type = entry.get("comment_type", [])

    to_process = [(entry, url)]
    # process 2nd level "downstream" comments
    if "reply" in comment_type:
        downstream_cmts = entry.get("comment", [])
        current_app.logger.debug("adding in downstream comments:%d", len(downstream_cmts))
        for dc in downstream_cmts:
            if dc.get("url"):
                to_process.append((dc, dc.get("url")))

    results = []
    for entry, url in to_process:
        current_app.logger.debug("processing %s %r", url, entry)
        content = util.clean_foreign_html(entry.get("content", ""))
        content_plain = util.format_as_text(content)

        published = entry.get("published")
        if not published:
            published = datetime.datetime.utcnow()

        # update an existing mention
        mention = next((m for m in post.mentions if m.url == url), None)
        # or create a new one
        if not mention:
            mention = Mention()
        mention.url = url
        mention.permalink = entry.get("url") or url
        mention.reftype = comment_type[0] if comment_type else "reference"
        mention.author_name = entry.get("author", {}).get("name", "")
        mention.author_url = entry.get("author", {}).get("url", "")
        mention.author_image = entry.get("author", {}).get("photo")
        mention.content = content
        mention.content_plain = content_plain
        mention.published = published
        mention.title = entry.get("name")
        mention.syndication = entry.get("syndication", [])
        mention.rsvp = entry.get("rsvp")
        results.append(mention)

    return results
Example #14
0
def format_markdown_as_facebook(data):
    return util.format_as_text(util.markdown_filter(data))
Example #15
0
def create_mentions(post, url, source_response, is_person_mention):
    # utility function for mf2util
    cached_mf2 = {}

    def fetch_mf2(url):
        if url in cached_mf2:
            return cached_mf2[url]
        p = mf2py.parse(url=url)
        cached_mf2[url] = p
        return p

    target_urls = []
    if post:
        base_target_urls = [post.permalink]

        for base_url in base_target_urls:
            target_urls.append(base_url)
            target_urls.append(base_url.replace('https://', 'http://')
                               if base_url.startswith('https://')
                               else base_url.replace('http://', 'https://'))

    blob = mf2py.parse(doc=source_response.text, url=url)
    cached_mf2[url] = blob

    if not blob:
        current_app.logger.debug('create_mention: no mf2 in source_response')
        return
    entry = mf2util.interpret_comment(
        blob, url, target_urls, fetch_mf2_func=fetch_mf2)
    current_app.logger.debug('interpreted comment: %r', entry)

    if not entry:
        current_app.logger.debug(
            'create_mention: mf2util found no comment entry')
        return
    comment_type = entry.get('comment_type', [])

    to_process = [(entry, url)]
    # process 2nd level "downstream" comments
    if 'reply' in comment_type:
        downstream_cmts = entry.get('comment', [])
        current_app.logger.debug('adding in downstream comments:%d',
                                 len(downstream_cmts))
        for dc in downstream_cmts:
            if dc.get('url'):
                to_process.append((dc, dc.get('url')))

    results = []
    for entry, url in to_process:
        current_app.logger.debug('processing %s %r', url, entry)
        content = util.clean_foreign_html(entry.get('content', ''))
        content_plain = util.format_as_text(content)

        published = entry.get('published')
        if not published:
            published = datetime.datetime.utcnow()

        # update an existing mention
        mention = next((m for m in post.mentions if m.url == url), None)\
                  if post else None

        # or create a new one
        if not mention:
            mention = Mention()
        mention.url = url
        mention.person_mention = is_person_mention
        mention.permalink = entry.get('url') or url
        mention.reftype = comment_type[0] if comment_type else 'reference'
        mention.author_name = entry.get('author', {}).get('name', '')
        mention.author_url = entry.get('author', {}).get('url', '')
        mention.author_image = entry.get('author', {}).get('photo')
        mention.content = content
        mention.content_plain = content_plain
        mention.published = published
        mention.title = entry.get('name')
        mention.syndication = entry.get('syndication', [])
        mention.rsvp = entry.get('rsvp')
        results.append(mention)

    return results
Example #16
0
def create_mentions(post, url, source_response, is_person_mention):
    # utility function for mf2util
    cached_mf2 = {}

    def fetch_mf2(url):
        if url in cached_mf2:
            return cached_mf2[url]
        p = mf2py.parse(url=url)
        cached_mf2[url] = p
        return p

    target_urls = []
    if post:
        base_target_urls = [post.permalink]

        for base_url in base_target_urls:
            target_urls.append(base_url)
            target_urls.append(
                base_url.replace('https://', 'http://') if base_url.startswith(
                    'https://') else base_url.replace('http://', 'https://'))

    blob = mf2py.parse(doc=source_response.text, url=url)
    cached_mf2[url] = blob

    if not blob:
        current_app.logger.debug('create_mention: no mf2 in source_response')
        return
    entry = mf2util.interpret_comment(blob,
                                      url,
                                      target_urls,
                                      fetch_mf2_func=fetch_mf2)
    current_app.logger.debug('interpreted comment: %r', entry)

    if not entry:
        current_app.logger.debug(
            'create_mention: mf2util found no comment entry')
        return
    comment_type = entry.get('comment_type', [])

    to_process = [(entry, url)]
    # process 2nd level "downstream" comments
    if 'reply' in comment_type:
        downstream_cmts = entry.get('comment', [])
        current_app.logger.debug('adding in downstream comments:%d',
                                 len(downstream_cmts))
        for dc in downstream_cmts:
            if dc.get('url'):
                to_process.append((dc, dc.get('url')))

    results = []
    for entry, url in to_process:
        current_app.logger.debug('processing %s %r', url, entry)
        content = util.clean_foreign_html(entry.get('content', ''))
        content_plain = util.format_as_text(content)

        published = entry.get('published')
        if not published:
            published = datetime.datetime.utcnow()

        # update an existing mention
        mention = next((m for m in post.mentions if m.url == url), None)\
                  if post else None

        # or create a new one
        if not mention:
            mention = Mention()
        mention.url = url
        mention.person_mention = is_person_mention
        mention.permalink = entry.get('url') or url
        mention.reftype = comment_type[0] if comment_type else 'reference'
        mention.author_name = entry.get('author', {}).get('name', '')
        mention.author_url = entry.get('author', {}).get('url', '')
        mention.author_image = entry.get('author', {}).get('photo')
        mention.content = content
        mention.content_plain = content_plain
        mention.published = published
        mention.title = entry.get('name')
        mention.syndication = entry.get('syndication', [])
        mention.rsvp = entry.get('rsvp')
        results.append(mention)

    return results