def main(args): db = pymongo.Connection(safe=True).motorblog print 'Updating', db.posts.count(), 'posts' print for post in db.posts.find().sort('_id'): print post['title'], post['type'], post['mod'] for key in ('body', 'original'): post[key] = remove_image_sizes.remove_image_sizes(post[key]) if not args.dry_run: print 'saving' db.posts.save(post)
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