Files
py_facebook/main.py

73 lines
2.0 KiB
Python
Raw Normal View History

2025-03-28 14:50:37 +08:00
from concurrent.futures.thread import ThreadPoolExecutor
from spider.task import *
from logger import error_logger, record_full_log
TASK_TYPE = {
2025-04-08 16:09:45 +08:00
'get_account_profile': playwright_get_user_profile,
'check_account_cookies': playwright_check_account_cookies,
'post': playwright_post
2025-03-28 14:50:37 +08:00
}
2025-04-08 16:09:45 +08:00
HOST = "http://192.168.1.69:8001"
2025-03-28 14:50:37 +08:00
2025-04-08 16:09:45 +08:00
def get_task():
if lock._block.locked():
return
url = f'{HOST}/queue/get-data'
header = {
'Content-Type': 'application/json'
}
response = requests.post(url, headers=header)
if response.status_code == 200:
result = response.json()
2025-03-28 14:50:37 +08:00
return result
2025-04-08 16:09:45 +08:00
def task_callback(tid, data, status='success', msg='success'):
body = {
'id': tid,
'status': status,
'data': data,
'message': msg,
}
2025-03-28 14:50:37 +08:00
response = requests.post(
2025-04-08 16:09:45 +08:00
f'{HOST}/queue/handle-data',
json=body
2025-03-28 14:50:37 +08:00
)
result = response.json()
2025-04-08 16:09:45 +08:00
if response.status_code != 200:
2025-03-28 14:50:37 +08:00
raise RuntimeError(f"任务回调失败:{result['msg']}")
2025-04-08 16:09:45 +08:00
def execute_task(tid, task_type, **kwargs):
2025-03-28 14:50:37 +08:00
try:
result = TASK_TYPE.get(task_type)(**kwargs)
2025-04-08 16:09:45 +08:00
task_callback(tid, data=result)
2025-03-28 14:50:37 +08:00
except AuthException as e:
record_full_log(error_logger, e)
2025-04-08 16:09:45 +08:00
task_callback(tid, data={}, status='fail', msg=str(e))
2025-03-28 14:50:37 +08:00
except Exception as e:
record_full_log(error_logger, e)
2025-04-08 16:09:45 +08:00
task_callback(tid, data={}, status='fail', msg=str(e))
2025-03-28 14:50:37 +08:00
def main():
with ThreadPoolExecutor(max_workers=1) as t:
while True:
try:
2025-04-08 16:09:45 +08:00
task = get_task()
2025-03-28 14:50:37 +08:00
if task is None:
time.sleep(10)
continue
2025-04-08 16:09:45 +08:00
task['data']['tid'] = task['id']
task['data']['task_type'] = task['taskType']
2025-03-28 14:50:37 +08:00
t.submit(execute_task, **task['data'])
except Exception as e:
error_logger.error(f'Main Error: {e}')
time.sleep(10)
if __name__ == '__main__':
main()