import sys, os
import json
import logging
import time
import redis
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(sys.path[0]))))
from amazon_spider.utils.read_db_data import ReadDb, ReadCookie
from scrapy.utils.project import get_project_settings

REDIS_CONF = get_project_settings().get('REDIS')


logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s %(message)s',
                            level=logging.INFO)


def get_redis_client():
    client = redis.StrictRedis(**REDIS_CONF)
    return client


redis_client = get_redis_client()


def seeds(site="us"):
    seed = list(list(i) for i in ReadDb(site).read_db_self_return().values)
    pipe = redis_client.pipeline(transaction=False)
    for job in seed:
        meta = {
            "asin": job[1],
            "site": job[2],
        }
        k = json.dumps(meta)
        # pipe.sadd(f"{site}_variat_seed", k)
        pipe.zadd(f"{site}_detail_returns_seed", {k: int(time.time())})
    pipe.execute()


if __name__ == '__main__':
    for i in ["us"]:
    # for i in ["us",  "fr", "de", "es"]:
        logging.info(f"推送{i}任务——————")
        seeds(i)
        logging.info(f"推送{i}任务完成——————")
        time.sleep(5)

