# -*- coding: utf-8 -*-

import os
import shutil
import zipfile

from tempfile import mktemp
from lxml import etree
from json import loads


class GoogleReaderTakeout(object):

    def __init__(self, file_obj):
        self.directory = ""
        self.temp = mktemp()
        self._unpack(file_obj)

    def _unpack(self, file_obj):
        z = zipfile.ZipFile(file_obj)
        z.extractall(path=self.temp)
        for (path, dirs, files) in os.walk(self.temp):
            if 'subscriptions.xml' in files:
                self.directory = path
        z.close()

    def _load_xml_file(self, filename):
        subscriptions = '%s/%s.xml' % (self.directory, filename)
        return etree.XML(''.join(open(subscriptions).readlines()))

    def _load_json_file(self, filename):
        json_file = '%s/%s.json' % (self.directory, filename)
        return loads(''.join(open(json_file).readlines()))

    def subscriptions(self):
        grab = self._load_xml_file('subscriptions')
        data = []
        for element in grab.xpath('//opml/body/outline'):
            if element.get('type') is None:
                # if type is none it's a category
                for child in element.getchildren():
                    data.append({
                        'text': child.get('text'),
                        'title': child.get('title'),
                        'type': child.get('type'),
                        'xmlUrl': child.get('xmlUrl'),
                        'htmlUrl': child.get('htmlUrl'),
                        'category': element.get('title'),
                    })
            else:
                data.append({
                    'text': element.get('text'),
                    'title': element.get('title'),
                    'type': element.get('type'),
                    'xmlUrl': element.get('xmlUrl'),
                    'htmlUrl': element.get('htmlUrl'),
                    'category': None,
                })
        return data

    def __getattr__(self):
        def method(*args, **kwargs):
            getattr(self, '_load_json_file')(*args, **kwargs)
        return method

    def __del__(self):
        shutil.rmtree(self.temp)

@@ -1067,576 +1067,22 @@
     
-if element.get('type') is None:%0A                # if type is none it's a category%0A                for child in element.getchildren():%0A                    data.append(%7B%0A                        'text': child.get('text'),%0A                        'title': child.get('title'),%0A                        'type': child.get('type'),%0A                        'xmlUrl': child.get('xmlUrl'),%0A                        'htmlUrl': child.get('htmlUrl'),%0A                        'category': element.get('title'),%0A                    %7D)%0A            else:%0A                data.append(%7B%0A    
+data.append(%7B%0A
     
@@ -1138,20 +1138,16 @@
         
- 
-   
 'title':
@@ -1185,20 +1185,16 @@
         
-    
 'type': 
@@ -1230,20 +1230,16 @@
         
-   
- 
 'xmlUrl'
@@ -1255,36 +1255,32 @@
 .get('xmlUrl'),%0A
-    
                 
@@ -1318,50 +1318,8 @@
 '),%0A
-                    'category': None,%0A    
     

Remove changes that belong to another feature branch.