import json
import logging

import requests
import scrapy


class FtsAgent(scrapy.Spider):
    name = 'poll_agent'
    start_urls = ['https://www.baidu.com']
    insert_api = 'http://39.98.192.5:28085/tduck-api/user/form/data/public/create'

    def __init__(self, params=None, *args, **kwargs):
        super(FtsAgent, self).__init__(*args, **kwargs)
        json_params = json.loads(params)
        if 'job_id' in json_params:
            self.job_id = json_params['job_id']

    def parse(self, response, **kwargs):
        json_data = {
            "completeTime": "",
            "extValue": "",
            "formKey": "6sy8hnt8",
            "formType": 1,
            "originalData": {
                "image_select1706180792742label": "选项一"
            },
            "submitBrowser": "",
            "submitOs": "",
            "submitUa": {
                "browser": "",
                "cpu": "",
                "device": {},
                "engine": {},
                "os": {},
                "ua": ""},
            "wxUserInfo": {}
        }

        rsp = requests.post(self.insert_api, json=json_data)
        logging.info(rsp.text)
        yield
