Guest User

Untitled

a guest
Mar 3rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. '''
  2. This imports your old WP posts to a static html version
  3. '''
  4.  
  5. import sys
  6. import MySQLdb as mysql
  7.  
  8. USER = ''
  9. PASSWORD = ''
  10. DATABASE = ''
  11. lOST = ''
  12.  
  13. SAVE_TO = ''
  14. BASE_URL = ''
  15. POSTS_URL = ''
  16. HEAD_TAG ='''
  17. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  18. <meta name="generator" content="WordPress 2 HTML v1.0" />
  19. <meta name="description" content="My old blog posts" />
  20.  
  21. <link rel="stylesheet" type="text/css" media="screen" href="%s/css/posts.css" />
  22. ''' % POSTS_URL
  23. OLDER_FIRST = True
  24. DATE_FORMAT = '%d-%m-%Y' # day-month-4-digit year
  25.  
  26. def get_posts():
  27. global USER, PASSWORD, DATABASE, HOST
  28. try:
  29. conn = mysql.Connection(user=USER, passwd=PASSWORD, db=DATABASE)
  30. except:
  31. print 'Couldn\'t connect to database %s' % DATABASE
  32. sys.exit()
  33.  
  34. cursor = conn.cursor();
  35.  
  36. cursor.execute('SELECT * FROM wp_posts WHERE post_status = \'publish\'')
  37.  
  38. results = cursor.fetchall()
  39. conn.close()
  40. return results
  41.  
  42. def write_html(content):
  43. global SAVE_TO, HEAD_TAG
  44. try:
  45. opened = open(SAVE_TO + str(content['id']) + '.html', 'w')
  46. except IOError,e:
  47. print 'Miserable failure, %s' % e
  48. sys.exit()
  49.  
  50. # content is a dict with 3 keys: id, title and correctly parsed content.
  51.  
  52. opened.write('''<html>
  53. <head>
  54. <title>%s</title>
  55. %s
  56. </head>
  57. <body>
  58. <h1>%s</h1>
  59. %s
  60. </body>
  61. </html>''' % (content['title'], HEAD_TAG, content['title'],
  62. content['content']))
  63.  
  64. opened.close();
  65.  
  66. def sortify(set_of_posts):
  67. # set_of_posts is a list with all the posts fetched by get_posts()
  68. # this should return a sorted by date list of posts.
  69. # I have no idea if there's a built in way to do this or if this does
  70. # it properly, so bear with me.
  71. # I am forced to do this because I imported some posts from RSS on my WP
  72. # Install so I have mess up ids.
  73. global OLDER_FIRST
  74.  
  75. datetime_objects = []
  76. for i in set_of_posts:
  77. datetime_objects.append(i[2])
  78.  
  79. datetime_objects.sort()
  80. new_set = []
  81.  
  82. for v in datetime_objects:
  83. for i in set_of_posts:
  84. if i[2] == v:
  85. new_set.append(i)
  86. if OLDER_FIRST:
  87. return reversed(new_set)
  88. else:
  89. return new_set
  90.  
  91. def htmlize():
  92. global BASE_URL, POSTS_URL, DATE_FORMAT
  93. posts = sortify(get_posts())
  94. links = ''
  95. for post in posts:
  96. not_parsed = post[4]
  97. content = '</p>\n\n<p>'.join(not_parsed.split('\r\n\r\n'))
  98. post_info = {'id': int(post[0]), 'title': post[5], 'content':
  99. content+'\n<p><a href="%s/index.html" alt="Return to index">Return to List of Posts' % POSTS_URL}
  100. write_html(post_info)
  101. 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'],
  102. post_info['title'], post[2].strftime(DATE_FORMAT))
  103.  
  104.  
  105. # also let's make a nice page for it
  106. content = {'id': 'index', 'title': 'My old wordpress posts', 'content':
  107. links+'\n<h2><a href="%s" alt="Return to Main Home">Return to Homepage</a></p>' % BASE_URL}
  108. write_html(content);
  109.  
  110. if __name__ == '__main__':
  111. htmlize();
Add Comment
Please, Sign In to add comment