utils_timing_rss_gen.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*-coding: utf-8 -*-
  2. '''
  3. 在批量执行 rss 爬虫时, 获取数据后会生成 rss 文件,无需执行此代码, 如需手动生成 rss 文件,执行此代码即可
  4. '''
  5. import os
  6. import threading
  7. import PyRSS2Gen
  8. import time
  9. import pymongo
  10. import tools_load_config
  11. config_json = tools_load_config.load_config()
  12. base_project = tools_load_config.get_base_path()
  13. PROJECT_NAME = config_json.get('PROJECT_NAME')
  14. DB_USER = config_json.get('DB_USER')
  15. DB_PASSWORD = config_json.get('DB_PASSWORD')
  16. DB_IP = config_json.get('DB_IP')
  17. DB_PORT = config_json.get('DB_PORT')
  18. MONGO_LINK = f'mongodb://{DB_USER}:{DB_PASSWORD}@{DB_IP}:{DB_PORT}/'
  19. MAIL_HOST = config_json.get('MAIL_HOST')
  20. MAIL_USER = config_json.get('MAIL_USER')
  21. MAIL_PASS = config_json.get('MAIL_PASS')
  22. MAIL_SENDER = config_json.get('MAIL_SENDER')
  23. MAIL_RECEIVERS = config_json.get('MAIL_RECEIVERS')
  24. now_day = time.strftime('%Y-%m-%d', time.localtime())
  25. rss_base_url = 'http://home.erhe.link:20002/xmlfile/'
  26. base_project = os.path.join(os.getcwd().split(PROJECT_NAME)[0], PROJECT_NAME)
  27. def string_mapping(string):
  28. mapping_dict = {
  29. '掌设': 'handheld',
  30. '汽车': 'car',
  31. '评测': 'testing',
  32. '美食': 'food',
  33. '电脑': 'pc',
  34. '视听': 'audiovisual',
  35. '腕表': 'watch',
  36. '单车': 'bicycle',
  37. '摄影': 'photograph',
  38. '家居': 'home'
  39. }
  40. for key in mapping_dict:
  41. if key in string:
  42. string = string.replace(key, mapping_dict[key])
  43. return string
  44. def handle_data(db_and_collection):
  45. db = db_and_collection['db']
  46. collection = db_and_collection['collection']
  47. title = db.replace('RSS_', '')
  48. print(f'读取 {db} -- {collection} 数据')
  49. client = pymongo.MongoClient(MONGO_LINK)
  50. _get_db = client[db]
  51. _get_collection = _get_db[collection]
  52. gen_file_name = f'{title}_{collection}_rss.xml'
  53. # 替换中英文
  54. gen_file_name = string_mapping(gen_file_name)
  55. link = db_and_collection['link']
  56. description = db_and_collection['source_type']
  57. lastBuildDate = now_day
  58. items = []
  59. xml_file = os.path.join(base_project, 'news', 'rss_xmlfile')
  60. path = os.path.join(xml_file, gen_file_name)
  61. xml_url_file = os.path.join(xml_file, 'rss_url.txt')
  62. # TODO
  63. # load mongodb
  64. # save to item
  65. for data in _get_collection.find():
  66. t = data.setdefault('title')
  67. d = data.setdefault('context')
  68. l = data.setdefault('source_url')
  69. item = PyRSS2Gen.RSSItem(
  70. title=t,
  71. link=l,
  72. description=d,
  73. pubDate=data.setdefault('posted_date'),
  74. )
  75. items.append(item)
  76. gen2rss(gen_file_name, title, link, description, lastBuildDate, items, path)
  77. with open(xml_url_file, 'a') as f:
  78. f.write(rss_base_url + gen_file_name + '\n\n')
  79. def gen2rss(gen_file_name, title, link, description, lastBuildDate, items, path):
  80. rss = PyRSS2Gen.RSS2(
  81. title=title,
  82. link=link,
  83. description=description,
  84. lastBuildDate=lastBuildDate,
  85. items=items)
  86. print('正在生成rss文件: 路径: %s, 文件名: %s' % (path, gen_file_name))
  87. rss.write_xml(open(path, "w", encoding='utf-8'), encoding='utf-8')
  88. def run():
  89. xml_file = os.path.join(os.getcwd().split(PROJECT_NAME)[0], PROJECT_NAME, 'news', 'rss_xmlfile')
  90. if not os.path.exists(xml_file):
  91. os.mkdir(xml_file)
  92. rss_url_txt = os.path.join(xml_file, 'rss_url.txt')
  93. if os.path.exists(rss_url_txt):
  94. os.remove(rss_url_txt)
  95. # 每一个列表, 0数据库名, 1爬虫目标网站url, 2目标网站名称
  96. dbs = [
  97. ['RSS_HelloGithub', 'https://hellogithub.com/', 'HelloGithub'],
  98. ['RSS_apprcn', 'https://free.apprcn.com/', '反斗限免'],
  99. ['RSS_news', 'https://www.anyknew.com/', '聚合新闻'],
  100. ['RSS_chiphell', 'https://www.chiphell.com/', 'chiphell']
  101. ]
  102. db_and_collections = []
  103. for db in dbs:
  104. client = pymongo.MongoClient(MONGO_LINK)
  105. if db[0] not in client.list_database_names():
  106. print(f'找不到数据库 {db[0]}')
  107. continue
  108. _get_db = client[db[0]]
  109. for collection in _get_db.list_collection_names():
  110. db_and_collections.append({
  111. 'db': db[0],
  112. 'collection': collection,
  113. 'link': db[1],
  114. 'source_type': db[2]
  115. })
  116. # for d in db_and_collections:
  117. # handle_data(d)
  118. # 进程列表
  119. threads = []
  120. # 使用 for 循环批量创建线程
  121. for i in db_and_collections:
  122. thread = threading.Thread(target=handle_data, args=(i,))
  123. threads.append(thread)
  124. thread.start()
  125. # 等待所有线程执行完成
  126. for thread in threads:
  127. thread.join()
  128. run()