Esempio n. 1
0
class GetTaokDetail(object):
    def __init__(self, http_client):
        from taobao import Taobao
        from oss.oss_api import *

        self.logger = logging.getLogger("taoke")
        self.taobao = Taobao('12570801', 'fbab4f2ded890ae889e876ae0eee90b9', http=http_client)
        
    def generate_cate_tree(self, site, http, pid):
    
        parents_id = [pid, ]
        while len(parents_id) > 0:
            tmp_list = parents_id
            parents_id = []
            for cid in tmp_list:
                sub_cate = self._get_cate_list(cid)
                parents_id += [ unicode(e['cid']) for e in sub_cate if e['is_parent'] ]                
                self._save_and_output(sub_cate)
                import time
                time.sleep(3) # taobao call limit.
        pass
        
    def _get_cate_list(self, cid):
        cate = self.taobao.taobao_itemcats_get(fields='cid,parent_cid,name,is_parent', parent_cid=cid)
        return cate["itemcats_get_response"]['item_cats']['item_cat']

    def _get_cate_props(self, cid):
        cate = self.taobao.taobao_itemprops_get(fields='pid,name,must,multi,prop_values', cid=cid)
        return cate["itemprops_get_response"]['item_props']['item_prop']
        
    def _save_props(self, cid, props):
        cid = unicode(cid)
        path = u"props/%s/%s.data" % (cid[-1], cid)
        
        values = {}
        for item in props:
            key = item['pid']            
            values[u'%s_name' % key] = item['name']
            if 'prop_values' not in item: continue
            for v in item['prop_values']['prop_value']:
                vk = u'%s_%s' % (key, v['vid'])
                values[vk] = v['name']
        
        import codecs
        if not os.path.isdir(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))    
        
        logging.info(u"save props:%s" % path)
        fd = codecs.open(path, "w", "utf-8")
        fd.write(json.dumps(values, encoding='utf-8', ensure_ascii=False))
        fd.close()

    
    def _save_and_output(self, cate_list):
        for c in cate_list:
            #self.fd.write(unicode(c['cid']))
            #self.fd.write("-->")
            #self.fd.write(c['name'])
            self.fd.write(u"%s-->%s --> %s\n" % (c['cid'], c['name'], c['parent_cid']))
            print u"%s -> %s" % (c['cid'], c['name'])
            if not c['is_parent']:
                try:
                    cid = unicode(c['cid'])
                    props = self._get_cate_props(cid)
                    self._save_props(cid, props)
                except Exception, e:
                    logging.error("get cate props error:%s" % e)