fetch_info_from_aigc.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. fetch info from aigc database system
  3. """
  4. from collections import defaultdict
  5. from typing import List, Dict
  6. from pymysql.cursors import DictCursor
  7. def fetch_publishing_account_list(db_client) -> List[Dict]:
  8. """
  9. fetch account_list from aigc database
  10. """
  11. fetch_sql = f"""
  12. SELECT DISTINCT
  13. t3.`name` as account_name,
  14. t3.gh_id as gh_id,
  15. t3.follower_count as fans,
  16. t6.account_source_name as account_source,
  17. t6.mode_type as mode_type,
  18. t6.account_type as account_type,
  19. t6.`status` as status
  20. FROM
  21. publish_plan t1
  22. JOIN publish_plan_account t2 ON t1.id = t2.plan_id
  23. JOIN publish_account t3 ON t2.account_id = t3.id
  24. LEFT JOIN publish_account_wx_type t4 on t3.id = t4.account_id
  25. LEFT JOIN wx_statistics_group_source_account t5 on t3.id = t5.account_id
  26. LEFT JOIN wx_statistics_group_source t6 on t5.group_source_name = t6.account_source_name
  27. WHERE
  28. t1.plan_status = 1
  29. AND t3.channel = 5
  30. GROUP BY t3.id;
  31. """
  32. account_list = db_client.fetch(
  33. query=fetch_sql,
  34. cursor_type=DictCursor
  35. )
  36. return account_list
  37. def fetch_account_fans(db_client, start_date: str) -> Dict:
  38. """
  39. fetch account fans from aigc database
  40. """
  41. sql = f"""
  42. SELECT t1.date_str, t1.fans_count, t2.gh_id
  43. FROM datastat_wx t1 JOIN publish_account t2 ON t1.account_id = t2.id
  44. WHERE t2.channel = 5
  45. AND t1.fans_count > 0
  46. AND t1.date_str >= '{start_date}'
  47. ORDER BY t1.date_str;
  48. """
  49. result = db_client.fetch(sql)
  50. fans_dict = defaultdict(dict)
  51. for dt, fans, gh_id in result:
  52. fans_dict.setdefault(gh_id, {})[dt] = fans
  53. return fans_dict