店播爬取Python脚本

mysql_live_replay.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import time
  2. from libs.db_mysql import DbMysql
  3. class MysqlLiveReplay(DbMysql):
  4. table = 'live_replay'
  5. # 判断任务是否为重试任务
  6. def is_retry_task(self, task_id):
  7. sql = 'SELECT * FROM ' + self.table + ' WHERE `id`=%s AND `status`=%s'
  8. try:
  9. # 执行SQL
  10. self.cursor.execute(sql, [task_id, '3'])
  11. return self.cursor.fetchone()
  12. finally:
  13. self.db.commit() # 提交
  14. self.cursor.close() # 关闭cursor
  15. self.db.close() # 关闭db连接
  16. # 设置下载任务开始状态
  17. def set_task_begin(self, task_id, status):
  18. sql = 'UPDATE ' + self.table + ' SET `status`=%s, `download_begin_at`=%s WHERE `id`=%s'
  19. try:
  20. # 执行SQL
  21. result = self.cursor.execute(sql, [status, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), task_id])
  22. return result
  23. finally:
  24. self.db.commit() # 提交
  25. self.cursor.close() # 关闭cursor
  26. self.db.close() # 关闭db连接
  27. # 修改下载任务状态
  28. def set_task_status(self, task_id, status):
  29. sql = 'UPDATE ' + self.table + ' SET `status`=%s WHERE `id`=%s'
  30. try:
  31. # 执行SQL
  32. result = self.cursor.execute(sql, [status, task_id])
  33. return result
  34. finally:
  35. self.db.commit() # 提交
  36. self.cursor.close() # 关闭cursor
  37. self.db.close() # 关闭db连接
  38. # 设置回放地址
  39. def set_video_url(self, task_id, url):
  40. current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  41. sql = 'UPDATE ' + self.table + ' SET `status`=2,video_url=%s,download_finish_at=%s WHERE `id`=%s'
  42. try:
  43. # 执行SQL
  44. result = self.cursor.execute(sql, [url, current_time, task_id])
  45. return result
  46. finally:
  47. self.db.commit() # 提交
  48. self.cursor.close() # 关闭cursor
  49. self.db.close() # 关闭db连接
  50. # 获取需要上传的直播信息
  51. def get_replay_upload_info(self):
  52. last_download_at = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()) - 3600))
  53. sql = 'SELECT id,live_id FROM ' + self.table + ' WHERE `status`=5 LIMIT 1'
  54. try:
  55. # 执行SQL
  56. self.cursor.execute(sql)
  57. return self.cursor.fetchone()
  58. finally:
  59. self.db.commit() # 提交
  60. self.cursor.close() # 关闭cursor
  61. self.db.close() # 关闭db连接