# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os

from scrapy import Request
from scrapy.pipelines.images import ImagesPipeline

from luoxia import settings


class LuoxiaPipeline(object):
    def process_item(self, item, spider):
        title= item['title']
        bookname = item['bookname']
        titlename = item['titlename']
        text = item['text']
        path = "books/%s/%s/" % (title, bookname)
        if not os.path.exists(path):
            os.makedirs(path)
        with open(path+titlename+'.txt', 'a', encoding='utf-8') as f:
            f.write(text)
        return item

class LuoxiaImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for url in item['image_urls']:
            yield Request(url, meta={'title': item['title'],
                                     'bookname': item['bookname']})

    def item_completed(self, results, item, info):
        # 将下载完成后的图片路径设置到item中
        item['images'] = [x for ok, x in results if ok]
        return item

    def file_path(self, request, response=None, info=None):
        # 为每本书创建一个目录，存放她自己所有的图片
        title = request.meta['title']
        bookname = request.meta['bookname']
        book_dir = os.path.join(settings.IMAGES_STORE, title +'/'+ bookname)
        if not os.path.exists(book_dir):
            os.makedirs(book_dir)
        # 从连接中提取扩展名
        try:
            ext_name = request.url.split(".")[-1]
        except:
            ext_name = 'jpg'
        # 返回的相对路径
        return '%s/%s/%s.%s' % (title, bookname, bookname, ext_name)
[((43, 19, 43, 76), 'os.path.join', 'os.path.join', ({(43, 32, 43, 53): 'settings.IMAGES_STORE', (43, 55, 43, 75): "title + '/' + bookname"}, {}), "(settings.IMAGES_STORE, title + '/' + bookname)", False, 'import os\n'), ((22, 15, 22, 35), 'os.path.exists', 'os.path.exists', ({(22, 30, 22, 34): 'path'}, {}), '(path)', False, 'import os\n'), ((23, 12, 23, 29), 'os.makedirs', 'os.makedirs', ({(23, 24, 23, 28): 'path'}, {}), '(path)', False, 'import os\n'), ((44, 15, 44, 39), 'os.path.exists', 'os.path.exists', ({(44, 30, 44, 38): 'book_dir'}, {}), '(book_dir)', False, 'import os\n'), ((45, 12, 45, 33), 'os.makedirs', 'os.makedirs', ({(45, 24, 45, 32): 'book_dir'}, {}), '(book_dir)', False, 'import os\n'), ((31, 18, 32, 67), 'scrapy.Request', 'Request', (), '', False, 'from scrapy import Request\n')]