# -*- encoding: utf-8 -*-
#
# Copyright 2012, Martin Zimmermann <info@posativ.org>. All rights reserved.
# License: BSD Style, 2 clauses. see isso/__init__.py
#
# TODO
#
# - export does not include website from commenters
# - Disqus includes already deleted comments

from time import mktime, strptime
from urlparse import urlparse
from collections import defaultdict

from xml.etree import ElementTree

from isso.models import Comment


ns = '{http://disqus.com}'
dsq = '{http://disqus.com/disqus-internals}'


def insert(db, thread, comments):

    path = urlparse(thread.find('%sid' % ns).text).path
    remap = dict()

    for item in sorted(comments, key=lambda k: k['created']):

        parent = remap.get(item.get('dsq:parent'))
        comment = Comment(created=item['created'], text=item['text'],
                          author=item['author'], email=item['email'], parent=parent)

        rv = db.add(path, comment)
        remap[item['dsq:id']] = rv.id


def disqus(db, xml):

    tree = ElementTree.fromstring(xml)
    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(
                post.find('%screatedAt' % ns).text, '%Y-%m-%dT%H:%M:%SZ'))
        }

        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)

    for thread in tree.findall('%sthread' % ns):
        id = thread.attrib.get(dsq + 'id')
        if id in res:
            insert(db, thread, res[id])

@@ -261,16 +261,71 @@
 mments%0A%0A
+from __future__ import division%0A%0Aimport sys%0Aimport os%0A%0A
 from tim
@@ -349,16 +349,23 @@
 strptime
+, sleep
 %0Afrom ur
@@ -1041,16 +1041,20 @@
 (db, xml
+file
 ):%0A%0A    
@@ -1076,22 +1076,21 @@
 ree.
-fromstring(xml
+parse(xmlfile
 )%0A  
@@ -1758,19 +1758,145 @@
 m)%0A%0A    
-for
+num = len(tree.findall('%25sthread' %25 ns))%0A    cols = int(os.popen('stty size', 'r').read().split()%5B1%5D)%0A%0A    threads = 0%0A    for i,
  thread 
@@ -1894,24 +1894,34 @@
 , thread in 
+enumerate(
 tree.findall
@@ -1937,18 +1937,254 @@
 d' %25 ns)
+)
 :%0A
+%0A        if int(round((i+1)/num, 2) * 100) %25 13 == 0:%0A%0A            sys.stdout.write(%22%5Cr%25s%22 %25 (%22 %22*cols))%0A            sys.stdout.write(%22%5Cr%5B%25i%25%25%5D  %25s%22 %25 (((i+1)/num * 100), thread.find('%25sid' %25 ns).text))%0A            sys.stdout.flush()%0A%0A
         
@@ -2240,16 +2240,41 @@
 in res:%0A
+            threads += 1%0A
         
@@ -2305,8 +2305,156 @@
 es%5Bid%5D)%0A
+%0A    sys.stdout.write(%22%5Cr%25s%22 %25 (%22 %22*cols))%0A    sys.stdout.write(%22%5Cr%5B100%25%25%5D  %25i threads, %25i comments%22 %25 (threads, len(tree.findall('%25spost' %25 ns))))%0A

add progress bar to migration