crawler_pipeline.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. @author: luojunhui
  3. """
  4. import os
  5. import json
  6. from applications import log
  7. from applications.utils import download_gzh_video
  8. from applications.utils import download_toutiao_video
  9. from applications.utils import upload_to_oss
  10. from config import apolloConfig
  11. my_config = apolloConfig()
  12. empty_dict = {}
  13. sensitive_word_list = json.loads(my_config.getConfigValue("sensitive_word_list"))
  14. def whether_title_sensitive(title: str) -> bool:
  15. """
  16. title sensitive words filter
  17. """
  18. for word in sensitive_word_list:
  19. if word in title:
  20. return True
  21. return False
  22. def whether_duplicate_video_title(video_title: str, db_client) -> bool:
  23. """
  24. whether duplicate video title
  25. """
  26. sql = f"""
  27. select id from publish_single_video_source
  28. where article_title = %s;
  29. """
  30. duplicate_id = db_client.fetch(query=sql, params=(video_title,))
  31. if duplicate_id:
  32. return True
  33. return False
  34. def scrape_video_entities_process(video_item, db_client) -> dict:
  35. """
  36. video crawler pipeline
  37. """
  38. article_url = video_item["article_url"]
  39. platform = video_item["platform"]
  40. video_title = video_item["article_title"]
  41. # whether title sensitive
  42. if whether_title_sensitive(video_title):
  43. return empty_dict
  44. # whether duplicate video title
  45. if whether_duplicate_video_title(video_title, db_client):
  46. return empty_dict
  47. # download video
  48. match platform:
  49. case "toutiao":
  50. video_path = download_toutiao_video(article_url)
  51. case "gzh":
  52. video_path = download_gzh_video(article_url)
  53. case "hksp":
  54. video_path = ""
  55. case "sph":
  56. video_path = ""
  57. case _:
  58. return empty_dict
  59. if video_path:
  60. # upload video to oss
  61. oss_path = upload_to_oss(video_path)
  62. video_item["video_oss_path"] = oss_path
  63. os.remove(video_path)
  64. return video_item
  65. else:
  66. return empty_dict