feishu_form.py 19 KB

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