def from_metaweblog(cls, struct, post_type='post', is_edit=False): """Receive metaWeblog RPC struct and initialize a Post. Used both by migrate_from_wordpress and when receiving a new or edited post from MarsEdit. """ title = struct.get('title', '') meta_description = struct.get('mt_excerpt', '') if len(meta_description) > 155: raise ValueError("Description is %d chars, max 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) 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) if not is_edit and 'date_created_gmt' in struct: # TODO: can fail if two posts created in same second, add random # suffix to ObjectId 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 main(args): db = pymongo.MongoClient().motorblog print('Updating all', db.posts.count(), 'posts') print for post in db.posts.find().sort('_id'): print post['title'], post['type'], post['mod'] post['plain'] = plain.plain(post['body']) db.posts.save(post)
def main(args): db = pymongo.MongoClient().motorblog print ( 'Updating all', db.posts.count(), 'posts') print for post in db.posts.find().sort('_id'): print post['title'], post['type'], post['mod'] post['plain'] = plain.plain(post['body']) db.posts.save(post)
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: # Not truncated return ' '.join(summary) # Text was truncated return ' '.join(summary) + ' [ ... ]'
def from_metaweblog( cls, struct, post_type='post', is_edit=False ): """Receive metaWeblog RPC struct and initialize a Post. Used both by migrate_from_wordpress and when receiving a new or edited post from MarsEdit. """ title = struct.get('title', '') custom_fields = { field['key']: field['value'] for field in struct.get('custom_fields', [])} meta_description = custom_fields.get('description', '') # We expect MarsEdit to set categories with mt_setPostCategories() assert 'categories' not in struct 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', '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() description = remove_image_sizes.remove_image_sizes(description) body = markup.markup(description) 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 ) if not is_edit and 'date_created_gmt' in struct: # TODO: can fail if two posts created in same second, add random # suffix to ObjectId 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 test_plain(self): self.assertEqual( "bar baz quux, fizzle, fazzle. hi!", plain(sample_html))
def from_metaweblog( cls, struct, post_type='post', is_edit=False ): """Receive metaWeblog RPC struct and initialize a Post. Used both by migrate_from_wordpress and when receiving a new or edited post from MarsEdit. """ title = struct.get('title', '') meta_description = struct.get('mt_excerpt', '') if len(meta_description) > 155: raise ValueError( "Description is %d chars, max 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) 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 ) if not is_edit and 'date_created_gmt' in struct: # TODO: can fail if two posts created in same second, add random # suffix to ObjectId 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 test_plain(self): self.assertEqual("bar baz quux, fizzle, fazzle. hi!", plain(sample_html))