insert_video.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/3/28
  4. import json
  5. import os
  6. import sys
  7. sys.path.append(os.getcwd())
  8. from common.common import Common
  9. from common.scheduling_db import MysqlHelper
  10. from common.feishu import Feishu
  11. class Insert:
  12. @classmethod
  13. def insert_video_from_feishu_to_mysql(cls, log_type, crawler, env, machine):
  14. gongzhonghao_sheetid_list = ['47e39d']
  15. for sheetid in gongzhonghao_sheetid_list:
  16. gongzhonghao_sheet = Feishu.get_values_batch(log_type, crawler, sheetid)
  17. for i in range(1, len(gongzhonghao_sheet)):
  18. # for i in range(1, 3):
  19. if gongzhonghao_sheet[i][5] is None or gongzhonghao_sheet[i][9] is None:
  20. continue
  21. video_id = gongzhonghao_sheet[i][9].replace("https://admin.piaoquantv.com/cms/post-detail/", "").replace("/info", "")
  22. if video_id == "None":
  23. continue
  24. video_id = int(video_id)
  25. out_user_id = str(gongzhonghao_sheet[i][14])
  26. platform = "公众号"
  27. strategy = "定向爬虫策略"
  28. out_video_id = str(gongzhonghao_sheet[i][8])
  29. video_title = str(gongzhonghao_sheet[i][7])
  30. cover_url = str(gongzhonghao_sheet[i][16])
  31. video_url = str(gongzhonghao_sheet[i][18])
  32. duration = int(gongzhonghao_sheet[i][10])
  33. publish_time = str(gongzhonghao_sheet[i][12]).replace("/", "-")
  34. crawler_rule = json.dumps({"play_cnt": {"min": 0}, "duration": {"min": 20}, "publish_day": {"min": 0}})
  35. width = int(gongzhonghao_sheet[i][11].split("*")[0])
  36. height = int(gongzhonghao_sheet[i][11].split("*")[1])
  37. # print(f"video_id:{video_id}, type:{type(video_id)}")
  38. # print(f"out_user_id:{out_user_id}, type:{type(out_user_id)}")
  39. # print(f"platform:{platform}, type:{type(platform)}")
  40. # print(f"strategy:{strategy}, type:{type(strategy)}")
  41. # print(f"out_video_id:{out_video_id}, type:{type(out_video_id)}")
  42. # print(f"video_title:{video_title}, type:{type(video_title)}")
  43. # print(f"cover_url:{cover_url}, type:{type(cover_url)}")
  44. # print(f"video_url:{video_url}, type:{type(video_url)}")
  45. # print(f"duration:{duration}, type:{type(duration)}")
  46. # print(f"publish_time:{publish_time}, type:{type(publish_time)}")
  47. # print(f"crawler_rule:{crawler_rule}, type:{type(crawler_rule)}")
  48. # print(f"width:{width}, type:{type(width)}")
  49. # print(f"height:{height}, type:{type(height)}\n")
  50. select_sql = f""" select * from crawler_video where platform="{platform}" and out_video_id="{out_video_id}" """
  51. Common.logger(log_type, crawler).info(f"select_sql:{select_sql}")
  52. repeat_video = MysqlHelper.get_values(log_type, crawler, select_sql, env, machine)
  53. Common.logger(log_type, crawler).info(f"repeat_video:{repeat_video}")
  54. if repeat_video is not None and len(repeat_video) != 0:
  55. Common.logger(log_type, crawler).info(f"{video_title} 已存在数据库中\n")
  56. else:
  57. # 视频信息保存数据库
  58. insert_sql = f""" insert into crawler_video(video_id,
  59. out_user_id,
  60. platform,
  61. strategy,
  62. out_video_id,
  63. video_title,
  64. cover_url,
  65. video_url,
  66. duration,
  67. publish_time,
  68. crawler_rule,
  69. width,
  70. height)
  71. values({video_id},
  72. "{out_user_id}",
  73. "{platform}",
  74. "{strategy}",
  75. "{out_video_id}",
  76. "{video_title}",
  77. "{cover_url}",
  78. "{video_url}",
  79. {duration},
  80. "{publish_time}",
  81. '{crawler_rule}',
  82. {width},
  83. {height}) """
  84. Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
  85. MysqlHelper.update_values(log_type, crawler, insert_sql, env, machine)
  86. Common.logger(log_type, crawler).info('视频信息插入数据库成功!\n')
  87. if __name__ == "__main__":
  88. Insert.insert_video_from_feishu_to_mysql("insert-prod", "gongzhonghao", "prod", "local")
  89. pass