#! /usr/bin/python
# -*- encoding:utf-8 -*-

import re
import my_utils
import logging
from lxml import etree
from items import Item
from base_parse import BaseParse


class Wangyi1(BaseParse):
    def _parse(self, raw_html):
        raw_json_data = my_utils.get_json_data(raw_html)
        for sub_item in raw_json_data.get("messages"):
            item = Item()
            item.set('nid', sub_item.get("id"))
            times = sub_item.get("time")
            item.set('timestamp', my_utils.get_timestamp(times))
            msg = sub_item.get("msg")
            if msg:
                content_title = msg.get("content")
            else:
                content_title = ""
            title, abstract = my_utils.split_title_content(content_title)
            item.set('title', title)
            item.set('abstract', abstract)
            self._add_to_data(item)


class Wangyi2(BaseParse):
    def _parse(self, raw_html):
        root = etree.HTML(raw_html)
        lines = root.xpath("//div[contains(@class,'list_item clearfix')]")
        for div in lines:
            item = Item()
            item.set('url', "".join(div.xpath("./div/h2/a/@href")))
            item.set('status', 1)
            # http://news.cnstock.com/news/sns_bwkx/201606/3817181.htm
            item.set('nid', my_utils.get_hash_nid(url=item.url))
            item.set('title', "".join(div.xpath("./div/h2/a/text()")))
            times = "".join(div.xpath(".//span[@class='time']/text()"))
            item.set('timestamp', my_utils.get_timestamp(times))
            item.set('abstract', "".join(div.xpath("./div/p/text()")))
            self._add_to_data(item)


if __name__ == "__main__":
    pass
