crawler_pipeline.py 2.2 KB

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