test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import json
  4. import ssl
  5. import warnings
  6. from PIL import Image
  7. import ddddocr
  8. import onnxruntime
  9. import requests
  10. import os
  11. onnxruntime.set_default_logger_severity(3)
  12. ssl._create_default_https_context = ssl._create_unverified_context
  13. warnings.filterwarnings("ignore")
  14. def base64_2_img(img_path, base64_content, resize=False, width=400, height=200):
  15. # base64保存至图片
  16. img_content = base64_content.split(',')[-1]
  17. img_data = base64.b64decode(img_content)
  18. try:
  19. with open(img_path, mode="wb") as f:
  20. f.write(img_data)
  21. f.close()
  22. if resize:
  23. with Image.open(img_path) as image:
  24. resized_image = image.resize((width, height))
  25. resized_image.save(img_path)
  26. except Exception as e:
  27. print(f"base64_to_img error:{e}")
  28. class DzSpider(object):
  29. def __init__(self):
  30. self.folder = fr'{os.getcwd()}'
  31. self.session = requests.session()
  32. self.data = {}
  33. self.name = ''
  34. self.bookId = '1854909287941992450'
  35. self.topUpTemplateId = '1854833762266247169'
  36. self.user_name = '康帅'
  37. self.password = 'Ks25666'
  38. self.headers_str = '''
  39. Accept: application/json, text/plain, */*
  40. Accept-Language: zh-CN,zh;q=0.9
  41. Cache-Control: no-cache
  42. Connection: keep-alive
  43. Content-Type: application/x-www-form-urlencoded
  44. Origin: https://manage.zhangwenpindu.cn
  45. Pragma: no-cache
  46. Referer: https://manage.zhangwenpindu.cn/
  47. Sec-Fetch-Dest: empty
  48. Sec-Fetch-Mode: cors
  49. Sec-Fetch-Site: same-site
  50. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
  51. sec-ch-ua: "Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"
  52. sec-ch-ua-mobile: ?0
  53. sec-ch-ua-platform: "Windows"
  54. '''
  55. self.headers = dict(
  56. [[y.strip() for y in x.strip().split(':', 1)] for x in self.headers_str.strip().split('\n') if x.strip()])
  57. def run_task(self):
  58. # 获取验证码
  59. self.get_captcha()
  60. # 登录
  61. self.login()
  62. # 充值模板
  63. self.get_topup_template_mapping()
  64. # 创建链接
  65. self.create_link()
  66. def get_topup_template_mapping(self):
  67. url = "https://ms.zhangwenpindu.cn/manage/distribution/topUpTemplateMapping"
  68. params = {
  69. "pageNum": "1",
  70. "pageSize": "30",
  71. "bookId": self.bookId
  72. }
  73. self.headers['Authorization'] = f'Bearer {self.data.get("token")}'
  74. res = requests.get(url, headers=self.headers, params=params).json()
  75. mbs = res.get('data')
  76. print(f'充值模板:{mbs}')
  77. def create_link(self):
  78. url = "https://ms.zhangwenpindu.cn/manage/distribution/createLink"
  79. data = {
  80. "name": self.name,
  81. "bookId": self.bookId,
  82. "passBackTemplateId": "5",
  83. "adPassbackTemplateId": "",
  84. "topUpTemplateId": self.topUpTemplateId,
  85. "mediaChannel": "1",
  86. "group": "35",
  87. "paidPoint": "",
  88. "site": "5"
  89. }
  90. res = requests.post(url, headers=self.headers, data=data).json()
  91. # print(res)
  92. print(f'{"=" * 40}\n'
  93. f'原始ID:{res.get("data").get("originalId")}\n'
  94. f'小程序ID:{res.get("data").get("appId")}\n'
  95. f'小程序启动页面:{res.get("data").get("launchPath")}\n'
  96. f'小程序启动参数:{res.get("data").get("launchParam")}\n'
  97. f'跳转路径参数:{res.get("data").get("fullPath")}\n'
  98. f'微信站外链接:{res.get("data").get("wxMountLink")}\n'
  99. f'{"=" * 40}\n')
  100. def get_captcha(self):
  101. url = "https://ms.zhangwenpindu.cn/captchaImage"
  102. res = self.session.get(url, headers=self.headers).json()
  103. img_path = fr'{self.folder}\qr.gif'
  104. base64_2_img(img_path, 'data:image/gif;base64,' + res.get('img'))
  105. ocr = ddddocr.DdddOcr(show_ad=False)
  106. with open(img_path, 'rb') as f:
  107. img_bytes = f.read()
  108. result = ocr.classification(img_bytes)
  109. f.close()
  110. self.data['code'] = result
  111. self.data['uuid'] = res.get('uuid')
  112. def login(self):
  113. url = "https://ms.zhangwenpindu.cn/login"
  114. headers = {
  115. "Accept": "application/json, text/plain, */*",
  116. "Accept-Language": "zh-CN,zh;q=0.9",
  117. "Cache-Control": "no-cache",
  118. "Connection": "keep-alive",
  119. "Content-Type": "application/json;charset=UTF-8",
  120. "Origin": "https://manage.zhangwenpindu.cn",
  121. "Pragma": "no-cache",
  122. "Referer": "https://manage.zhangwenpindu.cn/",
  123. "Sec-Fetch-Dest": "empty",
  124. "Sec-Fetch-Mode": "cors",
  125. "Sec-Fetch-Site": "same-site",
  126. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
  127. "sec-ch-ua": "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
  128. "sec-ch-ua-mobile": "?0",
  129. "sec-ch-ua-platform": "\"Windows\""
  130. }
  131. data = {
  132. 'username': self.user_name,
  133. 'password': self.password,
  134. 'code': self.data['code'],
  135. 'uuid': self.data['uuid']
  136. }
  137. data = json.dumps(data, separators=(',', ':'))
  138. res = self.session.post(url, headers=headers, data=data).json()
  139. if res.get('code') == 200:
  140. print('登录成功')
  141. self.data['token'] = res.get('token')
  142. else:
  143. print('登录失败')
  144. if __name__ == '__main__':
  145. spider = DzSpider()
  146. # 用户名、密码
  147. spider.user_name = '康帅'
  148. spider.password = 'Ks25666'
  149. # 推广名称
  150. spider.name = '测试的'
  151. # 书的ID
  152. spider.bookId = '1854909287941992450'
  153. # 充值模板对应的key值
  154. spider.topUpTemplateId = '1854833762266247169'
  155. spider.run_task()