Skip to content

Commit 3a4ab14

Browse files
Merge pull request #70 from Dimlitter/rootphantomer/issue69
Rootphantomer/issue69
2 parents 3844e32 + f1137be commit 3a4ab14

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

auto_comment_plus.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,24 @@ def download_image(img_url, file_name):
106106
fullUrl = f"https:{img_url}"
107107
response = requests.get(fullUrl)
108108
if response.status_code == 200:
109-
with open(file_name, "wb") as file:
109+
directory = "img"
110+
if not os.path.exists(directory):
111+
# 如果目录不存在,创建目录
112+
os.makedirs(directory)
113+
file_path = os.path.join(directory, file_name)
114+
with open(file_path, "wb") as file:
110115
file.write(response.content)
111-
return file_name
116+
return file_path
112117
else:
113118
print("Failed to download image")
114119
return None
115120

116121

117122
# 上传图片到JD接口
118-
def upload_image(file_path, session, headers):
123+
def upload_image(filename, file_path, session, headers):
119124

120125
files = {
121-
"name": (None, file_path),
126+
"name": (None, filename),
122127
# 不需要 PHPSESSID 时可以忽略
123128
# 如果需要的话,可以从初次登录响应中获取
124129
"Filedata": (file_path, open(file_path, "rb"), "image/jpeg"),
@@ -359,7 +364,9 @@ def ordinary(N, opts=None):
359364
downloaded_file1 = download_image(imgurl1, imgName1)
360365
# 上传图片
361366
if downloaded_file1:
362-
imgPart1 = upload_image(imgName1, session, headers)
367+
imgPart1 = upload_image(
368+
imgName1, downloaded_file1, session, headers
369+
)
363370
# print(imgPart1) # 和上传图片操作
364371
if imgPart1.status_code == 200:
365372
imgurl1 = f"{imgBasic}{imgPart1.text}"
@@ -372,7 +379,9 @@ def ordinary(N, opts=None):
372379
downloaded_file2 = download_image(imgurl2, imgName2)
373380
# 上传图片
374381
if downloaded_file2:
375-
imgPart2 = upload_image(imgName2, session, headers)
382+
imgPart2 = upload_image(
383+
imgName2, downloaded_file2, session, headers
384+
)
376385
# print(imgPart2) # 和上传图片操作
377386
if imgPart2.status_code == 200:
378387
imgurl2 = f"{imgBasic}{imgPart2.text}"
@@ -409,7 +418,7 @@ def ordinary(N, opts=None):
409418
# 当发送后的状态码 200,并且返回值里的 success 是 true 才是晒图成功,此外所有状态均为晒图失败
410419
opts["logger"].info(f"\t{i}.评价订单\t{oname}[{oid}]评论成功")
411420
else:
412-
opts["logger"].info(f"\t{i}.评价订单\t{oname}[{oid}]评论失败")
421+
opts["logger"].warning(f"\t{i}.评价订单\t{oname}[{oid}]评论失败")
413422
opts["logger"].debug("Sleep time (s): %.1f", ORDINARY_SLEEP_SEC)
414423
time.sleep(ORDINARY_SLEEP_SEC)
415424
idx += 1

jdspider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def getData(
129129
)
130130
if len(self.productsId) == 0:
131131
default_logger.warning(f"完了,self.productsId是空的,后面会导致默认评价了")
132+
sum_ = 0
132133
elif 0 < len(self.productsId) < 8: # limit the sum of products
133134
sum_ = len(self.productsId)
134135
else:

0 commit comments

Comments
 (0)