<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import sys <EOL> import yaml <EOL> import codecs <EOL> import sqlalchemy as sa <EOL> import sqlalchemy . orm as orm <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> table_prefix = "<STR_LIT>" <EOL> db_username = "<STR_LIT>" <EOL> db_password = "<STR_LIT>" <EOL> db_host = "<STR_LIT:127.0.0.1>" <EOL> db_port = "<STR_LIT>" <EOL> db_database = "<STR_LIT>" <EOL> db_encoding = "<STR_LIT:utf8>" <EOL> db_conn = "<STR_LIT>" . format ( ** locals ( ) ) <EOL> engine = sa . create_engine ( db_conn ) <EOL> Session = orm . scoped_session ( <EOL> orm . sessionmaker ( autocommit = False , <EOL> autoflush = False , <EOL> bind = engine ) ) <EOL> Base = declarative_base ( bind = engine ) <EOL> session = Session ( ) <EOL> class Post ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = sa . Column ( "<STR_LIT>" , sa . Integer , primary_key = True ) <EOL> author_id = sa . Column ( "<STR_LIT>" , <EOL> sa . ForeignKey ( table_prefix + '<STR_LIT>' ) ) <EOL> author = orm . relation ( "<STR_LIT>" , primaryjoin = "<STR_LIT>" ) <EOL> term_relationship = orm . relation ( "<STR_LIT>" , <EOL> primaryjoin = "<STR_LIT>" ) <EOL> def categories ( self ) : <EOL> return [ r . taxonomy . term . name for r in self . term_relationship <EOL> if r . taxonomy . taxonomy == "<STR_LIT>" ] <EOL> def tags ( self ) : <EOL> return [ r . taxonomy . term . name for r in self . term_relationship <EOL> if r . taxonomy . taxonomy == "<STR_LIT>" ] <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" . format ( <EOL> self . post_title , self . id , self . post_status ) <EOL> def permalink ( self ) : <EOL> site_url = get_blog_url ( ) <EOL> structure = get_blog_permalink_structure ( ) <EOL> structure = structure . replace ( "<STR_LIT>" , str ( self . post_date . year ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , <EOL> str ( self . post_date . month ) . zfill ( <NUM_LIT:2> ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , str ( self . post_date . day ) . zfill ( <NUM_LIT:2> ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , <EOL> str ( self . post_date . hour ) . zfill ( <NUM_LIT:2> ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , <EOL> str ( self . post_date . minute ) . zfill ( <NUM_LIT:2> ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , <EOL> str ( self . post_date . second ) . zfill ( <NUM_LIT:2> ) ) <EOL> structure = structure . replace ( "<STR_LIT>" , self . post_name ) <EOL> structure = structure . replace ( "<STR_LIT>" , str ( self . id ) ) <EOL> try : <EOL> structure = structure . replace ( "<STR_LIT>" , self . categories ( ) [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> pass <EOL> try : <EOL> structure = structure . replace ( "<STR_LIT>" , self . tags ( ) [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> pass <EOL> structure = structure . replace ( "<STR_LIT>" , self . author . user_nicename ) <EOL> return site_url . rstrip ( "<STR_LIT:/>" ) + "<STR_LIT:/>" + structure . lstrip ( "<STR_LIT:/>" ) <EOL> class User ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = sa . Column ( "<STR_LIT>" , sa . Integer , primary_key = True ) <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" . format ( self . user_nicename ) <EOL> class Term ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = sa . Column ( "<STR_LIT>" , sa . Integer , primary_key = True ) <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" . format ( self . name ) <EOL> class TermTaxonomy ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = sa . Column ( '<STR_LIT>' , sa . Integer , primary_key = True ) <EOL> term_id = sa . Column ( "<STR_LIT>" , <EOL> sa . ForeignKey ( table_prefix + "<STR_LIT>" ) ) <EOL> term = orm . relation ( "<STR_LIT>" , primaryjoin = "<STR_LIT>" ) <EOL> class TermRelationship ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = sa . Column ( '<STR_LIT>' , sa . ForeignKey ( table_prefix + "<STR_LIT>" ) , <EOL> primary_key = True ) <EOL> taxonomy_id = sa . Column ( "<STR_LIT>" , sa . ForeignKey ( <EOL> table_prefix + "<STR_LIT>" ) , primary_key = True ) <EOL> taxonomy = orm . relation ( "<STR_LIT>" , <EOL> primaryjoin = "<STR_LIT>" ) <EOL> class WordpressOptions ( Base ) : <EOL> __tablename__ = table_prefix + "<STR_LIT>" <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> def get_published_posts ( blog_id = <NUM_LIT:0> ) : <EOL> return [ p for p in session . query ( Post ) . all ( ) if p . post_status == "<STR_LIT>" <EOL> and p . post_type == "<STR_LIT>" ] <EOL> def get_blog_url ( blog_id = <NUM_LIT:0> ) : <EOL> return session . query ( WordpressOptions ) . filter ( <EOL> WordpressOptions . blog_id == blog_id ) . filter ( WordpressOptions . option_name == "<STR_LIT>" ) . first ( ) . option_value <EOL> def get_blog_permalink_structure ( blog_id = <NUM_LIT:0> ) : <EOL> return session . query ( WordpressOptions ) . filter ( <EOL> WordpressOptions . blog_id == blog_id ) . filter ( WordpressOptions . option_name == "<STR_LIT>" ) . first ( ) . option_value <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if os . path . isdir ( "<STR_LIT>" ) : <EOL> print "<STR_LIT>" "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> os . mkdir ( "<STR_LIT>" ) <EOL> post_num = <NUM_LIT:1> <EOL> for post in get_published_posts ( ) : <EOL> yaml_data = { <EOL> "<STR_LIT:title>" : post . post_title , <EOL> "<STR_LIT:date>" : post . post_date . strftime ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : post . permalink ( ) , <EOL> "<STR_LIT>" : "<STR_LIT:U+002CU+0020>" . join ( post . categories ( ) ) , <EOL> "<STR_LIT>" : "<STR_LIT:U+002CU+0020>" . join ( post . tags ( ) ) , <EOL> "<STR_LIT>" : post . guid <EOL> } <EOL> fn = u"<STR_LIT>" . format ( <EOL> str ( post_num ) . zfill ( <NUM_LIT:4> ) , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT>' , post . post_title . strip ( ) . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ) ) <EOL> print "<STR_LIT>" + fn <EOL> f = codecs . open ( os . path . join ( "<STR_LIT>" , fn ) , "<STR_LIT:w>" , "<STR_LIT:utf-8>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( yaml . safe_dump ( yaml_data , default_flow_style = False , allow_unicode = True ) . decode ( "<STR_LIT:utf-8>" ) ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( post . post_content . replace ( u"<STR_LIT:\r\n>" , u"<STR_LIT:\n>" ) ) <EOL> f . close ( ) <EOL> post_num += <NUM_LIT:1> </s>
