utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # 控制流程的暂停和继续
  2. import csv
  3. import os
  4. import time
  5. import uuid
  6. import keyboard
  7. from openpyxl import Workbook, load_workbook
  8. import requests
  9. from urllib.parse import urlparse
  10. def is_valid_url(url):
  11. try:
  12. result = urlparse(url)
  13. return all([result.scheme, result.netloc])
  14. except ValueError:
  15. return False
  16. def check_pause(key, event):
  17. while True:
  18. if keyboard.is_pressed(key): # 按下p键,暂停程序
  19. if event._flag == False:
  20. print("任务执行中,长按p键暂停执行。")
  21. print("Task is running, long press 'p' to pause.")
  22. # 设置Event的值为True,使得线程b可以继续执行
  23. event.set()
  24. else:
  25. # 设置Event的值为False,使得线程b暂停执行
  26. print("任务已暂停,长按p键继续执行...")
  27. print("Task paused, press 'p' to continue...")
  28. event.clear()
  29. time.sleep(1) # 每秒检查一次
  30. def download_image(url, save_directory):
  31. # 定义浏览器头信息
  32. headers = {
  33. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
  34. }
  35. if is_valid_url(url):
  36. # 发送 GET 请求获取图片数据
  37. response = requests.get(url, headers=headers)
  38. # 检查响应状态码是否为成功状态
  39. if response.status_code == requests.codes.ok:
  40. # 提取文件名
  41. file_name = url.split('/')[-1].split("?")[0]
  42. # 生成唯一的新文件名
  43. new_file_name = file_name + '_' + \
  44. str(uuid.uuid4()) + '_' + file_name
  45. # 构建保存路径
  46. save_path = os.path.join(save_directory, new_file_name)
  47. # 保存图片到本地
  48. with open(save_path, 'wb') as file:
  49. file.write(response.content)
  50. print("图片已成功下载到:", save_path)
  51. print("The image has been successfully downloaded to:", save_path)
  52. else:
  53. print("下载图片失败,请检查此图片链接是否有效:", url)
  54. print(
  55. "Failed to download image, please check if this image link is valid:", url)
  56. else:
  57. print("下载图片失败,请检查此图片链接是否有效:", url)
  58. print("Failed to download image, please check if this image link is valid:", url)
  59. def get_output_code(output):
  60. try:
  61. if output.find("rue") != -1: # 如果返回值中包含true
  62. code = 1
  63. else:
  64. code = int(output)
  65. except:
  66. code = 0
  67. return code
  68. # 判断字段是否为空
  69. def isnull(s):
  70. return len(s) != 0
  71. def write_to_csv(file_name, data):
  72. with open(file_name, 'a', encoding='utf-8-sig', newline="") as f:
  73. f_csv = csv.writer(f)
  74. for line in data:
  75. f_csv.writerow(line)
  76. f.close()
  77. def write_to_excel(file_name, data):
  78. if os.path.exists(file_name):
  79. # 加载现有的工作簿
  80. wb = load_workbook(file_name)
  81. ws = wb.active
  82. else:
  83. # 创建新的工作簿和工作表
  84. wb = Workbook()
  85. ws = wb.active
  86. # 追加数据到工作表
  87. for line in data:
  88. ws.append(line)
  89. # 保存工作簿
  90. wb.save(file_name)
  91. class Time:
  92. def __init__(self, type1=""):
  93. self.t = int(round(time.time() * 1000))
  94. self.type = type1
  95. def end(self):
  96. at = int(round(time.time() * 1000))
  97. print("Time used for", self.type, ":", at - self.t, "ms")