# coding: utf-8
"""
    burglar
    ~~~~~~~

    Let's steal something and publish it.
"""

import os
from . import daily, zhuanlan, weixin
from .utils import write_feed, logger

__version__ = '0.1'
__author__ = 'Hsiaoming Yang <me@lepture.com>'


class Burglar(object):
    """A burglar rob in the night.

    :param sitedir: directory for storing feed files.
    """
    def __init__(self, sitedir):
        self.sitedir = sitedir

    def __call__(self, item):
        self.feed(item)

    def feed(self, item):
        assert 'type' in item
        item_type = item['type']
        if item_type == 'daily':
            name = 'daily'
            dest = os.path.join(self.sitedir, 'zhihu', name + '.xml')
            feed = daily.parse()
        elif item_type == 'zhuanlan':
            name = item['name']
            dest = os.path.join(self.sitedir, 'zhihu', name + '.xml')
            feed = zhuanlan.parse(item['title'], name)
        elif item_type == 'weixin':
            name = item['name']
            dest = os.path.join(self.sitedir, 'weixin', name + '.xml')
            feed = weixin.parse(item['title'], item['openid'])
        else:
            raise ValueError('Invalid item type')
        logger.info('Feeding %s - %s' % (item_type, name))
        write_feed(feed, dest)
        logger.info('Finished %s - %s' % (item_type, name))

@@ -358,24 +358,71 @@
 es.%0A    %22%22%22%0A
+    PARSERS = %5B'daily', 'zhuanlan', 'weixin'%5D%0A%0A
     def __in
@@ -529,16 +529,592 @@
 (item)%0A%0A
+    def parse_daily(self, item):%0A        feed = daily.parse()%0A        dest = os.path.join(self.sitedir, 'zhihu', 'daily.xml')%0A        return feed, dest%0A%0A    def parse_zhuanlan(self, item):%0A        name = item%5B'name'%5D%0A        feed = zhuanlan.parse(item%5B'title'%5D, name)%0A        dest = os.path.join(self.sitedir, 'zhihu', name + '.xml')%0A        return feed, dest%0A%0A    def parse_weixin(self, item):%0A        name = item%5B'name'%5D%0A        feed = weixin.parse(item%5B'title'%5D, item%5B'openid'%5D)%0A        dest = os.path.join(self.sitedir, 'weixin', name + '.xml')%0A        return feed, dest%0A%0A
     def 
@@ -1190,24 +1190,65 @@
 tem%5B'type'%5D%0A
+        assert item_type in self.PARSERS%0A
         if i
@@ -1307,139 +1307,12 @@
     
-    dest = os.path.join(self.sitedir, 'zhihu', name + '.xml')%0A            feed = daily.parse()%0A        elif item_type == 'zhuanlan'
+else
 :%0A  
@@ -1337,32 +1337,33 @@
 tem%5B'name'%5D%0A
+%0A
         
     dest = o
@@ -1354,389 +1354,112 @@
     
-    dest = os.path.join(self.sitedir, 'zhihu', name + '.xml')%0A            feed = zhuanlan.parse(item%5B'title'%5D
+logger.info('Feeding %25s - %25s' %25 (item_type
 , name)
+)
 %0A
- 
        
-elif item_type == 'weixin':%0A            name = item%5B'name'%5D%0A            dest = os.path.join(self.sitedir, 'weixin', name + '.xml')%0A            feed = weixin.parse(item%5B'title'%5D, item%5B'openid'%5D)%0A        else:%0A            raise ValueError('Invalid
+ parser = getattr(self, 'parse_%25s' %25
  item
- 
+_
 type
-'
 )%0A  
@@ -1468,57 +1468,32 @@
     
-logger.info('Feeding %25s - %25s' %25 (item_type, name)
+feed, dest = parser(item
 )%0A  

Refactor burglar