Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This imports your old WP posts to a static html version
- '''
- import sys
- import MySQLdb as mysql
- USER = ''
- PASSWORD = ''
- DATABASE = ''
- lOST = ''
- SAVE_TO = ''
- BASE_URL = ''
- POSTS_URL = ''
- HEAD_TAG ='''
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="generator" content="WordPress 2 HTML v1.0" />
- <meta name="description" content="My old blog posts" />
- <link rel="stylesheet" type="text/css" media="screen" href="%s/css/posts.css" />
- ''' % POSTS_URL
- OLDER_FIRST = True
- DATE_FORMAT = '%d-%m-%Y' # day-month-4-digit year
- def get_posts():
- global USER, PASSWORD, DATABASE, HOST
- try:
- conn = mysql.Connection(user=USER, passwd=PASSWORD, db=DATABASE)
- except:
- print 'Couldn\'t connect to database %s' % DATABASE
- sys.exit()
- cursor = conn.cursor();
- cursor.execute('SELECT * FROM wp_posts WHERE post_status = \'publish\'')
- results = cursor.fetchall()
- conn.close()
- return results
- def write_html(content):
- global SAVE_TO, HEAD_TAG
- try:
- opened = open(SAVE_TO + str(content['id']) + '.html', 'w')
- except IOError,e:
- print 'Miserable failure, %s' % e
- sys.exit()
- # content is a dict with 3 keys: id, title and correctly parsed content.
- opened.write('''<html>
- <head>
- <title>%s</title>
- %s
- </head>
- <body>
- <h1>%s</h1>
- %s
- </body>
- </html>''' % (content['title'], HEAD_TAG, content['title'],
- content['content']))
- opened.close();
- def sortify(set_of_posts):
- # set_of_posts is a list with all the posts fetched by get_posts()
- # this should return a sorted by date list of posts.
- # I have no idea if there's a built in way to do this or if this does
- # it properly, so bear with me.
- # I am forced to do this because I imported some posts from RSS on my WP
- # Install so I have mess up ids.
- global OLDER_FIRST
- datetime_objects = []
- for i in set_of_posts:
- datetime_objects.append(i[2])
- datetime_objects.sort()
- new_set = []
- for v in datetime_objects:
- for i in set_of_posts:
- if i[2] == v:
- new_set.append(i)
- if OLDER_FIRST:
- return reversed(new_set)
- else:
- return new_set
- def htmlize():
- global BASE_URL, POSTS_URL, DATE_FORMAT
- posts = sortify(get_posts())
- links = ''
- for post in posts:
- not_parsed = post[4]
- content = '</p>\n\n<p>'.join(not_parsed.split('\r\n\r\n'))
- post_info = {'id': int(post[0]), 'title': post[5], 'content':
- content+'\n<p><a href="%s/index.html" alt="Return to index">Return to List of Posts' % POSTS_URL}
- write_html(post_info)
- links += '<p><a href="%s/%d.html" alt="Link to: %s">%s</a> posted on %s</p>\n' % (POSTS_URL, post_info['id'],post_info['title'],
- post_info['title'], post[2].strftime(DATE_FORMAT))
- # also let's make a nice page for it
- content = {'id': 'index', 'title': 'My old wordpress posts', 'content':
- links+'\n<h2><a href="%s" alt="Return to Main Home">Return to Homepage</a></p>' % BASE_URL}
- write_html(content);
- if __name__ == '__main__':
- htmlize();
Add Comment
Please, Sign In to add comment