import os
import sys
import time
import django
local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(local_path)
if local_path not in sys.path:
    sys.path.append(local_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
django.setup()
from django.conf import settings
from smartfarming.models.pests_bank import MongoPestBank
from smartfarming.models.worm_forecast import MongoBZYphoto, MongoCBDphoto
import json
import requests
import pymongo
from urllib import parse


def dsj_qxz_conf_info():
    user = parse.quote_plus("root")
    passwd = parse.quote_plus("yfkj@6020")
    # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
    myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
    # 指定数据库
    db = myclient.smartfarming
    # 指定集合
    collection = db.sa_pest_bank
    pests = collection.find({})
    for i in pests:
        bank = MongoPestBank.objects.create(
            course=i["course"],
            name=i["name"],
            img_urls=i["img_urls"],
            prevention=i["prevention"],
            search=i["search"],
            code=i["code"]
        )
    print(f"数量:  {pests.count()}")
    return True


if __name__ == "__main__":
    dsj_qxz_conf_info()