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

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


class DrspyscraperPipeline(object):
    def process_item(self, item, spider):
        return item


class DatabasescraperPipeline(object):

    def __init__(self, db, user, passwd, host):
        self.db = db
        self.user = user
        self.passwd = passwd
        self.host = host

    @classmethod
    def from_crawler(cls, crawler):
        db_settings = crawler.settings.getdict("DB_SETTINGS")
        if not db_settings:
            raise NotConfigured
        db = db_settings['db']
        user = db_settings['user']
        passwd = db_settings['passwd']
        host = db_settings['host']
        return cls(db, user, passwd, host)

    def process_item(self, item, spider):
        try:
            query="""INSERT INTO scraped_data (main_category_id, sub_catgegory_id, product_id, url) VALUES (%s, %s, %s, %s, %s, %s)"""
            params=(self.main_category_id, self.sub_catgegory_id, self.product_id, data['url'])
            spider.cursor.insert(query,params)
            return item
        except Exception as e:
            self.connection.rollback()
            return False

    def close_spider(self, spider):
        self.conn.close()
