# -*- coding: utf-8 -*-
import scrapy
import json
import os
import codecs
from AnimeSpider.items import AnimespiderItem


class AinmelinklistSpider(scrapy.Spider):
    name = 'AinmeLinkList'
    allowed_domains = ['bilibili.com']
    start_urls = ['http://bilibili.com/']

    def start_requests(self):
        jsonpath = os.path.dirname(__file__) + '/output'
        jsonfile = codecs.open('%s/AinmeList_items.json' % jsonpath, 'r', encoding='utf-8')
        for line in jsonfile:
            ainme = json.loads(line)
            ainmename = ainme["name"]
            url = ainme["link"].replace("//", "https://")
            yield scrapy.Request(url=url, callback=self.parse, meta={'ainmename': ainmename})

    def parse(self, response):
        item = AnimespiderItem()
        item["info_link"] = response.css(".media-title").xpath('@href').get()
        yield item

[(16, 'codecs.open', 'codecs.open', False, 'import codecs\n'), (24, 'AnimeSpider.items.AnimespiderItem', 'AnimespiderItem', False, 'from AnimeSpider.items import AnimespiderItem\n'), (15, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (18, 'json.loads', 'json.loads', False, 'import json\n'), (21, 'scrapy.Request', 'scrapy.Request', False, 'import scrapy\n')]