source_qianlima_mt.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. import random
  3. import time
  4. from concurrent.futures import Future, ThreadPoolExecutor, wait
  5. from loguru import logger
  6. from source_qianlima import (
  7. core as reqeust,
  8. disrupt_account_pool
  9. )
  10. from utils.config_parms import area_dict, city_dict, province_dict, channel_dict
  11. from utils.tools import get_today_of_day
  12. def spider(account):
  13. date = get_today_of_day(-1)
  14. follow = account['follow']
  15. phone = account['phone']
  16. try:
  17. for category, category_name in channel_dict.items():
  18. for area_id in follow:
  19. cities = area_dict[area_id]
  20. for city in cities:
  21. logger.info(' && '.join([
  22. phone,
  23. date,
  24. category_name,
  25. province_dict[area_id],
  26. city_dict[city]
  27. ]))
  28. if len(cities) == 1:
  29. city = area_id
  30. reqeust(date, category, city, account, page_size=100)
  31. except Exception as e:
  32. raise e
  33. def show_exception(f: Future):
  34. error = f.exception()
  35. if error:
  36. logger.exception(f'工作线程运行错误:{error}')
  37. def delay_bar():
  38. try:
  39. interval = random.randint(300, 900)
  40. ts = time.time()
  41. start_dt = time.strftime('%Y-%m-%d %H:%M:%S',
  42. time.localtime(ts + interval))
  43. i = 0
  44. logger.info(f'采集开始时间:{start_dt}')
  45. while time.time() - ts < interval:
  46. dots = '.' * i
  47. print(f'\r请稍候{dots}', end='', flush=True)
  48. time.sleep(.5)
  49. if i >= 3:
  50. i = 0
  51. else:
  52. i += 1
  53. finally:
  54. print('')
  55. def start():
  56. try:
  57. delay_bar()
  58. logger.info('+++ 采集开始 +++')
  59. account_pool = disrupt_account_pool()
  60. with ThreadPoolExecutor(max_workers=5) as pool:
  61. fs = []
  62. for account in account_pool:
  63. f = pool.submit(spider, account)
  64. f.add_done_callback(show_exception)
  65. wait(fs)
  66. except KeyboardInterrupt:
  67. pass
  68. finally:
  69. print('')
  70. logger.info('+++ 采集结束 +++')
  71. if __name__ == '__main__':
  72. start()