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

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


class Stcn1(BaseParse):
    def _parse(self, raw_html):
        root = etree.HTML(raw_html)
        lines = root.xpath("/html/body//div[contains(@class,'mainlist')][contains(@id,'mainlist')]//li")
        for li in lines:
            item = Item()
            item.set('url', "".join(li.xpath(".//a/@href")))
            item.set('status', 1)
            nid_match = re.match(r".*/(\d*)/(\d*)/(\d*)\.shtml",item.url)
            if nid_match:
                item.set('nid', nid_match.group(1)+nid_match.group(2)+nid_match.group(3))
            item.set('title', "".join(li.xpath(".//a/@title")))
            times = "".join(li.xpath(".//span/text()"))
            item.set('timestamp', my_utils.get_timestamp(times))
            self._add_to_data(item)


class Stcn2(BaseParse):
    def _parse(self, raw_html):
        root = etree.HTML(raw_html)
        # lines = root.xpath("/html/body//div[contains(@class,'mainlist')][contains(@id,'mainlist')]//li")
        lines = root.xpath("//ul[contains(@id,'news_list2')]/li")
        # print "lines", lines
        for li in lines:
            item = Item()
            item.set('url', "".join(li.xpath(".//a[not(@class='a1')]/@href")))
            item.set('status', 1)
            nid_match = re.match(r".*/(\d*)/(\d*)/(\d*)\.shtml", item.url)
            if nid_match:
                item.set('nid', nid_match.group(1) + nid_match.group(2) + nid_match.group(3))
            item.set('title', "".join(li.xpath(".//a[not(@class='a1')]/text()")))
            times = "".join(li.xpath(".//span/text()"))
            item.set('timestamp', my_utils.get_timestamp(times))
            self._add_to_data(item)


if __name__=="__main__":
    pass
