Example #1
0
    def from_metaweblog(cls, struct, post_type ='post', is_edit = False):
        # 从metaweblog RPC取得的结构,初始化一个post
        title = struct.get('title','')

        meta_description = struct.get('mt_excerpt', '')
        if len(meta_description) > 155:
            raise ValueError(
                "Description is %d chars, Max length is 155" % len(meta_description)
            )

        if 'mt_keywords' in struct:
            tags = [
                tag.strip() for tag in struct['mt_keywords'].split(',')
                if tag.strip()
            ]
        else:
            tags = None

        slug = (
            slugify.slugify(struct['wp_slug'])
                if struct.get('wp_slug')
                else slugify.slugify(title))

        description = struct.get('description', '')
        status = (struct.get('post_status')
                or struct.get('page_status')
                or 'publish')

        if "date_modified_gmt" in struct:
            tup = struct['date_modified_gmt'].timetuple()
            mod = utc_tz.localize(datetime.datetime(*tup[0:6]))
        else:
            mod = datetime.datetime.utcnow()

        body = markup.markup(description)
        img_src = struct[u'img_src'] if struct.has_key(u'img_src') else ''
        rv = cls(
            title=title,
            # Format for display
            body=body,
            plain=plain.plain(body),
            summary=summarize.summarize(body, 200),
            original=description,
            meta_description=meta_description,
            tags=tags,
            slug=slug,
            type=post_type,
            status=status,
            wordpress_id=struct.get('postid'),
            mod=mod,
            img_src=img_src
        )

        if not is_edit and "data_created_gmt" in struct:
            date_created = datetime.datetime.strptime(
                struct['date_created_gmt'].value, "%Y%m%dT%H:%M:%S")
            rv.id = ObjectId.from_datetime(date_created)

        return rv
def summarize(html, n):
    """Returns plain-text summary
    """
    summary = []
    length = 0
    for word in whitespace.split(plain(html)):
        if word:
            if length + len(word) < n:
                summary.append(word)
                length += len(word)
            else:
                break
    else:
        return ' '.join(summary)

    return ' '.join(summary) + ' [...]'