feishu_form.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import os
  4. import random
  5. import sys
  6. import datetime
  7. import itertools
  8. from collections import defaultdict
  9. from common.sql_help import sqlCollect
  10. sys.path.append(os.getcwd())
  11. from common.feishu_utils import Feishu
  12. class Material():
  13. @classmethod
  14. def get_count_restrict(cls, channel):
  15. count_channel = Feishu.get_values_batch("KsoMsyP2ghleM9tzBfmcEEXBnXg", "187FZ7")
  16. for row in count_channel[1:]:
  17. sheet_channel = row[0]
  18. if sheet_channel == channel:
  19. return row[1]
  20. """
  21. 获取汇总表所有负责人列表
  22. """
  23. @classmethod
  24. def feishu_list(cls):
  25. summary = Feishu.get_values_batch("summary", "bc154d")
  26. list = []
  27. for row in summary[1:]:
  28. mark = row[0]
  29. name = row[1]
  30. feishu_id = row[3]
  31. feishu_sheet = row[4]
  32. cookie_sheet = row[5]
  33. number = {"mark": mark, "name": name, "feishu_id": feishu_id, "feishu_sheet": feishu_sheet, "cookie_sheet": cookie_sheet}
  34. if mark:
  35. list.append(number)
  36. else:
  37. return list
  38. return list
  39. @classmethod
  40. def get_sph_user(cls):
  41. data = Feishu.get_values_batch("GPbhsb5vchAN3qtzot6cu1f0n1c", "cc7ef0")
  42. user_data_list = []
  43. try:
  44. for row in data[1:]:
  45. users = str(row[2])
  46. if users and users != 'None':
  47. if ',' in users:
  48. user_list = users.split(',')
  49. else:
  50. user_list = [users]
  51. for user in user_list:
  52. status = sqlCollect.sph_channel_user(user)
  53. if status:
  54. user_data_list.append(user)
  55. else:
  56. return user_data_list
  57. return user_data_list
  58. except:
  59. return user_data_list
  60. """
  61. list 重新排序
  62. """
  63. @classmethod
  64. def sort_keyword_data(cls, data):
  65. # 解析 JSON 数据
  66. data = [json.loads(item) for item in data]
  67. # 根据 keyword_name 进行分组
  68. groups = defaultdict(list)
  69. for item in data:
  70. groups[item['keyword_name']].append(item)
  71. # 获取所有唯一的 keyword_name
  72. priority_names = list(groups.keys())
  73. # 对每个分组内的数据按 first_category 进行随机打乱
  74. for name in priority_names:
  75. random.shuffle(groups[name]) # 打乱每个分组内的数据顺序
  76. # 轮流排序每个分组的数据,保持 keyword_name 的顺序
  77. result = []
  78. max_length = max(len(groups[name]) for name in priority_names)
  79. for i in range(max_length):
  80. for name in priority_names:
  81. if i < len(groups[name]):
  82. result.append(groups[name][i])
  83. # 将结果转回 JSON 字符串列表
  84. sorted_list = [json.dumps(item, ensure_ascii=False) for item in result]
  85. return sorted_list
  86. """
  87. 获取搜索任务
  88. """
  89. @classmethod
  90. def get_keyword_data(cls, feishu_id, feishu_sheet):
  91. data = Feishu.get_values_batch(feishu_id, feishu_sheet)
  92. processed_list = []
  93. try:
  94. for row in data[1:]:
  95. channel_id = row[1]
  96. channel_url = str(row[2])
  97. tags = row[3]
  98. piaoquan_id = row[4]
  99. number = row[5]
  100. limit_number = row[6]
  101. video_share = row[7]
  102. video_ending = row[8]
  103. voice = row[9]
  104. crop_tool = row[10]
  105. gg_duration = row[11]
  106. title = row[12]
  107. if channel_url == None or channel_url == "" or len(channel_url) == 0:
  108. continue
  109. first_category = row[14] # 一级品类
  110. secondary_category = row[15] # 二级品类
  111. def count_items(item, separator):
  112. if item and item not in {'None', ''}:
  113. return len(item.split(separator))
  114. return 0
  115. video_id_total = count_items(str(channel_url), ',')
  116. title_total = count_items(str(title), '/')
  117. video_ending_total = count_items(str(video_ending), ',')
  118. values = [channel_id, video_id_total, piaoquan_id, video_share, video_ending_total, crop_tool, gg_duration, title_total, first_category]
  119. filtered_values = [str(value) for value in values if value is not None and value != "None"]
  120. task_mark = "_".join(map(str, filtered_values))
  121. keyword_sort = row[16] # 排序条件
  122. keyword_time = row[17] # 发布时间
  123. keyword_duration = row[18] # 视频时长
  124. keyword_name = row[19] # 负责人
  125. keyword_sort_list = keyword_sort.split(',')
  126. keyword_duration_list = keyword_duration.split(',')
  127. keyword_time_list = keyword_time.split(',')
  128. combinations = list(itertools.product(keyword_sort_list, keyword_time_list, keyword_duration_list))
  129. if ',' in channel_url:
  130. channel_url = channel_url.split(',')
  131. else:
  132. channel_url = [channel_url]
  133. for user in channel_url:
  134. for combo in combinations:
  135. number_dict = {
  136. "task_mark": task_mark,
  137. "channel_id": channel_id,
  138. "channel_url": user,
  139. "piaoquan_id": piaoquan_id,
  140. "number": number,
  141. "title": title,
  142. "video_share": video_share,
  143. "video_ending": video_ending,
  144. "crop_total": crop_tool,
  145. "gg_duration_total": gg_duration,
  146. "voice": voice,
  147. "first_category": first_category, # 一级品类
  148. "secondary_category": secondary_category, # 二级品类
  149. "combo": combo, # 搜索条件
  150. "keyword_name": keyword_name, # 品类负责人
  151. "tags": tags,
  152. "limit_number":limit_number
  153. }
  154. processed_list.append(json.dumps(number_dict, ensure_ascii=False))
  155. except:
  156. processed_list = cls.sort_keyword_data(processed_list)
  157. return processed_list
  158. processed_list = cls.sort_keyword_data(processed_list)
  159. return processed_list
  160. """
  161. 获取品类对应负责人任务明细
  162. """
  163. @classmethod
  164. def get_pl_task_data(cls, feishu_id, feishu_sheet):
  165. data = Feishu.get_values_batch( feishu_id, feishu_sheet )
  166. processed_list = []
  167. try:
  168. for row in data[1:]:
  169. channel_id = row[1]
  170. channel_url = str( row[2] )
  171. tags = row[3]
  172. piaoquan_id = row[4]
  173. number = row[5]
  174. limit_number = row[6]
  175. video_share = row[7]
  176. video_ending = row[8]
  177. voice = row[9]
  178. crop_tool = row[10]
  179. gg_duration = row[11]
  180. title = row[12]
  181. if channel_url == None or channel_url == "" or len( channel_url ) == 0:
  182. continue
  183. try:
  184. ls_number = int( row[13] )
  185. except:
  186. ls_number = None
  187. first_category = row[14]
  188. name = row[15]
  189. def count_items(item, separator):
  190. if item and item not in {'None', ''}:
  191. return len( item.split( separator ) )
  192. return 0
  193. video_id_total = count_items( str( channel_url ), ',' )
  194. title_total = count_items( str( title ), '/' )
  195. video_ending_total = count_items( str( video_ending ), ',' )
  196. values = [channel_id, video_id_total, piaoquan_id, video_share, video_ending_total, crop_tool,
  197. gg_duration, title_total]
  198. filtered_values = [str( value ) for value in values if value is not None and value != "None"]
  199. task_mark = "_".join( map( str, filtered_values ) )
  200. if piaoquan_id and piaoquan_id not in {'None', ''}:
  201. if ',' in channel_url:
  202. channel_url = channel_url.split( ',' )
  203. else:
  204. channel_url = [channel_url]
  205. for user in channel_url:
  206. number_dict = {
  207. "task_mark": task_mark,
  208. "channel_id": channel_id,
  209. "channel_url": user,
  210. "piaoquan_id": piaoquan_id,
  211. "number": number,
  212. "title": title,
  213. "video_share": video_share,
  214. "video_ending": video_ending,
  215. "crop_total": crop_tool,
  216. "gg_duration_total": gg_duration,
  217. "voice": voice,
  218. "first_category": first_category, # 一级品类
  219. "keyword_name":name,
  220. "tags": tags,
  221. "limit_number":limit_number
  222. }
  223. processed_list.append( json.dumps( number_dict, ensure_ascii=False ) )
  224. if channel_id == "抖音" or channel_id == "快手" or channel_id == "视频号":
  225. if ls_number and ls_number not in {'None', ''}:
  226. if channel_id == "抖音":
  227. new_channel_id = "抖音历史"
  228. if channel_id == "快手":
  229. new_channel_id = "快手历史"
  230. if channel_id == "视频号":
  231. new_channel_id = "视频号历史"
  232. # values1 = [new_channel_id, video_id_total, piaoquan_id, video_share, video_ending_total,
  233. # crop_tool,
  234. # gg_duration, title_total]
  235. # filtered_values1 = [str( value ) for value in values1 if
  236. # value is not None and value != "None"]
  237. # task_mark1 = "_".join( map( str, filtered_values1 ) )
  238. number_dict = {
  239. "task_mark": task_mark,
  240. "channel_id": new_channel_id,
  241. "channel_url": user,
  242. "piaoquan_id": piaoquan_id,
  243. "number": ls_number,
  244. "title": title,
  245. "video_share": video_share,
  246. "video_ending": video_ending,
  247. "crop_total": crop_tool,
  248. "gg_duration_total": gg_duration,
  249. "voice": voice,
  250. "first_category": first_category, # 一级品类
  251. "keyword_name": name,
  252. "tags": tags,
  253. "limit_number":limit_number
  254. }
  255. processed_list.append( json.dumps( number_dict, ensure_ascii=False ) )
  256. else:
  257. processed_list = cls.sort_keyword_data(processed_list)
  258. return processed_list
  259. processed_list = cls.sort_keyword_data(processed_list)
  260. return processed_list
  261. except:
  262. processed_list = cls.sort_keyword_data(processed_list)
  263. return processed_list
  264. """
  265. 获取对应负责人任务明细
  266. """
  267. @classmethod
  268. def get_task_data(cls, feishu_id, feishu_sheet):
  269. data = Feishu.get_values_batch(feishu_id, feishu_sheet)
  270. processed_list = []
  271. try:
  272. for row in data[1:]:
  273. channel_id = row[1]
  274. channel_url = str(row[2])
  275. tags = row[3]
  276. piaoquan_id = row[4]
  277. number = row[5]
  278. limit_number = row[6]
  279. video_share = row[7]
  280. video_ending = row[8]
  281. voice = row[9]
  282. crop_tool = row[10]
  283. gg_duration = row[11]
  284. title = row[12]
  285. if channel_url == None or channel_url == "" or len(channel_url) == 0:
  286. continue
  287. try:
  288. ls_number = int(row[13])
  289. except:
  290. ls_number = None
  291. def count_items(item, separator):
  292. if item and item not in {'None', ''}:
  293. return len(item.split(separator))
  294. return 0
  295. video_id_total = count_items(str(channel_url), ',')
  296. title_total = count_items(str(title), '/')
  297. video_ending_total = count_items(str(video_ending), ',')
  298. values = [channel_id, video_id_total, piaoquan_id, video_share, video_ending_total, crop_tool, gg_duration, title_total]
  299. filtered_values = [str(value) for value in values if value is not None and value != "None"]
  300. task_mark = "_".join(map(str, filtered_values))
  301. if piaoquan_id and piaoquan_id not in {'None', ''}:
  302. if ',' in channel_url:
  303. channel_url = channel_url.split(',')
  304. else:
  305. channel_url = [channel_url]
  306. for user in channel_url:
  307. number_dict = {
  308. "task_mark": task_mark,
  309. "channel_id": channel_id,
  310. "channel_url": user,
  311. "piaoquan_id": piaoquan_id,
  312. "number": number,
  313. "title": title,
  314. "video_share": video_share,
  315. "video_ending": video_ending,
  316. "crop_total": crop_tool,
  317. "gg_duration_total": gg_duration,
  318. "voice": voice,
  319. "tags":tags,
  320. "limit_number":limit_number
  321. }
  322. processed_list.append(json.dumps(number_dict, ensure_ascii=False))
  323. if channel_id == "抖音" or channel_id == "快手" or channel_id == "视频号":
  324. if ls_number and ls_number not in {'None', ''}:
  325. if channel_id == "抖音":
  326. new_channel_id = "抖音历史"
  327. if channel_id == "快手":
  328. new_channel_id = "快手历史"
  329. if channel_id == "视频号":
  330. new_channel_id = "视频号历史"
  331. # values1 = [new_channel_id, video_id_total, piaoquan_id, video_share, video_ending_total, crop_tool,
  332. # gg_duration, title_total]
  333. # filtered_values1 = [str(value) for value in values1 if value is not None and value != "None"]
  334. # task_mark1 = "_".join(map(str, filtered_values1))
  335. number_dict = {
  336. "task_mark": task_mark,
  337. "channel_id": new_channel_id,
  338. "channel_url": user,
  339. "piaoquan_id": piaoquan_id,
  340. "number": ls_number,
  341. "title": title,
  342. "video_share": video_share,
  343. "video_ending": video_ending,
  344. "crop_total": crop_tool,
  345. "gg_duration_total": gg_duration,
  346. "voice": voice,
  347. "tags": tags,
  348. "limit_number":limit_number
  349. }
  350. processed_list.append(json.dumps(number_dict, ensure_ascii=False))
  351. else:
  352. return processed_list
  353. return processed_list
  354. except:
  355. return processed_list
  356. """
  357. 获取对应片尾+srt
  358. """
  359. @classmethod
  360. def get_pwsrt_data(cls, feishu_id, feishu_sheet, video_ending):
  361. data = Feishu.get_values_batch(feishu_id, feishu_sheet)
  362. for row in data[1:]:
  363. pw_mark = row[0]
  364. pw_id = row[1]
  365. pw_srt = row[2]
  366. if pw_id != 'None' and pw_id != '' and pw_id != None:
  367. if pw_mark == video_ending:
  368. number = {"pw_id": pw_id, "pw_srt": pw_srt}
  369. return number
  370. return ''
  371. """
  372. 获取对应固定字幕
  373. """
  374. @classmethod
  375. def get_pzsrt_data(cls, feishu_id, feishu_sheet, video_share_name):
  376. data = Feishu.get_values_batch(feishu_id, feishu_sheet)
  377. for row in data[1:]:
  378. pz_mark = row[0]
  379. pz_zm = row[1]
  380. if pz_zm != 'None' and pz_zm != '' and pz_zm != None:
  381. if pz_mark == video_share_name:
  382. return pz_zm
  383. return ''
  384. """
  385. 获取 cookie 信息
  386. """
  387. @classmethod
  388. def get_cookie_data(cls, feishu_id, cookie_sheet, channel):
  389. data = Feishu.get_values_batch(feishu_id, cookie_sheet)
  390. for row in data[1:]:
  391. channel_mask = row[0]
  392. cookie = row[1]
  393. if channel_mask == channel:
  394. return cookie