HEX
Server: Apache
System: Linux vps87182.inmotionhosting.com 3.10.0-1160.119.1.vz7.224.4 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64
User: mother99 (1002)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/mother99/jacksonholdingcompany.com/erp__b710813/myscript/distributor_connect.py
import paramiko
import csv
import json
import requests
from io import StringIO

# SFTP details
def download_files():
    host = 'sftp.ingrammicro.com'
    port = 22
    username = 'your_username'
    password = 'your_password'
    remote_path = '/path/to/product/data/file.csv'
    
    with paramiko.SSHClient() as client:
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(host, port, username, password)
        with client.open_sftp() as sftp:
            with sftp.open(remote_path, 'r') as file_handle:
                data = file_handle.read()
    return data

# Parse CSV data
def parse_data(data):
    products = []
    file_str = StringIO(data.decode('utf-8'))
    reader = csv.DictReader(file_str)

    for row in reader:
        products.append({
            'label': row['Product Name'],
            'ref': row['SKU'],
            'price': float(row['Price'])
            # Add other product details as needed
        })
    return products

# Post data to Dolibarr API
def post_to_dolibarr(products):
    dolibarr_api_url = 'https://your.dolibarr.com/api/index.php/products'
    api_key = 'your_api_key'
    headers = {'DOLAPIKEY': api_key, 'Content-Type': 'application/json'}

    for product in products:
        response = requests.post(dolibarr_api_url, headers=headers, data=json.dumps(product))
        if response.status_code != 201:
            print(f"Error posting product {product['ref']}: {response.text}")
        else:
            print(f"Product {product['ref']} added successfully")

# Main execution function
def main():
    data = download_files()
    products = parse_data(data)
    post_to_dolibarr(products)

if __name__ == "__main__":
    main()