import logging
import dataset
from sqlalchemy.dialects.postgresql import JSONB
logger = logging.getLogger(__name__)


class PgPipeline(object):
    def __init__(self, **kwargs):
        self.args = kwargs

    @classmethod
    def from_crawler(cls, crawler):
        args = crawler.settings.get('PG_PIPELINE', {})
        return cls(**args)

    def open_spider(self, spider):
        if self.args.get('connection'):
            self.db = dataset.connect(self.args.get('connection'))
            self.table = self.db[self.args.get('table_name')]
            self.pkey = self.args.get('pkey')
            self.types = self.args.get('types')
            self.ignore_identical = self.args.get('ignore_identical')
            self.table.create_index([self.pkey])
            self.table.create_index(self.ignore_identical)
            self.enabled = True

    def process_item(self, item, spider):
        if self.enabled:
            logger.debug("INSERT %s", item)
            self.table.insert_ignore(
                item, self.ignore_identical, types=self.types)
        else:
            logger.debug("DISABLED")
        return item
