askrobots / dbbasic-rss
Public
RSS feeds from TSV, CSV, JSON or Python lists, for any Python web framework or a static site.
generators.py · 9.6 KB · 318 lines
| 1 | """Data source generators for RSS feeds""" |
| 2 | |
| 3 | import json |
| 4 | import csv |
| 5 | from pathlib import Path |
| 6 | from typing import List, Dict, Any, Optional, Callable |
| 7 | from .feed import Feed, FeedItem |
| 8 | |
| 9 | |
| 10 | def from_posts( |
| 11 | posts: List[Dict[str, Any]], |
| 12 | title: str = "RSS Feed", |
| 13 | link: str = "", |
| 14 | description: str = "", |
| 15 | url_pattern: Optional[str] = None, |
| 16 | title_field: str = "title", |
| 17 | date_field: str = "date", |
| 18 | content_field: str = "content", |
| 19 | description_field: Optional[str] = None, |
| 20 | author_field: str = "author", |
| 21 | url_field: Optional[str] = "url", |
| 22 | categories_field: Optional[str] = None, |
| 23 | **feed_kwargs |
| 24 | ) -> Feed: |
| 25 | """ |
| 26 | Generate RSS feed from list of dictionaries/objects. |
| 27 | |
| 28 | Args: |
| 29 | posts: List of post dictionaries |
| 30 | title: Feed title |
| 31 | link: Feed URL |
| 32 | description: Feed description |
| 33 | url_pattern: URL pattern with {field} placeholders (e.g., 'https://site.com/{slug}/') |
| 34 | title_field: Field name for post title |
| 35 | date_field: Field name for publication date |
| 36 | content_field: Field name for content/description |
| 37 | description_field: Optional separate field for description |
| 38 | author_field: Field name for author |
| 39 | url_field: Field name for URL (if url_pattern not used) |
| 40 | categories_field: Field name for categories (can be comma-separated string or list) |
| 41 | **feed_kwargs: Additional Feed() arguments |
| 42 | |
| 43 | Returns: |
| 44 | Populated Feed object |
| 45 | |
| 46 | Example: |
| 47 | posts = [ |
| 48 | {'title': 'Post 1', 'date': '2025-10-19', 'content': 'Text...', 'slug': 'post-1'}, |
| 49 | ] |
| 50 | feed = from_posts(posts, url_pattern='https://example.com/{slug}/') |
| 51 | """ |
| 52 | feed = Feed(title=title, link=link, description=description, **feed_kwargs) |
| 53 | |
| 54 | for post in posts: |
| 55 | # Get field values with fallbacks |
| 56 | item_title = post.get(title_field, "Untitled") |
| 57 | item_content = post.get(content_field, "") |
| 58 | item_description = post.get(description_field) if description_field else item_content |
| 59 | item_date = post.get(date_field) |
| 60 | item_author = post.get(author_field) |
| 61 | |
| 62 | # Handle URL generation |
| 63 | if url_pattern: |
| 64 | # Replace {field} placeholders with post values |
| 65 | item_url = url_pattern |
| 66 | for key, value in post.items(): |
| 67 | item_url = item_url.replace(f'{{{key}}}', str(value)) |
| 68 | else: |
| 69 | item_url = post.get(url_field, "") |
| 70 | |
| 71 | # Handle categories |
| 72 | categories = [] |
| 73 | if categories_field and categories_field in post: |
| 74 | cats = post[categories_field] |
| 75 | if isinstance(cats, str): |
| 76 | categories = [c.strip() for c in cats.split(',') if c.strip()] |
| 77 | elif isinstance(cats, list): |
| 78 | categories = cats |
| 79 | |
| 80 | feed.add_post( |
| 81 | title=item_title, |
| 82 | link=item_url, |
| 83 | description=item_description, |
| 84 | content=item_content if description_field else None, # Only use content if separate description |
| 85 | pub_date=item_date, |
| 86 | author=item_author, |
| 87 | categories=categories, |
| 88 | ) |
| 89 | |
| 90 | return feed |
| 91 | |
| 92 | |
| 93 | def from_tsv( |
| 94 | filepath: str, |
| 95 | title: str = "RSS Feed", |
| 96 | link: str = "", |
| 97 | description: str = "", |
| 98 | url_pattern: Optional[str] = None, |
| 99 | **kwargs |
| 100 | ) -> Feed: |
| 101 | """ |
| 102 | Generate RSS feed from TSV file. |
| 103 | |
| 104 | Args: |
| 105 | filepath: Path to TSV file |
| 106 | title: Feed title |
| 107 | link: Feed URL |
| 108 | description: Feed description |
| 109 | url_pattern: URL pattern with {field} placeholders |
| 110 | **kwargs: Additional arguments passed to from_posts() |
| 111 | |
| 112 | Returns: |
| 113 | Populated Feed object |
| 114 | |
| 115 | Example: |
| 116 | feed = from_tsv('articles.tsv', |
| 117 | title='My Blog', |
| 118 | url_pattern='https://example.com/{slug}/') |
| 119 | """ |
| 120 | try: |
| 121 | from dbbasic_tsv import TSV |
| 122 | table = TSV(filepath) |
| 123 | posts = table.select() |
| 124 | except ImportError: |
| 125 | # Fallback to standard CSV reader if dbbasic_tsv not available |
| 126 | posts = [] |
| 127 | with open(filepath, 'r', encoding='utf-8') as f: |
| 128 | reader = csv.DictReader(f, delimiter='\t') |
| 129 | posts = list(reader) |
| 130 | |
| 131 | return from_posts(posts, title=title, link=link, description=description, |
| 132 | url_pattern=url_pattern, **kwargs) |
| 133 | |
| 134 | |
| 135 | def from_csv( |
| 136 | filepath: str, |
| 137 | title: str = "RSS Feed", |
| 138 | link: str = "", |
| 139 | description: str = "", |
| 140 | url_pattern: Optional[str] = None, |
| 141 | **kwargs |
| 142 | ) -> Feed: |
| 143 | """ |
| 144 | Generate RSS feed from CSV file. |
| 145 | |
| 146 | Args: |
| 147 | filepath: Path to CSV file |
| 148 | title: Feed title |
| 149 | link: Feed URL |
| 150 | description: Feed description |
| 151 | url_pattern: URL pattern with {field} placeholders |
| 152 | **kwargs: Additional arguments passed to from_posts() |
| 153 | |
| 154 | Returns: |
| 155 | Populated Feed object |
| 156 | |
| 157 | Example: |
| 158 | feed = from_csv('posts.csv', |
| 159 | url_pattern='https://example.com/{id}/') |
| 160 | """ |
| 161 | posts = [] |
| 162 | with open(filepath, 'r', encoding='utf-8') as f: |
| 163 | reader = csv.DictReader(f) |
| 164 | posts = list(reader) |
| 165 | |
| 166 | return from_posts(posts, title=title, link=link, description=description, |
| 167 | url_pattern=url_pattern, **kwargs) |
| 168 | |
| 169 | |
| 170 | def from_json( |
| 171 | filepath: str, |
| 172 | title: str = "RSS Feed", |
| 173 | link: str = "", |
| 174 | description: str = "", |
| 175 | url_pattern: Optional[str] = None, |
| 176 | **kwargs |
| 177 | ) -> Feed: |
| 178 | """ |
| 179 | Generate RSS feed from JSON file. |
| 180 | |
| 181 | Args: |
| 182 | filepath: Path to JSON file (should contain array of objects) |
| 183 | title: Feed title |
| 184 | link: Feed URL |
| 185 | description: Feed description |
| 186 | url_pattern: URL pattern with {field} placeholders |
| 187 | **kwargs: Additional arguments passed to from_posts() |
| 188 | |
| 189 | Returns: |
| 190 | Populated Feed object |
| 191 | |
| 192 | Example: |
| 193 | feed = from_json('posts.json', |
| 194 | url_pattern='https://example.com/{id}/') |
| 195 | """ |
| 196 | with open(filepath, 'r', encoding='utf-8') as f: |
| 197 | posts = json.load(f) |
| 198 | |
| 199 | if not isinstance(posts, list): |
| 200 | raise ValueError("JSON file must contain an array of objects") |
| 201 | |
| 202 | return from_posts(posts, title=title, link=link, description=description, |
| 203 | url_pattern=url_pattern, **kwargs) |
| 204 | |
| 205 | |
| 206 | def from_directory( |
| 207 | directory: str, |
| 208 | pattern: str = "*.md", |
| 209 | extract_metadata: bool = True, |
| 210 | url_pattern: Optional[str] = None, |
| 211 | title: str = "RSS Feed", |
| 212 | link: str = "", |
| 213 | description: str = "", |
| 214 | **kwargs |
| 215 | ) -> Feed: |
| 216 | """ |
| 217 | Generate RSS feed from files in a directory. |
| 218 | |
| 219 | Args: |
| 220 | directory: Directory path |
| 221 | pattern: Glob pattern for files (e.g., '*.md', '*.txt') |
| 222 | extract_metadata: Try to extract YAML frontmatter from files |
| 223 | url_pattern: URL pattern with {filename}, {stem} placeholders |
| 224 | title: Feed title |
| 225 | link: Feed URL |
| 226 | description: Feed description |
| 227 | **kwargs: Additional arguments passed to from_posts() |
| 228 | |
| 229 | Returns: |
| 230 | Populated Feed object |
| 231 | |
| 232 | Example: |
| 233 | feed = from_directory('posts/', |
| 234 | pattern='*.md', |
| 235 | url_pattern='https://example.com/posts/{stem}/') |
| 236 | """ |
| 237 | posts = [] |
| 238 | dir_path = Path(directory) |
| 239 | |
| 240 | for file_path in sorted(dir_path.glob(pattern), reverse=True): |
| 241 | post = { |
| 242 | 'filename': file_path.name, |
| 243 | 'stem': file_path.stem, |
| 244 | } |
| 245 | |
| 246 | content = file_path.read_text(encoding='utf-8') |
| 247 | |
| 248 | # Extract YAML frontmatter if requested |
| 249 | if extract_metadata and content.startswith('---'): |
| 250 | parts = content.split('---', 2) |
| 251 | if len(parts) >= 3: |
| 252 | # Parse YAML frontmatter |
| 253 | try: |
| 254 | import yaml |
| 255 | metadata = yaml.safe_load(parts[1]) |
| 256 | if isinstance(metadata, dict): |
| 257 | post.update(metadata) |
| 258 | content = parts[2].strip() |
| 259 | except ImportError: |
| 260 | # Fallback: simple key: value parsing |
| 261 | for line in parts[1].strip().split('\n'): |
| 262 | if ':' in line: |
| 263 | key, value = line.split(':', 1) |
| 264 | post[key.strip()] = value.strip() |
| 265 | content = parts[2].strip() |
| 266 | |
| 267 | # Set content |
| 268 | post['content'] = content |
| 269 | post['title'] = post.get('title', file_path.stem.replace('-', ' ').title()) |
| 270 | |
| 271 | # Get file modification time as date if not in metadata |
| 272 | if 'date' not in post: |
| 273 | post['date'] = file_path.stat().st_mtime |
| 274 | |
| 275 | posts.append(post) |
| 276 | |
| 277 | return from_posts(posts, title=title, link=link, description=description, |
| 278 | url_pattern=url_pattern, **kwargs) |
| 279 | |
| 280 | |
| 281 | def generate( |
| 282 | source: str, |
| 283 | output: str, |
| 284 | title: str = "RSS Feed", |
| 285 | link: str = "", |
| 286 | description: str = "", |
| 287 | **kwargs |
| 288 | ): |
| 289 | """ |
| 290 | One-liner to generate RSS feed from file to file. |
| 291 | |
| 292 | Auto-detects source format from extension and generates feed. |
| 293 | |
| 294 | Args: |
| 295 | source: Input file path (.tsv, .csv, .json) |
| 296 | output: Output XML file path |
| 297 | title: Feed title |
| 298 | link: Feed URL |
| 299 | description: Feed description |
| 300 | **kwargs: Additional arguments |
| 301 | |
| 302 | Example: |
| 303 | generate('articles.tsv', 'feed.xml', title='My Blog') |
| 304 | """ |
| 305 | source_path = Path(source) |
| 306 | ext = source_path.suffix.lower() |
| 307 | |
| 308 | # Auto-detect source type |
| 309 | if ext == '.tsv': |
| 310 | feed = from_tsv(source, title=title, link=link, description=description, **kwargs) |
| 311 | elif ext == '.csv': |
| 312 | feed = from_csv(source, title=title, link=link, description=description, **kwargs) |
| 313 | elif ext == '.json': |
| 314 | feed = from_json(source, title=title, link=link, description=description, **kwargs) |
| 315 | else: |
| 316 | raise ValueError(f"Unsupported file format: {ext}") |
| 317 | |
| 318 | feed.write(output) |