import json
import logging

import requests
import scrapy


class FtsAgent(scrapy.Spider):
    name = 'file_agent'
    start_urls = ['https://www.baidu.com']
    insert_api = 'http://39.104.83.207:28082/FileExplorer/commonAPI/upload'
    file_path = r"D:\code\graham.py"

    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):
        form_data = {
            'objMark': 'file-test',
            'userName': 'JSC',
            'userID': 'JSC',
            'sysCode': 'EODS'
        }
        files = {'file': ('filename', open(self.file_path, 'rb'))}
        rsp = requests.post(self.insert_api, data=form_data, files=files)
        logging.info(rsp.text)
        yield
