common.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # encoding: utf-8
  2. """
  3. @author: luojunhui
  4. """
  5. import time
  6. import json
  7. import uuid
  8. import random
  9. import hashlib
  10. import requests
  11. import aiohttp
  12. import asyncio
  13. import urllib.parse
  14. def create_gzh_path(video_id, shared_uid):
  15. """
  16. :param video_id: 视频 id
  17. :param shared_uid: 分享 id
  18. """
  19. def auto_white(root_share_id_):
  20. """
  21. 自动加入白名单, 保证公众号百分百出广告
  22. :param root_share_id_:
  23. :return:
  24. """
  25. def get_cookie():
  26. """
  27. 获取 cookie
  28. :return:
  29. """
  30. url = "https://admin.piaoquantv.com/manager/login?account=luojunhui&passWd=e10adc3949ba59abbe56e057f20f883e&muid=7"
  31. payload = {}
  32. headers = {
  33. 'accept': 'application/json, text/plain, */*',
  34. 'accept-language': 'en',
  35. 'priority': 'u=1, i',
  36. 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
  37. 'sec-ch-ua-mobile': '?0',
  38. 'sec-ch-ua-platform': '"macOS"',
  39. 'sec-fetch-dest': 'empty',
  40. 'sec-fetch-mode': 'cors',
  41. 'sec-fetch-site': 'same-origin',
  42. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
  43. }
  44. response = requests.request("GET", url, headers=headers, data=payload)
  45. return response.cookies.values()[0]
  46. url = "https://admin.piaoquantv.com/manager/ad/own/white/rootShare/save"
  47. dd = {
  48. "rootShareId": root_share_id_,
  49. "commit": "算法自动加入白名单--"
  50. }
  51. payload = json.dumps(dd)
  52. cookie = get_cookie()
  53. headers = {
  54. 'accept': 'application/json',
  55. 'accept-language': 'en',
  56. 'content-type': 'application/json;',
  57. 'cookie': "SESSION=" + cookie,
  58. 'origin': 'https://admin.piaoquantv.com',
  59. 'priority': 'u=1, i',
  60. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
  61. }
  62. response = requests.request("POST", url, headers=headers, data=payload)
  63. return response.json()['content']
  64. def generate_source_id():
  65. """
  66. generate_source_id
  67. :return:
  68. """
  69. timestamp = str(int(time.time() * 1000))
  70. random_str = str(random.randint(1000, 9999))
  71. hash_input = f"{timestamp}-{random_str}"
  72. return hashlib.md5(hash_input.encode()).hexdigest()
  73. root_share_id = str(uuid.uuid4())
  74. source_id = "longArticles_" + generate_source_id()
  75. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
  76. # 自动把 root_share_id 加入到白名单
  77. # auto_white(root_share_id)
  78. return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  79. def create_gzh_path_v2(video_id, shared_uid):
  80. """
  81. :param video_id: 视频 id
  82. :param shared_uid: 分享 id
  83. """
  84. def generate_source_id():
  85. """
  86. generate_source_id
  87. :return:
  88. """
  89. timestamp = str(int(time.time() * 1000))
  90. random_str = str(random.randint(1000, 9999))
  91. hash_input = f"{timestamp}-{random_str}"
  92. return hashlib.md5(hash_input.encode()).hexdigest()
  93. root_share_id = str(uuid.uuid4())
  94. source_id = "touliu_tencentGzhArticle_" + generate_source_id()
  95. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
  96. # 自动把 root_share_id 加入到白名单
  97. # auto_white(root_share_id)
  98. return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  99. def request_for_info(video_id):
  100. """
  101. 请求数据
  102. :param video_id:
  103. :return:
  104. """
  105. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  106. data = {
  107. "videoIdList": [video_id]
  108. }
  109. header = {
  110. "Content-Type": "application/json",
  111. }
  112. response = requests.post(url, headers=header, data=json.dumps(data))
  113. return response.json()
  114. def clean_title(strings):
  115. """
  116. :param strings:
  117. :return:
  118. """
  119. return (
  120. strings.strip()
  121. .replace("\n", "")
  122. .replace("/", "")
  123. .replace("\r", "")
  124. .replace("#", "")
  125. .replace(".", "。")
  126. .replace("\\", "")
  127. .replace("&NBSP", "")
  128. .replace(":", "")
  129. .replace("*", "")
  130. .replace("?", "")
  131. .replace("?", "")
  132. .replace('"', "")
  133. .replace("<", "")
  134. .replace(">", "")
  135. .replace("|", "")
  136. .replace(" ", "")
  137. .replace('"', "")
  138. .replace("'", "")
  139. )
  140. def sensitive_flag(s_words, ori_title):
  141. """
  142. :param s_words:
  143. :param ori_title:
  144. :return:
  145. """
  146. # for word in s_words:
  147. # if str(word) in ori_title:
  148. # return False
  149. return True
  150. def account_info_map(gh_id):
  151. """
  152. 根据账号 id 来判断返回哪个小程序账号
  153. :param gh_id:
  154. :return:
  155. """
  156. pqlh = {
  157. "name": "票圈乐活",
  158. "id": "wxe8f8f0e23cecad0f",
  159. "avatar": "https://rescdn.yishihui.com/0temp/lehuo.png"
  160. }
  161. xyfxhyjl = {
  162. "name": "幸运福星好运锦鲤",
  163. "id": "wx95dcbfc0753c06a8",
  164. "avatar": "https://rescdn.yishihui.com/0temp/xyfxhyjl.png"
  165. }
  166. pqzf = {
  167. "name": "票圈祝福",
  168. "id": "wxf7261ed54f2e450e",
  169. "avatar": "https://rescdn.yishihui.com/0temp/pqzf.png"
  170. }
  171. buy_accounts = [
  172. "gh_084a485e859a",
  173. "gh_e24da99dc899",
  174. "gh_e0eb490115f5",
  175. "gh_183d80deffb8",
  176. "gh_5ff48e9fb9ef",
  177. "gh_9f8dc5b0c74e",
  178. "gh_6d9f36e3a7be"
  179. ]
  180. dyy = [
  181. "gh_9877c8541764",
  182. "gh_6d205db62f04",
  183. "gh_c69776baf2cd",
  184. "gh_7e5818b2dd83",
  185. "gh_89ef4798d3ea",
  186. "gh_a2901d34f75b",
  187. "gh_b15de7c99912"
  188. ]
  189. if gh_id in buy_accounts:
  190. return ""
  191. elif gh_id in dyy:
  192. return ""
  193. else:
  194. return ""
  195. async def request_etl(url, headers, json_data, retries=6):
  196. """
  197. :param url:
  198. :param headers:
  199. :param json_data:
  200. :param retries:
  201. :return:
  202. """
  203. async with aiohttp.ClientSession() as session:
  204. for attempt in range(retries):
  205. try:
  206. async with session.post(url, headers=headers, json=json_data, timeout=120) as response:
  207. return await response.json()
  208. except asyncio.TimeoutError:
  209. if attempt < retries - 1:
  210. await asyncio.sleep(2) # 等待一段时间后重试
  211. else:
  212. raise
  213. async def async_post(url, headers, payload):
  214. """
  215. :param url:
  216. :param headers:
  217. :param payload:
  218. :return:
  219. """
  220. retries = 3
  221. async with aiohttp.ClientSession() as session:
  222. for attempt in range(3):
  223. try:
  224. async with session.post(url, headers=headers, data=payload, timeout=10) as response:
  225. return await response.json()
  226. except asyncio.TimeoutError:
  227. if attempt < retries - 1:
  228. await asyncio.sleep(2) # 等待一段时间后重试
  229. else:
  230. raise