import sqlite3
import os
import json

response_data_path = r'E:\yuxin\darkweb\all'

if __name__ == "__main__":
    local_db = sqlite3.connect('data.db')
    local_db_cursor = local_db.cursor()

    for file_name in os.listdir(response_data_path):
        try:
            _file = open(os.path.join(response_data_path, file_name), 'r', encoding='utf-8')
            _content = _file.read()
            _file.close()
            rsp_body = json.loads(_content)
            goods = rsp_body['data']['goods']
            for item in goods:
                _id = item['id']
                _price = item['price']
                _total = item['total']
                _sales = item['sales']
                _lmt = item['lmt']
                _ctime = item['ctime']
                _mtime = item['mtime']
                _itime = item['itime']
                _read_count = item['read_count']
                _name = item['name']
                _title = item['title']
                _intro = item['intro']
                SQL_INSERT = f"INSERT INTO search_result VALUES " \
                             f"('{_id}', {_price}, {_total}, {_sales}, {_lmt}, {_ctime}, {_mtime}, {_itime}, " \
                             f"{_read_count}, '{_name}', '{_title}', '{_intro}');"
                local_db_cursor.execute(SQL_INSERT)
                local_db.commit()
        except Exception as e:
            print(repr(e))

    local_db.close()
