tmp8.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import uuid
  3. from datetime import datetime
  4. import pymysql
  5. from dbutils.pooled_db import PooledDB
  6. class MySQLPool:
  7. def __init__(self, host, port, user, password, database, pool_size=5):
  8. self.pool = PooledDB(
  9. creator=pymysql,
  10. maxconnections=pool_size,
  11. host=host,
  12. port=port,
  13. user=user,
  14. password=password,
  15. database=database,
  16. charset='utf8mb4',
  17. cursorclass=pymysql.cursors.DictCursor,
  18. )
  19. def get_connection(self):
  20. return self.pool.connection()
  21. def exec(self, sql, params=None):
  22. conn = self.get_connection()
  23. try:
  24. with conn.cursor() as cursor:
  25. result = cursor.execute(sql, params)
  26. conn.commit()
  27. return result
  28. finally:
  29. conn.close()
  30. # 初始化连接池
  31. pool = MySQLPool(
  32. host='www.tianyunperfect.cn',
  33. port=3306,
  34. user='memos',
  35. password='s3ijTsaH5cciP8s8',
  36. database='memos'
  37. )
  38. base_dir = "/Users/alvin/Downloads/fcf7e810-2472-4a76-9d00-b2e58bf5cf20_Export-b76ceacb-9576-437c-9ad3-fb0cea7a6076/梦记 de2ddb5d286e4dffacb1eb26a4e074be"
  39. def extract_date_and_process(text):
  40. lines = text.splitlines()
  41. # 移除第一行
  42. new_lines = lines[1:]
  43. # 提取第三行(原输入中的第三行,现在索引1)
  44. try:
  45. third_line = new_lines[1]
  46. except IndexError:
  47. return None, "输入内容不足,无法提取日期"
  48. # 解析日期
  49. if ':' in third_line:
  50. parts = third_line.split(':', 1)
  51. date_str = parts[1].strip()
  52. else:
  53. return None, "第三行格式不正确,缺少日期"
  54. # 重新组合字符串
  55. new_text = '\n'.join(new_lines)
  56. return date_str, new_text
  57. for root, dirs, files in os.walk(base_dir):
  58. for file in files:
  59. if file.endswith(".md"):
  60. file_path = os.path.join(root, file)
  61. try:
  62. with open(file_path, "r", encoding="utf-8") as f:
  63. content = f.read()
  64. created_ts, full_content = extract_date_and_process(content)
  65. full_content = "#梦记 " + full_content
  66. # 生成 resource_name uuid 去除特殊字符
  67. resource_name = str(uuid.uuid4()).replace('-', '')
  68. # 构建 SQL 参数
  69. params = (
  70. resource_name,
  71. 1, # creator_id
  72. created_ts, # created_ts
  73. created_ts, # updated_ts
  74. 'NORMAL', # row_status
  75. full_content, # content
  76. 'PRIVATE' # visibility
  77. )
  78. # 执行插入
  79. sql = """
  80. INSERT INTO memo
  81. (resource_name, creator_id, created_ts, updated_ts, row_status, content, visibility)
  82. VALUES (%s, %s, %s, %s, %s, %s, %s)
  83. """
  84. # 打印 SQL 参数
  85. print(f"SQL 参数: {params}")
  86. result = pool.exec(sql, params)
  87. print(f"成功插入: {file},影响行数: {result}")
  88. except Exception as e:
  89. print(f"处理文件 {file} 时出错: {str(e)}")