crawler_pipeline.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 whether_duplicate_article_title(article_title: str, db_client) -> bool:
  36. fetch_query = f"""
  37. select article_id from crawler_meta_article
  38. where title = %s;
  39. """
  40. duplicate_id = db_client.fetch(query=fetch_query, params=(article_title,))
  41. if duplicate_id:
  42. return True
  43. return False
  44. def scrape_video_entities_process(video_item, db_client) -> dict:
  45. """
  46. video crawler pipeline
  47. """
  48. article_url = video_item["article_url"]
  49. platform = video_item["platform"]
  50. video_title = video_item["article_title"]
  51. # whether title sensitive
  52. if whether_title_sensitive(video_title):
  53. return empty_dict
  54. # whether duplicate video title
  55. if whether_duplicate_video_title(video_title, db_client):
  56. return empty_dict
  57. # download video
  58. match platform:
  59. case "toutiao":
  60. video_path = download_toutiao_video(article_url)
  61. case "gzh":
  62. video_path = download_gzh_video(article_url)
  63. case "hksp":
  64. video_path = ""
  65. case "sph":
  66. video_path = ""
  67. case "sohu":
  68. video_path = download_sohu_video(article_url)
  69. case "piaoquan":
  70. return video_item
  71. case _:
  72. return empty_dict
  73. if video_path:
  74. # upload video to oss
  75. oss_path = upload_to_oss(video_path)
  76. video_item["video_oss_path"] = oss_path
  77. os.remove(video_path)
  78. return video_item
  79. else:
  80. return empty_dict
  81. def scrape_article_entities_process(article_item, db_client) -> dict:
  82. """
  83. article crawler pipeline
  84. """
  85. article_title = article_item['title']
  86. if whether_duplicate_article_title(article_title, db_client):
  87. return empty_dict
  88. # whether sensitive title
  89. if whether_title_sensitive(article_title):
  90. article_item['title_sensitivity'] = 1
  91. return article_item
  92. return article_item