miner_u.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. import sys
  3. import logging
  4. from urllib.parse import urlparse
  5. # 配置日志
  6. logging.basicConfig(
  7. level=logging.INFO,
  8. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  9. )
  10. logger = logging.getLogger("MinerU")
  11. # 导入配置和管理器
  12. sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
  13. from app.base.mineru_connection import get_mineru_manager
  14. from app.base.async_mysql_connection import get_db_connection
  15. def main_cli(doc_id):
  16. """
  17. MinerU 命令行入口,供后台进程调用
  18. """
  19. manager = get_mineru_manager()
  20. conn = get_db_connection()
  21. if not conn:
  22. logger.error("数据库连接失败")
  23. return
  24. try:
  25. with conn.cursor() as cursor:
  26. # 2. 获取文档基本信息 (主表即为唯一资产中心)
  27. cursor.execute("SELECT title, file_url FROM t_samp_document_main WHERE id = %s", (doc_id,))
  28. row = cursor.fetchone()
  29. if not row:
  30. logger.warning(f"文档不存在: {doc_id}")
  31. return
  32. title = row['title']
  33. file_url = row['file_url']
  34. if not file_url:
  35. logger.error(f"未找到文件链接: {doc_id}")
  36. manager.update_db_status(doc_id, status=3, error="未找到文件链接(file_url)")
  37. return
  38. # 3. 调用管理器执行转换
  39. logger.info(f"开始处理文档 [{doc_id}]: {title}")
  40. manager.process_document(doc_id, title, file_url)
  41. except Exception as e:
  42. logger.exception(f"处理文档 [{doc_id}] 时发生未捕获异常: {e}")
  43. manager.update_db_status(doc_id, status=3, error=str(e))
  44. finally:
  45. conn.close()
  46. if __name__ == "__main__":
  47. if len(sys.argv) > 1:
  48. # 兼容旧格式: python miner_u.py <table_type> <doc_id>
  49. # 兼容新格式: python miner_u.py <doc_id>
  50. target_id = sys.argv[-1]
  51. main_cli(target_id)
  52. else:
  53. print("Usage: python miner_u.py <doc_id>")