I currently have a program that I wrote to work with the woocommerce API to get orders daily from our websites. The program runs(most of the time) without issue. However, randomly, I will get errors like the image attached. I have the proper packages to make the program work but it gets hung up and fails around 5% of the time. This is a big issue as I am going to automate the process. Does anyone have any idea why this error would only happen a small amount of the time? See code below for context. Thank you.
import csv
import time
from datetime import datetime, timedelta, date
import os
from woocommerce import API
# Define woocommerce api credentials
wcapi = API(
url="https://mysite/",
consumer_key="ck_123456789", # Your consumer key
consumer_secret="cs_secret", # Your consumer secret
wp_api=True, # Enable the WP REST API integration
version="wc/v3")
# set a time query for orders over the passed 24 hours
yesterday = datetime.now() - timedelta(hours=24)
today = date.today()
timeNow = datetime.now()
current_time = timeNow.strftime("%H:%M:%S")
file_date = datetime.now().strftime("%m-%d-%Y-%H%M")
r = wcapi.get(f"orders?after={yesterday}")
meta = []
# If the get request returns a valid status
# get email error reporting set up if file sets up
if r.status_code in range(200, 299):
time.sleep(1)
# Fill the empty list with the json from the get request
orders = r.json()
# set the path that you want to save the file
dir_path = "I:\store\orders"
# set the name of the file with todays date
file_name = f"store_orders-{file_date}.csv"
# create the full file path
file_path = os.path.join(dir_path, file_name)
with open(file_path, 'w', newline='', encoding='utf_8') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
# delcare all of the column headers in the csv file
header = ['Order ID', 'Order Number', 'Order Created at', 'Status', 'Currency', 'Total', 'Total Shipping',
'Total Discount',
'Shipping Tax', 'Total Tax', 'Total Items', 'Cart Tax', 'Discount Tax', 'Total Fees', 'Total Refunds',
'Net',
'Billing Fist Name', 'Billing Last Name', 'Billing Company', 'Billing address1', 'Billing address2',
'Billing city', 'Billing state', 'Billing postcode', 'Billing Country', 'Email', 'Phone',
'Shipping Fist Name', 'Shipping Last Name', 'Shipping Company', 'Shipping address1',
'Shipping address2',
'Shipping city', 'Shipping state', 'Shipping postcode', 'Shipping Country', 'Order updated at',
'order completed at', 'order paid at', 'payment method', 'payment method title',
'shipping method title',
'Transaction ID', 'Order key', 'customer IP', 'Customer browser', 'Customer note', 'customer id',
'customer role', 'created via', 'referring site', 'coupon code', 'coupon amount', 'fees', 'cogs',
'gross profit', 'line item id', 'line item product id', 'line item variation id', 'line item name',
'line item sku', 'line itme qty', 'line item meta', 'line item subtotal', 'line item subtotal tax',
'line item total', 'line item total tax', 'line item price', 'line item cogs']
# write headers to csv file
writer.writerow(header)
for order in orders:
# create dictionary reference variables for nested dictionaries to clean up keys below
shipping = order['shipping']
billing = order['billing']
shipping_lines = order['shipping_lines']
# this for look goes through the list of shipping_line items for shipping information and matches the pairs
# in dictionary format to be accessed below
for ship_info in shipping_lines:
pass
for line_item in order['line_items']:
# create the new row of data to send to csv
row = []
# create a dictionary out of a list of meta data and assign the meta data list to the meta var
mt = line_item['meta_data']
for m in mt:
if m['key'] == 'vpc-cart-data':
meta = m
#build csv
row.append(order['id'])
row.append('#'+ order['number'])
row.append(order['date_created'])
row.append(order['status'])
row.append(order['currency'])
row.append(order['total'])
row.append(shipping_lines[0]['total'])
row.append(order['discount_total'])
row.append(order['shipping_tax'])
row.append(order['total_tax'])
row.append(line_item['quantity'])
row.append(order['cart_tax'])
row.append(order['discount_tax'])
row.append(order['fee_lines'])
row.append(order['refunds'])
row.append(order['total'])
row.append(billing['first_name'])
row.append(billing['last_name'])
row.append(billing['company'])
row.append(billing['address_1'])
row.append(billing['address_2'])
row.append(billing['city'])
row.append(billing['state'])
row.append(billing['postcode'])
row.append(billing['country'])
row.append(billing['email'])
row.append(billing['phone'])
row.append(shipping['first_name'])
row.append(shipping['last_name'])
row.append(shipping['company'])
row.append(shipping['address_1'])
row.append(shipping['address_2'])
row.append(shipping['city'])
row.append(shipping['state'])
row.append(shipping['postcode'])
row.append(shipping['country'])
row.append(order['date_modified'])
row.append(order['date_completed'])
row.append(order['date_paid'])
row.append(order['payment_method'])
row.append(order['payment_method_title'])
row.append(order['payment_method_title'])
row.append(order['transaction_id'])
row.append(order['order_key'])
row.append(order['customer_ip_address'])
row.append(order['customer_user_agent'])
row.append(order['customer_note'])
row.append(order['customer_id'])
row.append(order['customer_id'])
row.append(order['created_via'])
row.append(order['payment_url'])
row.append(order['discount_total'])
row.append(order['discount_total'])
row.append(order['customer_note'])
row.append(order['discount_tax'])
row.append(order['discount_tax'])
row.append(line_item['id'])
row.append(line_item['product_id'])
row.append(line_item['variation_id'])
row.append(line_item['name'])
row.append(line_item['sku'])
row.append(line_item['quantity'])
row.append(line_item['parent_name'])
row.append(line_item['subtotal'])
row.append(line_item['subtotal_tax'])
row.append(line_item['total'])
row.append(line_item['total_tax'])
row.append(line_item['price'])
row.append(line_item['price'])
# write each row to the csv file
writer.writerow(row)
# clear accumulated meta data after row is written
meta.clear()
csv_file.close()