-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
productlist.py
165 lines (146 loc) Β· 6.1 KB
/
productlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import sys
import requests
import hashlib
import time
import json
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve variables from environment
app_key = os.getenv('APP_KEY')
app_secret = os.getenv('APP_SECRET')
session_key = os.getenv('SESSION_KEY')
# Command-line arguments
if len(sys.argv) != 4 or sys.argv[2] not in ['10', '20', '30']:
print("Usage: script.py <subject> <page_size> <page_number>")
print("Subject is the product title to be looked up.")
print("Page size options: 10, 20, 30")
sys.exit(1)
subject = sys.argv[1]
page_size = sys.argv[2]
page_number = sys.argv[3]
# API endpoint and parameters
url = 'https://eco.taobao.com/router/rest'
params = {
'app_key': app_key,
'format': 'json',
'method': 'alibaba.icbu.product.list',
'partner_id': 'apidoc',
'session': session_key,
'sign_method': 'md5',
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S"),
'v': '2.0',
'current_page': page_number,
'page_size': page_size,
'subject': subject,
'language': 'ENGLISH',
}
# Calculate sign
def calculate_sign(params, secret):
sorted_params = sorted(params.items())
sign_string = secret + ''.join([f'{k}{v}' for k, v in sorted_params]) + secret
return hashlib.md5(sign_string.encode('utf-8')).hexdigest().upper()
# Add sign to parameters
params['sign'] = calculate_sign(params, app_secret)
# Remove sensitive information for logging
def remove_sensitive_info(params):
safe_params = params.copy()
safe_params.pop('app_key', None)
safe_params.pop('session', None)
safe_params.pop('sign', None)
return safe_params
# Create logs directory if it doesn't exist
log_dir = 'api_logs'
os.makedirs(log_dir, exist_ok=True)
try:
# Make POST request
response = requests.post(url, data=params)
# Log the request details with timestamp (sensitive info removed)
request_time = time.strftime("%Y-%m-%d %H:%M:%S")
productlist_request_log = {
"Request Time": request_time,
"Request URL": url,
"Request Method": "POST",
"Request Headers": remove_sensitive_info(params)
}
with open(os.path.join(log_dir, 'productlist_request_log.txt'), 'a') as f:
f.write(json.dumps(productlist_request_log, indent=4) + "\n\n")
# Log the response details with timestamp
response_time = time.strftime("%Y-%m-%d %H:%M:%S")
productlist_response_log = {
"Response Time": response_time,
"Response Status Code": response.status_code,
"Response Headers": dict(response.headers),
"Response Body": response.text
}
with open(os.path.join(log_dir, 'productlist_response_log.txt'), 'a') as f:
f.write(json.dumps(productlist_response_log, indent=4) + "\n\n")
# Check response status code
if response.status_code == 200:
try:
# Parse JSON response
data = response.json()
# Check if error response
if 'error_response' in data:
error = data['error_response']
code = error.get('code', '')
msg = error.get('msg', '')
sub_msg = error.get('sub_msg', '')
print(f"API Error: {msg}. {sub_msg}")
# Log error details
error_log = {
"Error Time": time.strftime('%Y-%m-%d %H:%M:%S'),
"Error Code": code,
"Error Message": msg,
"Error Sub Message": sub_msg
}
with open(os.path.join(log_dir, 'productlist_error_log.txt'), 'a') as f:
f.write(json.dumps(error_log, indent=4) + "\n\n")
else:
# Save response body to JSON file
response_json_filename = os.path.join(log_dir, f'productlist_response_{response_time.replace(":", "").replace(" ", "_")}.json')
with open(response_json_filename, 'w') as json_file:
json.dump(data, json_file, indent=4)
# Check if expected response structure is present
if 'alibaba_icbu_product_list_response' in data:
product_list_response = data['alibaba_icbu_product_list_response']
products = product_list_response.get('products', {}).get('alibaba_product_brief_response', [])
if isinstance(products, list):
for product in products:
if isinstance(product, dict):
subject = product.get('subject', '')
print(f"Product Subject: {subject}")
else:
print("Invalid product structure.")
else:
print("No products found in response.")
else:
print("Unexpected JSON structure: alibaba_icbu_product_list_response not found.")
except json.JSONDecodeError as je:
print(f"Failed to parse JSON response: {je}")
# Log JSON decoding error
error_log = {
"JSON Decode Error Time": time.strftime('%Y-%m-%d %H:%M:%S'),
"Error Message": str(je)
}
with open(os.path.join(log_dir, 'productlist_error_log.txt'), 'a') as f:
f.write(json.dumps(error_log, indent=4) + "\n\n")
else:
print(f"Request failed with status code: {response.status_code}")
# Log request failure
failure_log = {
"Request Failure Time": time.strftime('%Y-%m-%d %H:%M:%S'),
"Status Code": response.status_code
}
with open(os.path.join(log_dir, 'productlist_error_log.txt'), 'a') as f:
f.write(json.dumps(failure_log, indent=4) + "\n\n")
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
# Log request exception
exception_log = {
"Request Exception Time": time.strftime('%Y-%m-%d %H:%M:%S'),
"Exception Message": str(e)
}
with open(os.path.join(log_dir, 'productlist_error_log.txt'), 'a') as f:
f.write(json.dumps(exception_log, indent=4) + "\n\n")