import pycomm, os, lxml
import mcollect


class Resolver(object):
    def __init__(self):
        pass

    def zaopan(self, data):
        # print(mcollect.getMongo())

        re = self.mixbodyv(data, 'zaopan', '/html/head/script[2]/text()')
        return re

    def zhibo(self, data):
        re = self.mixbodyv(data, 'zhibo', '/html/head/script[1]/text()')
        return re

    def today(self, data):
        re = self.mixbodyv(data, 'today', '/html/head/script[2]/text()')
        return re

    def chain(self, data):
        re = self.mixbodyv(data, 'chain', '/html/head/script[2]/text()')
        return re

    def mixbodyv(self, data, fname, content_xpath):
        cpath = os.path.dirname(os.path.abspath(__file__))
        val = pycomm.readFile(cpath + '/tmpl/' + fname + '.tmpl')
        data = str(data.decode('utf-8'))

        doc = lxml.etree.HTML(data)
        # doc=lxml.etree.fromstring(data)  
        ls = doc.xpath(content_xpath)
        text = ls[0]
        ftext = pycomm.parseTag(val, 'CONTENT', text)
        ftext = str(ftext.encode('utf-8').decode('utf-8'))
        pycomm.saveFile(cpath + '/fcache/hg0088.' + fname + '.js', ftext)
        re = pycomm.exejs('node ' + cpath + '/fcache/hg0088.' + fname + '.js')
        return re
