update_info.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import requests
  6. import pymysql
  7. from tqdm import tqdm
  8. class UpdateInfo(object):
  9. """
  10. updateInfo
  11. """
  12. connection = pymysql.connect(
  13. host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
  14. port=3306,
  15. user='crawler',
  16. password='crawler123456@',
  17. db='piaoquan-crawler',
  18. charset='utf8mb4')
  19. @classmethod
  20. def request_for_info(cls, video_id):
  21. """
  22. request for info
  23. :param video_id:
  24. :return:
  25. """
  26. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  27. data = {
  28. "videoIdList": [video_id]
  29. }
  30. header = {
  31. "Content-Type": "application/json",
  32. }
  33. response = requests.post(url, headers=header, data=json.dumps(data))
  34. return response.json()
  35. @classmethod
  36. def updateInfoToMysql(cls, video_id, uid, title):
  37. """
  38. :param video_id:
  39. :param uid:
  40. :param title:
  41. :return:
  42. """
  43. update_sql = f"""
  44. update article_match_videos
  45. set video_title = %s, uid = %s, oss_status = %s
  46. where video_id = %s;"""
  47. cursor = cls.connection.cursor()
  48. cursor.execute(
  49. update_sql,
  50. (
  51. title, uid, 1, video_id
  52. )
  53. )
  54. cls.connection.commit()
  55. @classmethod
  56. def select_video_info(cls):
  57. """
  58. :return:
  59. """
  60. sql = f"""
  61. select video_id from article_match_videos where video_path is not NULL and uid is NULL;
  62. """
  63. cursor = cls.connection.cursor()
  64. cursor.execute(sql)
  65. data = cursor.fetchall()
  66. for line in tqdm(data):
  67. video_id = line[0]
  68. detail = cls.request_for_info(video_id)
  69. uid = detail['data'][0]['user']['uid']
  70. title = detail['data'][0]['title']
  71. if "search" not in title:
  72. cls.updateInfoToMysql(video_id, uid, title)
  73. U = UpdateInfo()
  74. U.select_video_info()