Commit 8e20a20f authored by ale's avatar ale
Browse files

Update WP dump script to py3

parent c074aab9
from __future__ import print_function
import os
import re
import MySQLdb
import collections_py26 as collections
import logging
import pymysql
import collections
import sys
def _connect(url):
m = re.search(r'^mysql://([^/]+)/(.*)$', url)
def _connect(url, defaults_file=None):
m = re.search(r'^mysql://([^/]*)/(.*)$', url)
if not m:
raise Exception('malformed db url')
netloc, path = m.groups()
user_pw, host_port = netloc.split('@', 1)
user, pw = user_pw.split(':', 1)
if ':' in host_port:
user_pw, host_port = None, None
if netloc and '@' in netloc:
user_pw, host_port = netloc.split('@', 1)
user, pw = None, None
if user_pw and ':' in user_pw:
user, pw = user_pw.split(':', 1)
if host_port and ':' in host_port:
host, port = host_port.split(':', 1)
else:
host, port = host_port, 3306
try:
return MySQLdb.connect(host=host, port=int(port), user=user,
passwd=pw, db=path.strip('/'), charset='utf8')
except:
print 'ERROR connecting to %s:%s' % (host, port)
return pymysql.connect(host=host, port=int(port), user=user,
passwd=pw, db=path.strip('/'), charset='utf8',
read_defaults_file=defaults_file)
except Exception as e:
logging.error('could not connect to to %s: %s', url, e)
raise
......@@ -57,22 +66,11 @@ class WPDb(object):
"""
def __init__(self):
self.dbs = []
rcfile = os.getenv('NOBLOGS_WPDB', os.path.join(
os.getenv('HOME'), '.noblogs_wpdb'))
for line in open(rcfile):
if line.startswith('#') or line == '\n':
continue
self.dbs.append(_connect(line.strip()))
self.primarydb = self.dbs[0]
self._db = _connect(os.getenv('DBURL'),
os.getenv('DEFAULTS_FILE'))
def _cursor(self, blog_id=None):
if blog_id:
# This is our amazing hashing function.
db = self.dbs[(1 + blog_id) % 2]
else:
db = self.primarydb
return db.cursor()
return self._db.cursor()
def get_blogs(self, **args):
"""Search blogs.
......@@ -155,12 +153,15 @@ class WPDb(object):
if __name__ == '__main__':
logging.basicConfig()
if len(sys.argv) < 2:
logging.error('Not enough arguments')
sys.exit(2)
# Dump comments for the given blog IDs.
wp = WPDb()
all_blogs = wp.get_blogs(public=1)
print [x.domain for x in all_blogs[:10]]
print '******'
blog_id = all_blogs[10].blog_id
print wp.get_blog(blog_id)
print '******'
print wp.get_posts(blog_id, post_type='post', post_status='publish')
for blog_id in sys.argv[1:]:
comments = wp.get_comments(int(blog_id), comment_type='comment')
for c in comments:
print('%s' % (c,))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment