sql_help.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import os
  2. import sys
  3. from typing import Optional
  4. from common.mysql_db_aigc import AigcMysqlHelper
  5. sys.path.append(os.getcwd())
  6. from datetime import datetime
  7. from common.mysql_db import MysqlHelper
  8. class sqlCollect():
  9. """
  10. 视频信息写入库中
  11. """
  12. @classmethod
  13. def insert_task(cls, task_mark, video_id, mark, channel):
  14. current_time = datetime.now()
  15. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  16. insert_sql = f"""INSERT INTO pj_video_data (task_name, used_video_id, mark_name, data_time, channel) values ('{task_mark}' ,'{video_id}','{mark}', '{formatted_time}', '{channel}')"""
  17. MysqlHelper.update_values(
  18. sql=insert_sql
  19. )
  20. """
  21. 判断该任务id是否用过
  22. """
  23. @classmethod
  24. def is_used(cls, task_mark, video_id, mark_name, channel):
  25. sql = """
  26. SELECT used_video_id
  27. FROM pj_video_data
  28. WHERE used_video_id = %s AND task_name = %s AND mark_name = %s AND channel = %s
  29. ORDER BY data_time DESC
  30. LIMIT 1
  31. """
  32. data = MysqlHelper.get_values(sql, (str(video_id), task_mark, mark_name, channel))
  33. if len(data) == 0 or data == ():
  34. return False
  35. return True
  36. @classmethod
  37. def get_history_id(cls, channel, url):
  38. """
  39. 从数据库表中读取 id
  40. """
  41. sql = f"""select name_id from accounts where name = %s and platform = %s and useful = 1 limit 1"""
  42. data = MysqlHelper.get_values(sql, (url, channel))
  43. if data:
  44. return data[0][0]
  45. else:
  46. return False
  47. @classmethod
  48. def insert_history_id(cls, account_name, target, channel):
  49. insert_sql = f"""INSERT INTO accounts (name, name_id, platform, useful) values ("{account_name}", "{target}", "{channel}", 1 )"""
  50. MysqlHelper.update_values(
  51. sql=insert_sql
  52. )
  53. @classmethod
  54. def insert_machine_making_data(cls, name: str, task_mark: str, channel_id: str, url: str, v_id: str, piaoquan_id: str, new_title: str, code: str, formatted_time, old_title: str, oss_object_key: str):
  55. insert_sql = f"""INSERT INTO machine_making_data (name, task_mark, channel, user, v_id, pq_uid, title, pq_vid, data_time, old_title, oss_object_key) values ("{name}", "{task_mark}", "{channel_id}", "{url}", "{v_id}" , "{piaoquan_id}", "{new_title}", "{code}", "{formatted_time}", "{old_title}", "{oss_object_key}")"""
  56. MysqlHelper.update_values(
  57. sql=insert_sql
  58. )
  59. """
  60. 判断该任务id是否用过
  61. """
  62. @classmethod
  63. def ks_is_used(cls, photo_id):
  64. sql = """
  65. SELECT photo_id
  66. FROM ks_category_video
  67. WHERE photo_id = %s
  68. """
  69. data = MysqlHelper.get_values(sql, (str(photo_id)))
  70. if len(data) == 0 or data == ():
  71. return False
  72. return True
  73. """查询视频号user是否添加过"""
  74. @classmethod
  75. def sph_channel_user(cls, user_id):
  76. sql = """
  77. SELECT user_id
  78. FROM sph_channel_user
  79. WHERE user_id = %s
  80. """
  81. data = MysqlHelper.get_values(sql, (str(user_id)))
  82. if len(data) == 0 or data == ():
  83. return True
  84. return False
  85. """抓取视频号数据入库"""
  86. @classmethod
  87. def sph_data_info(cls, channel: str, objectId: str, video_url: str, cover: str, video_title: str, share_cnt: str, like_cnt: str, oss_video_key: str, oss_cover_key: str, nick_name: str, user_name: str, comment_count: str, fav_count: str, create_time: str, duration: str):
  88. insert_sql = f"""INSERT INTO sph_data_info (channel, video_id, video_url, video_cover, video_title, share_cnt, like_cnt, oss_url, oss_cover, nick_name, user_name, comment_count, fav_count, create_time, duration)
  89. values
  90. ("{channel}", "{objectId}", "{video_url}", "{cover}", "{video_title}", "{share_cnt}", "{like_cnt}", "{oss_video_key}", "{oss_cover_key}", "{nick_name}", "{user_name}", "{comment_count}", "{fav_count}", "{create_time}", "{duration}")"""
  91. res = MysqlHelper.update_values(
  92. sql=insert_sql
  93. )
  94. return res
  95. """查询是否有视频号是否插入过数据库"""
  96. @classmethod
  97. def sph_data_info_v_id(cls, video_id, channel):
  98. sql = """
  99. SELECT video_id
  100. FROM sph_data_info
  101. WHERE video_id = %s and channel = %s
  102. """
  103. data = MysqlHelper.get_values(sql, (str(video_id), channel))
  104. if data:
  105. return True
  106. return False
  107. @classmethod
  108. def sph_data_info_count(cls, user, channel):
  109. sql = """SELECT count(*) FROM sph_data_info WHERE nick_name = %s and channel = %s """
  110. data = MysqlHelper.get_values(sql, (user, channel))
  111. return data[0]
  112. """获取视频号历史数据"""
  113. @classmethod
  114. def sph_data_info_list(cls, user):
  115. sql = """SELECT video_id, video_title, share_cnt, like_cnt, oss_url, oss_cover, duration FROM sph_data_info WHERE nick_name = %s ORDER BY share_cnt DESC"""
  116. data = MysqlHelper.get_values(sql, (user))
  117. if data:
  118. return data
  119. return None
  120. """查询是否有视频号数据"""
  121. @classmethod
  122. def sph_channel_user_list(cls):
  123. sql = """
  124. SELECT user_id
  125. FROM sph_channel_user
  126. WHERE status = %s
  127. """
  128. data = MysqlHelper.get_values(sql, (0))
  129. if len(data) == 0 or data == ():
  130. return None
  131. return data
  132. @classmethod
  133. def update_sph_channel_user_status(cls, user):
  134. """
  135. 视频号账号状态改为1 代表已经抓取过
  136. """
  137. sql = f"""UPDATE sph_channel_user set status = 1 where user_id = '{user}'"""
  138. res = MysqlHelper.update_values(
  139. sql=sql
  140. )
  141. return res
  142. """插入视频号信息"""
  143. @classmethod
  144. def insert_sph_channel_user(cls, channel, user_id):
  145. insert_sql = f"""INSERT INTO sph_channel_user (channel, user_id, status) values ("{channel}", "{user_id}", 0)"""
  146. res = MysqlHelper.update_values(
  147. sql=insert_sql
  148. )
  149. @classmethod
  150. def insert_ks_data(cls, user_name: str, user_sex: str, time_data, caption: str, view_count: str, like_count: str,
  151. share_count: str, duration: str, main_mv_url: str, thumbnail_url: str, user_id: str, status: str,
  152. photo_id: str, category_name: str, age: str, oss_object: Optional[str] = None,
  153. video_uid: Optional[str] = None):
  154. current_time = datetime.now()
  155. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  156. insert_sql = f"""INSERT INTO ks_category_video (user_name, user_sex, time_data, caption, view_count, like_count, share_count, duration, main_mv_url, thumbnail_url, user_id, status, age_proportion, video_oss_path, pq_video_id, update_time, photo_id, category_name) values ("{user_name}", "{user_sex}", "{time_data}", "{caption}", "{view_count}", "{like_count}", "{share_count}", "{duration}", "{main_mv_url}", "{thumbnail_url}", "{user_id}", "{status}", "{age}", "{oss_object}", "{video_uid}", "{formatted_time}", "{photo_id}", "{category_name}")"""
  157. res = MysqlHelper.update_values(
  158. sql=insert_sql
  159. )
  160. @classmethod
  161. def get_shp_dd_data(cls, url):
  162. """
  163. 获取视频号单点内容
  164. """
  165. sql = f"""select video_id,title,author_id,author_name,cover_url,video_url,video_duration,from_user_id,from_user_name,from_group_id,from_group_name,source,wx_msg, is_encrypted, decode_key from dandian_content where from_user_name = %s and has_used = 0 ORDER BY create_timestamp DESC limit 2"""
  166. data = AigcMysqlHelper.get_values(sql, (url,))
  167. return data
  168. @classmethod
  169. def get_feed_data(cls, channel):
  170. """
  171. 获取feed流视频
  172. """
  173. sql = f"""select video_id,channel,video_url,cover_url,title from automator_feed_video where channel = %s and has_used = 0 and if_50_like = 1 limit 1"""
  174. data = AigcMysqlHelper.get_values(sql, (channel,))
  175. return data
  176. @classmethod
  177. def update_shp_dd_vid(cls, vid):
  178. """
  179. 视频号单点修改状态为1
  180. """
  181. sql = f"""UPDATE dandian_content set has_used = 1 where video_id = '{vid}'"""
  182. res = AigcMysqlHelper.update_values(
  183. sql=sql
  184. )
  185. return res
  186. @classmethod
  187. def update_shp_dd_vid_4(cls, vid):
  188. """
  189. 视频号单点修改状态为4
  190. """
  191. sql = f"""UPDATE dandian_content set has_used = 4 where video_id = '{vid}'"""
  192. res = AigcMysqlHelper.update_values(
  193. sql=sql
  194. )
  195. return res
  196. @classmethod
  197. def select_ks_star_data(cls, profile_id):
  198. sql = """SELECT * FROM ks_star_info where profile_id = %s"""
  199. data = MysqlHelper.get_values(sql, (profile_id))
  200. if data:
  201. return data
  202. return None
  203. @classmethod
  204. def insert_ks_star_data(cls, user_id: str, star_id: str, name, kwai_id: str, gender: str, fans_number: str,
  205. profile_id: str, star_tag_str: str, industry_tag_str: str, photo_expect_play: str, photo_expect_cpm: str, photo_interaction_rate: str,
  206. photo_complete_play_rate: str, fans_increase_num: str, fans_increase_rate: str,):
  207. insert_sql = f"""INSERT INTO ks_star_info (user_id, star_id, name, kwai_id, gender, fans_number, profile_id, star_tag_str, industry_tag_str, photo_expect_play, photo_expect_cpm, photo_interaction_rate, photo_complete_play_rate, fans_increase_num, fans_increase_rate) values ("{user_id}", "{star_id}", "{name}", "{kwai_id}", "{gender}", "{fans_number}", "{profile_id}", "{star_tag_str}", "{industry_tag_str}", "{photo_expect_play}", "{photo_expect_cpm}", "{photo_interaction_rate}", "{photo_complete_play_rate}", "{fans_increase_num}", "{fans_increase_rate}")"""
  208. res = MysqlHelper.update_values(
  209. sql=insert_sql
  210. )
  211. return res
  212. @classmethod
  213. def insert_ks_star_works_info(cls, star_id: str, profile_id: str, pplay_median_data_90: str, pplay_median_data_30: str, pphoto_cnt_data_90: str, pphoto_cnt_data_30: str, pavg_video_duration_data_90: str, pavg_video_duration_data_30: str, star_work_report_90: str, star_work_report_30: str, pavg_share_cnt_90: str, pavg_share_cnt_30: str):
  214. insert_sql = f"""INSERT INTO ks_star_works_info
  215. (star_id, profile_id, pplay_median_data_90, pplay_median_data_30, pphoto_cnt_data_90, pphoto_cnt_data_30, pavg_video_duration_data_90, pavg_video_duration_data_30, star_work_report_90, star_work_report_30, pavg_share_cnt_90, pavg_share_cnt_30)
  216. values
  217. ("{star_id}", "{profile_id}", "{pplay_median_data_90}", "{pplay_median_data_30}", "{pphoto_cnt_data_90}", "{pphoto_cnt_data_30}", "{pavg_video_duration_data_90}", "{pavg_video_duration_data_30}", "{star_work_report_90}", "{star_work_report_30}", "{pavg_share_cnt_90}", "{pavg_share_cnt_30}")"""
  218. res = MysqlHelper.update_values(
  219. sql=insert_sql
  220. )
  221. return res
  222. @classmethod
  223. def insert_ks_list_portrait_info(cls, star_id: str, profile_id: str, type: str,
  224. name: str, proportion: str):
  225. insert_sql = f"""INSERT INTO ks_list_portrait_info
  226. (star_id, profile_id, type, name, proportion)
  227. values
  228. ("{star_id}", "{profile_id}", "{type}", "{name}", "{proportion}")"""
  229. res = MysqlHelper.update_values(
  230. sql=insert_sql
  231. )
  232. return res
  233. """品类视频新条数记录"""
  234. @classmethod
  235. def insert_spider_supply_targetcnt(cls, channel_id: str, name: str, url: str,
  236. number: str, new_count: str, s_cnt: str):
  237. insert_sql = f"""INSERT INTO spider_supply_targetcnt
  238. (channel_id, name, url, o_cnt, n_cnt, s_cnt)
  239. values
  240. ("{channel_id}", "{name}", "{url}", "{number}", "{new_count}", "{s_cnt}")"""
  241. res = MysqlHelper.update_values(
  242. sql=insert_sql
  243. )
  244. return res
  245. """
  246. 获取相似溯源基础账号
  247. """
  248. @classmethod
  249. def get_machine_making_reflux(cls, channel, channel1, channel2, channel3, channel4):
  250. sql = """SELECT user FROM machine_making_reflux WHERE (channel = %s or channel = %s or channel = %s or channel = %s or channel = %s) and status = 0"""
  251. data = MysqlHelper.get_values(sql, (channel, channel1, channel2, channel3, channel4))
  252. if len(data) == 0 or data == ():
  253. return None
  254. return data
  255. """
  256. 修改相似溯源基础账号状态
  257. """
  258. @classmethod
  259. def update_machine_making_reflux(cls, user_id):
  260. sql = f"""UPDATE machine_making_reflux set status = 1 where user = '{user_id}'"""
  261. res = MysqlHelper.update_values(
  262. sql=sql
  263. )
  264. return res
  265. """插入符合规则的用户主页"""
  266. @classmethod
  267. def insert_user_data(cls, uid, name, account_id, channel, status):
  268. insert_sql = f"""INSERT INTO machine_making_traceability (old_account_id, name, new_account_id, channel, status) values ("{uid}", "{name}","{account_id}","{channel}", {status})"""
  269. res = MysqlHelper.update_values(
  270. sql=insert_sql
  271. )
  272. return res
  273. """相似溯源-视频号数据插入"""
  274. @classmethod
  275. def insert_xssy_sph_info(cls, account_user: str, traceable_user: str, channel: str, traceable_user_v2: str, has_used: str, appid:Optional[str] = None, pq_id:Optional[str] = None):
  276. insert_sql = f"""INSERT INTO xssy_sph (account_user, traceable_user, channel, traceable_user_v2, pq_id, has_used, appid) values ("{account_user}", "{traceable_user}","{channel}", "{traceable_user_v2}","{pq_id}", {has_used},"{appid}")"""
  277. res = MysqlHelper.update_values(
  278. sql=insert_sql
  279. )
  280. return res
  281. """查询该账号是否存在"""
  282. @classmethod
  283. def select_crawler_uesr_v3(cls, link: str):
  284. sql = """SELECT uid FROM crawler_user_v3 WHERE link = %s and source = 'jiqizidonggaizao'"""
  285. data = MysqlHelper.get_values(sql, (link))
  286. if data:
  287. return data
  288. return None