# -*- 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

from scrapy.exporters import CsvItemExporter
from scrapy.conf import settings
#import pymongo


# class MongoProductPipeline(object):
#     collection_name = 'products'

#     def __init__(self):
#         connection = pymongo.MongoClient(settings['MONGODB_SERVER'],
#                                          settings['MONGODB_PORT'])
#         db = connection[settings['MONGODB_DB']]
#         self.collection = db[settings['MONGODB_COLLECTION']]

#     def process_item(self, item, spider):
#         self.collection.insert(dict(item))
#         return item


class ProductPipeline(object):
    def __init__(self):
        self.file = open('Exported.csv', 'wb')
        self.exporter = CsvItemExporter(self.file)
        self.exporter.start_exporting()

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item
