You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
# coding=utf-8
|
|
from pathlib import Path
|
|
|
|
import config
|
|
from utils.ai_seo_api_utils import AiSeoApis
|
|
|
|
|
|
async def get_spider_session(platform_id, id=''):
|
|
"""
|
|
获取可用的爬虫session
|
|
:param platform_id:
|
|
:return:
|
|
"""
|
|
base_path = f'{config.ROOT_PATH}/data/session_data'
|
|
# 爬虫信息
|
|
session_info = await AiSeoApis.get_spider_session(platform_id, id)
|
|
if not session_info:
|
|
raise Exception(f"平台id: {platform_id} 没有可用的爬虫session")
|
|
if platform_id == 8:
|
|
return session_info
|
|
# 根据id去爬虫文件夹中找
|
|
target = search_session_file(session_info['id'], base_path)
|
|
# 如果没有找到 下载这个文件并保存
|
|
if not target:
|
|
await AiSeoApis.download_spider_session_file(session_info['url'], F"{base_path}/{session_info['id']}.json")
|
|
target = f"{session_info['id']}.json"
|
|
else:
|
|
target = target[0]
|
|
session_info['session_path'] = f"{base_path}/{target}"
|
|
return session_info
|
|
|
|
|
|
def search_session_file(session_id, path):
|
|
folder_path = Path(path)
|
|
file_filter = f"{session_id}.json"
|
|
return [file.name for file in folder_path.glob(file_filter)]
|
|
|
|
async def main():
|
|
path = await get_spider_session(1)
|
|
print(path)
|
|
|
|
if __name__ == '__main__':
|
|
import asyncio
|
|
asyncio.run(main())
|