2012-10-23 18:36:43 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2013-09-02 12:22:08 +00:00
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2013-11-11 10:34:13 +00:00
|
|
|
import textwrap
|
2013-09-02 12:22:08 +00:00
|
|
|
|
2013-09-08 11:02:25 +00:00
|
|
|
from time import mktime, strptime
|
2012-10-23 18:36:43 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2013-11-11 11:02:49 +00:00
|
|
|
try:
|
|
|
|
input = raw_input
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
try:
|
|
|
|
from urlparse import urlparse
|
|
|
|
except ImportError:
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2012-10-23 18:36:43 +00:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
|
|
ns = '{http://disqus.com}'
|
|
|
|
dsq = '{http://disqus.com/disqus-internals}'
|
|
|
|
|
2013-11-11 10:34:13 +00:00
|
|
|
threads = set([])
|
|
|
|
comments = set([])
|
|
|
|
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-11-11 10:34:13 +00:00
|
|
|
def insert(db, thread, posts):
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-11-16 19:30:48 +00:00
|
|
|
path = urlparse(thread.find('%slink' % ns).text).path
|
2012-10-23 18:36:43 +00:00
|
|
|
remap = dict()
|
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
if path not in db.threads:
|
|
|
|
db.threads.new(path, thread.find('%stitle' % ns).text.strip())
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-11-11 10:34:13 +00:00
|
|
|
for item in sorted(posts, key=lambda k: k['created']):
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
dsq_id = item.pop('dsq:id')
|
|
|
|
item['parent'] = remap.get(item.pop('dsq:parent', None))
|
|
|
|
rv = db.comments.add(path, item)
|
|
|
|
remap[dsq_id] = rv["id"]
|
2013-09-13 13:21:18 +00:00
|
|
|
|
2013-11-11 10:34:13 +00:00
|
|
|
comments.update(set(remap.keys()))
|
|
|
|
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-09-02 12:22:08 +00:00
|
|
|
def disqus(db, xmlfile):
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-11-11 11:02:49 +00:00
|
|
|
if db.execute("SELECT * FROM comments").fetchone():
|
|
|
|
if input("Isso DB is not empty! Continue? [y/N]: ") not in ("y", "Y"):
|
|
|
|
raise SystemExit("Abort.")
|
|
|
|
|
2013-09-02 12:22:08 +00:00
|
|
|
tree = ElementTree.parse(xmlfile)
|
2012-10-23 18:36:43 +00:00
|
|
|
res = defaultdict(list)
|
|
|
|
|
|
|
|
for post in tree.findall('%spost' % ns):
|
|
|
|
|
|
|
|
item = {
|
|
|
|
'dsq:id': post.attrib.get(dsq + 'id'),
|
|
|
|
'text': post.find('%smessage' % ns).text,
|
|
|
|
'author': post.find('%sauthor/%sname' % (ns, ns)).text,
|
|
|
|
'email': post.find('%sauthor/%semail' % (ns, ns)).text,
|
|
|
|
'created': mktime(strptime(
|
2013-09-19 16:30:46 +00:00
|
|
|
post.find('%screatedAt' % ns).text, '%Y-%m-%dT%H:%M:%SZ')),
|
2013-10-13 13:33:57 +00:00
|
|
|
'remote_addr': '127.0.0.0',
|
2013-11-11 10:44:32 +00:00
|
|
|
'mode': 1 if post.find("%sisDeleted" % ns).text == "false" else 4
|
2012-10-23 18:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if post.find(ns + 'parent') is not None:
|
|
|
|
item['dsq:parent'] = post.find(ns + 'parent').attrib.get(dsq + 'id')
|
|
|
|
|
|
|
|
res[post.find('%sthread' % ns).attrib.get(dsq + 'id')].append(item)
|
|
|
|
|
2013-09-02 12:22:08 +00:00
|
|
|
num = len(tree.findall('%sthread' % ns))
|
2013-12-18 11:51:14 +00:00
|
|
|
cols = int((os.popen('stty size', 'r').read() or "25 80").split()[1])
|
2013-09-02 12:22:08 +00:00
|
|
|
|
|
|
|
for i, thread in enumerate(tree.findall('%sthread' % ns)):
|
|
|
|
|
|
|
|
if int(round((i+1)/num, 2) * 100) % 13 == 0:
|
|
|
|
|
|
|
|
sys.stdout.write("\r%s" % (" "*cols))
|
|
|
|
sys.stdout.write("\r[%i%%] %s" % (((i+1)/num * 100), thread.find('%sid' % ns).text))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2013-11-11 10:34:13 +00:00
|
|
|
# skip (possibly?) duplicate, but empty thread elements
|
2013-10-19 10:45:10 +00:00
|
|
|
if thread.find('%sid' % ns).text is None:
|
|
|
|
continue
|
|
|
|
|
2012-10-23 18:36:43 +00:00
|
|
|
id = thread.attrib.get(dsq + 'id')
|
|
|
|
if id in res:
|
2013-11-11 10:34:13 +00:00
|
|
|
threads.add(id)
|
2012-10-23 18:36:43 +00:00
|
|
|
insert(db, thread, res[id])
|
2013-09-02 12:22:08 +00:00
|
|
|
|
2013-11-11 10:44:32 +00:00
|
|
|
# in case a comment has been deleted (and no further childs)
|
|
|
|
db.comments._remove_stale()
|
|
|
|
|
2013-09-02 12:22:08 +00:00
|
|
|
sys.stdout.write("\r%s" % (" "*cols))
|
2013-11-11 10:34:13 +00:00
|
|
|
sys.stdout.write("\r[100%%] %i threads, %i comments\n" % (len(threads), len(comments)))
|
|
|
|
|
|
|
|
orphans = set(map(lambda e: e.attrib.get(dsq + "id"), tree.findall("%spost" % ns))) - comments
|
|
|
|
if orphans:
|
|
|
|
print("Found %i orphans:" % len(orphans))
|
|
|
|
for post in tree.findall("%spost" % ns):
|
|
|
|
if post.attrib.get(dsq + "id") not in orphans:
|
|
|
|
continue
|
|
|
|
|
|
|
|
print(" * %s by %s <%s>" % (post.attrib.get(dsq + "id"),
|
|
|
|
post.find("%sauthor/%sname" % (ns, ns)).text,
|
|
|
|
post.find("%sauthor/%semail" % (ns, ns)).text))
|
|
|
|
print(textwrap.fill(post.find("%smessage" % ns).text,
|
|
|
|
initial_indent=" ", subsequent_indent=" "))
|
|
|
|
print("")
|