| 22 | |
| 23 | |
| 24 | def parse_markdown(): |
| 25 | # 优先解析中文README文件 |
| 26 | readme_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'README_ZH.md') |
| 27 | if not os.path.exists(readme_path): |
| 28 | readme_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'README.md') |
| 29 | |
| 30 | with open(readme_path, 'r', encoding='utf-8') as file: |
| 31 | content = file.read() |
| 32 | |
| 33 | pattern = r'(?<=往期列表)(.*?)(?=\n## )' |
| 34 | section = re.search(pattern, content, re.DOTALL) |
| 35 | if not section: |
| 36 | return [] |
| 37 | |
| 38 | entries = [] |
| 39 | lines = section.group(0).splitlines() |
| 40 | for i in range(len(lines)): |
| 41 | issue_date_match = re.match(r'- 第 (\d+) 期:.*?\[(.*?)\]\(.*?(\d{4}-\d{2}-\d{2})-weekly.md\)', lines[i]) |
| 42 | if issue_date_match: |
| 43 | issue_no = int(issue_date_match.group(1)) |
| 44 | date = issue_date_match.group(3) |
| 45 | |
| 46 | # 从下一行开始匹配文章数、开源项目数、音视频数、热门话题数、赠书数 |
| 47 | if i + 1 < len(lines): |
| 48 | next_line = lines[i + 1] |
| 49 | article_count = int(re.search(r'(\d+) 篇', next_line).group(1)) if re.search(r'(\d+) 篇', next_line) else 0 |
| 50 | project_count = int(re.search(r'(\d+) 个开源', next_line).group(1)) if re.search(r'(\d+) 个开源', next_line) else 0 |
| 51 | |
| 52 | audio_video_match = re.search(r'(\d+) 则音|(\d+) 则视|(\d+) 则播', next_line) |
| 53 | if audio_video_match: |
| 54 | audio_video_count = int(audio_video_match.group(1) or audio_video_match.group(2) or audio_video_match.group(3) or 0) |
| 55 | else: |
| 56 | audio_video_count = 0 |
| 57 | |
| 58 | hot_topic_count = int(re.search(r'(\d+) [个|则]热门', next_line).group(1)) if re.search(r'(\d+) [个|则]热门', next_line) else 0 |
| 59 | book_count = int(re.search(r'赠书 (\d+) 本', next_line).group(1)) if re.search(r'赠书 (\d+) 本', next_line) else 0 |
| 60 | |
| 61 | entries.append({ |
| 62 | 'issue_no': issue_no, |
| 63 | 'date': date, |
| 64 | 'article_count': article_count, |
| 65 | 'project_count': project_count, |
| 66 | 'audio_video_count': audio_video_count, |
| 67 | 'hot_topic_count': hot_topic_count, |
| 68 | 'book_count': book_count |
| 69 | }) |
| 70 | return entries |
| 71 | |
| 72 | def insert_into_database(entries): |
| 73 | with sqlite3.connect(db_path) as conn: |