# Path: crawler/gather/items.py
# class ChannelItem(Item):
#     office_id = Field()
#     short = Field()
#     name = Field()
#     image = Field()
#     url = Field()
# 
# class RoomItem(Item):
#     office_id = Field()
#     name = Field()
#     image = Field()
#     url = Field()
#     online = Field()
#     host = Field()
#     channel = Field()
#     followers = Field()
#     description = Field()
#     announcement = Field()
#     start_time = Field()


from scrapy import Spider, Request
from ..items import ChannelItem, RoomItem
import json

class HuyaSpider(Spider):
    name = 'huya'
    allowed_domains = ['huya.com']
    start_urls = [
        'http://www.huya.com/g'
    ]
    custom_settings = {
        'SITE': {
            'code': 'huya',
            'name': '虎牙',
            'description': '虎牙直播-中国领先的互动直播平台',
            'url': 'http://www.huya.com',
            'image': 'http://a.dwstatic.com/huya/main/img/logo.png',
            'show_seq': 2
        }
    }

    def parse(self, response):
        room_query_list = []
        for a_element in response.xpath('//li[@class="game-list-item"]/a'):
            url = a_element.xpath('@href').extract_first()
            short = url[url.rfind('/') + 1:]
            report_attr = json.loads(a_element.xpath('@report').extract_first())
            office_id = report_attr['game_id']
            img_element = a_element.xpath('img')[0]
            name = img_element.xpath('@title').extract_first()
            image = img_element.xpath('@data-original').extract_first()
            yield ChannelItem({
                'office_id': office_id,

# Path: crawler/gather/items.py
# class ChannelItem(Item):
#     office_id = Field()
#     short = Field()
#     name = Field()
#     image = Field()
#     url = Field()
# 
# class RoomItem(Item):
#     office_id = Field()
#     name = Field()
#     image = Field()
#     url = Field()
#     online = Field()
#     host = Field()
#     channel = Field()
#     followers = Field()
#     description = Field()
#     announcement = Field()
#     start_time = Field()


# Path: crawler/gather/spiders/huya.py
from scrapy import Spider, Request
from ..items import ChannelItem, RoomItem
import json


class HuyaSpider(Spider):
    name = 'huya'
    allowed_domains = ['huya.com']
    start_urls = [
        'http://www.huya.com/g'
    ]
    custom_settings = {
        'SITE': {
            'code': 'huya',
            'name': '虎牙',
            'description': '虎牙直播-中国领先的互动直播平台',
            'url': 'http://www.huya.com',
            'image': 'http://a.dwstatic.com/huya/main/img/logo.png',
            'show_seq': 2
        }
    }

    def parse(self, response):
        room_query_list = []
        for a_element in response.xpath('//li[@class="game-list-item"]/a'):
            url = a_element.xpath('@href').extract_first()
            short = url[url.rfind('/') + 1:]
            report_attr = json.loads(a_element.xpath('@report').extract_first())
            office_id = report_attr['game_id']
            img_element = a_element.xpath('img')[0]
            name = img_element.xpath('@title').extract_first()
            image = img_element.xpath('@data-original').extract_first()
            yield ChannelItem({
                'office_id': office_id,

                'short': short,