-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_phil_wilson_posts.py
More file actions
108 lines (97 loc) · 4.72 KB
/
Copy pathextract_phil_wilson_posts.py
File metadata and controls
108 lines (97 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import re
import xml.etree.ElementTree as ET
import html2text
# This script extracts posts authored by Phil Wilson from the University of Bath's Web Services Blog RSS feed
# and converts them to Markdown format with appropriate frontmatter for Zola.
# It assumes the XML file is well-formed and contains the necessary elements.
# The output will be saved in a specified directory, organized by year and month based on the publication date.
# Path to the XML file and output directory
XML_PATH = 'webservices.xml'
OUTPUT_DIR = 'markdown'
# Ensure output directory exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
def sanitize_filename(title):
"""Create a safe filename from the post title."""
filename = re.sub(r'[^a-zA-Z0-9\-_ ]', '', title)
filename = filename.strip().replace(' ', '-').lower()
return filename + '.md'
def extract_phil_wilson_posts(xml_path, output_dir):
# Parse the XML file
tree = ET.parse(xml_path)
root = tree.getroot()
ns = {
'dc': 'http://purl.org/dc/elements/1.1/',
'ns2': 'http://purl.org/rss/1.0/modules/content/'
}
from datetime import datetime
import email.utils
for item in root.iter('item'):
creator = item.find('dc:creator', ns)
if creator is not None and creator.text == 'Phil Wilson':
title_elem = item.find('title')
encoded_elem = item.find('ns2:encoded', ns)
desc_elem = item.find('description')
link_elem = item.find('link')
pubdate_elem = item.find('pubDate')
categories = [cat.text for cat in item.findall('category') if cat.text]
if title_elem is not None:
title = title_elem.text or 'untitled'
if encoded_elem is not None and encoded_elem.text and encoded_elem.text.strip():
content = encoded_elem.text.strip()
elif desc_elem is not None and desc_elem.text:
content = desc_elem.text.strip()
else:
content = ''
filename = sanitize_filename(title)
# Determine output subdirectory based on date
subdir = OUTPUT_DIR
if pubdate_elem is not None and pubdate_elem.text:
try:
# Parse RFC822 date and format as YYYY-MM-DD HH:MM:SS
parsed = email.utils.parsedate_to_datetime(pubdate_elem.text)
date_str = parsed.strftime('%Y-%m-%d %H:%M:%S')
year, month = date_str.split()[0].split('-')[:2]
subdir = os.path.join(OUTPUT_DIR, year, month)
os.makedirs(subdir, exist_ok=True)
except Exception:
pass
out_path = os.path.join(subdir, filename)
# Extract slug from link
slug = ''
if link_elem is not None and link_elem.text:
# Remove trailing slash, then take last path segment
parts = link_elem.text.rstrip('/').split('/')
if parts:
slug = parts[-1]
# Build frontmatter
frontmatter = ['---']
frontmatter.append(f'title: "{title}"')
if slug:
frontmatter.append(f'slug: {slug}')
if date_str:
frontmatter.append(f'date: {date_str}')
# Output categories as tags under taxonomies, always lowercase
if categories or True:
frontmatter.append('taxonomies:')
frontmatter.append(' tags:')
for cat in categories:
frontmatter.append(f' - {cat.lower()}')
frontmatter.append(' collections:')
frontmatter.append(' - university of bath')
# Add extra metadata
frontmatter.append('extra:')
frontmatter.append(' originalLocationName: The University of Bath\'s Web Services Blog')
if link_elem is not None and link_elem.text:
frontmatter.append(f' originalUrl: {link_elem.text}')
frontmatter.append('---\n')
# Convert HTML content to Markdown with no line length limit
h2t = html2text.HTML2Text()
h2t.body_width = 0 # No line wrapping
content_md = h2t.handle(content)
with open(out_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(frontmatter))
f.write(content_md + '\n')
print(f'Wrote: {out_path}')
if __name__ == '__main__':
extract_phil_wilson_posts(XML_PATH, OUTPUT_DIR)