sql_help.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import datetime
  2. import os
  3. import random
  4. import sys
  5. from datetime import timedelta
  6. sys.path.append(os.getcwd())
  7. from datetime import datetime
  8. from common import MysqlHelper
  9. from common import Common
  10. class sqlHelp():
  11. @classmethod
  12. def get_count_list(cls, name_list):
  13. count_list = []
  14. current_time = datetime.now()
  15. formatted_time = current_time.strftime("%Y-%m-%d")
  16. for name in name_list:
  17. count = f"""SELECT COUNT(*) AS agc_video_deposit FROM ( SELECT audio, account_id FROM agc_video_deposit WHERE time = '{formatted_time}' AND mark = '{name["mark"]}' GROUP BY audio, account_id) AS subquery;"""
  18. count = MysqlHelper.get_values(count, "prod")
  19. if count == None:
  20. count = 0
  21. count = str(count).replace('(', '').replace(')', '').replace(',', '')
  22. count_list.append(f"{name['mark_name']}生成条数为:{count}条 \n")
  23. return count_list
  24. """
  25. 获取未使用的视频链接
  26. """
  27. @classmethod
  28. def get_url_list(cls, user_list, mark, limit_count):
  29. for i in range(8):
  30. user = str(random.choice(user_list))
  31. user = user.replace('(', '').replace(')', '').replace(',', '')
  32. current_time = datetime.now()
  33. three_days_ago = current_time - timedelta(days=1)
  34. formatted_current_time = current_time.strftime("%Y-%m-%d")
  35. formatted_three_days_ago = three_days_ago.strftime("%Y-%m-%d")
  36. url_list = f"""SELECT a.video_id, a.account_id, a.oss_object_key
  37. FROM agc_video_url a
  38. LEFT JOIN agc_video_deposit b
  39. ON a.oss_object_key = b.oss_object_key
  40. AND b.time >= '{formatted_three_days_ago}'
  41. AND b.time <= '{formatted_current_time}'
  42. WHERE b.video_id IS NULL
  43. AND a.account_id = {user}
  44. AND a.status = 1
  45. AND a.mark = '{mark}'
  46. LIMIT {limit_count};"""
  47. url_list = MysqlHelper.get_values(url_list, "prod")
  48. if url_list:
  49. if len(url_list) >= 35:
  50. return url_list, user
  51. return None, None
  52. """
  53. 获取已入库的用户id
  54. """
  55. @classmethod
  56. def get_user_id(cls, channel_type, mark):
  57. account_id = f"""select account_id from agc_video_url where mark = '{mark}' and oss_object_key LIKE '%{channel_type}%' group by account_id ;"""
  58. account_id = MysqlHelper.get_values(account_id, "prod")
  59. return account_id
  60. """
  61. 获取已入库的用户id
  62. """
  63. @classmethod
  64. def get_link_count(cls, mark, platform):
  65. current_time = datetime.now()
  66. formatted_time = current_time.strftime("%Y-%m-%d")
  67. count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM agc_video_deposit WHERE time = '{formatted_time}' AND platform = '{platform}' and mark = '{mark}' GROUP BY audio, account_id) AS subquery;"""
  68. count = MysqlHelper.get_values(count, "prod")
  69. if count == None:
  70. count = 0
  71. count = str(count).replace('(', '').replace(')', '').replace(',', '')
  72. return int(count)
  73. """
  74. 获取跟随脚本已入库数量
  75. """
  76. @classmethod
  77. def get_link_gs_count(cls, mark):
  78. current_time = datetime.now()
  79. formatted_time = current_time.strftime("%Y-%m-%d")
  80. count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM agc_video_deposit WHERE time = '{formatted_time}' and mark LIKE '%{mark}%' GROUP BY audio, account_id) AS subquery;"""
  81. count = MysqlHelper.get_values(count, "prod")
  82. if count == None:
  83. count = 0
  84. count = str(count).replace('(', '').replace(')', '').replace(',', '')
  85. return int(count)
  86. """
  87. 获取跟随脚本站外已入库数量
  88. """
  89. @classmethod
  90. def get_link_zw_count(cls, mark, platform):
  91. current_time = datetime.now()
  92. formatted_time = current_time.strftime("%Y-%m-%d")
  93. count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM agc_video_deposit WHERE time = '{formatted_time}' and mark = '{mark}' GROUP BY audio, account_id) AS subquery;"""
  94. count = MysqlHelper.get_values(count, "prod")
  95. if count == None:
  96. count = 0
  97. count = str(count).replace('(', '').replace(')', '').replace(',', '')
  98. return int(count)
  99. """
  100. 获取跟随脚本站内已入库数量
  101. """
  102. @classmethod
  103. def get_link_zn_count(cls, mark, platform):
  104. current_time = datetime.now()
  105. formatted_time = current_time.strftime("%Y-%m-%d")
  106. count = f"""SELECT COUNT(*) AS total_count FROM ( SELECT audio, account_id FROM agc_video_deposit WHERE time = '{formatted_time}' AND platform = '{platform}' and mark = '{mark}' GROUP BY audio, account_id) AS subquery;"""
  107. count = MysqlHelper.get_values(count, "prod")
  108. if count == None:
  109. count = 0
  110. count = str(count).replace('(', '').replace(')', '').replace(',', '')
  111. return int(count)
  112. """
  113. 已使用视频链接存表
  114. """
  115. @classmethod
  116. def insert_videoAudio(cls, video_files, uid, platform, mark):
  117. current_time = datetime.now()
  118. formatted_time = current_time.strftime("%Y-%m-%d")
  119. for j in video_files:
  120. insert_sql = f"""INSERT INTO agc_video_deposit (audio, video_id, account_id, oss_object_key, time, platform, mark) values ('{uid}', '{j[0]}', '{j[1]}', '{j[2]}', '{formatted_time}', '{platform}', '{mark}')"""
  121. MysqlHelper.update_values(
  122. sql=insert_sql,
  123. env="prod",
  124. machine="",
  125. )