├── README.md
└── atomfeed.py
/README.md:
--------------------------------------------------------------------------------
1 | # atomfeed
2 | A single file atom feed library with no dependencies
3 |
4 | Based on code from gtlsgamr/htxyz. (In particular this function.)
5 |
6 | Demo: The feed on www.gibney.org
7 |
8 | License: GPL v3.0
9 |
10 | Example usage:
11 |
12 | ```python
13 | import atomfeed
14 |
15 | data = {
16 | "title" : "A wonderful feed",
17 | "id" : "https://www.example.com",
18 | "link_web" : "https://www.example.com",
19 | "link_feed" : "https://www.example.com/atom.xml",
20 | "subtitle" : "Everything about feeds and stuff",
21 | "updated" : "2023-01-07",
22 | "author_name": "Mr. Example",
23 | "author_mail": "example@example.com",
24 | }
25 |
26 | data["entries"] = [{
27 | "title" : "Hello World",
28 | "date" : "2022-01-08",
29 | "updated": "2022-01-08",
30 | "link" : "https://www.example.com/hello_world",
31 | "summary": "A greeting to the world",
32 | "content": "Bonjour!",
33 | }]
34 |
35 | feed = atomfeed.generate(data)
36 | ```
37 |
--------------------------------------------------------------------------------
/atomfeed.py:
--------------------------------------------------------------------------------
1 | from datetime import datetime
2 |
3 | atom_template = """
4 |
5 |
6 | {title}
7 | {id}
8 | {subtitle}
9 |
10 |
11 | {updated}
12 |
13 | {author_name}
14 | {author_mail}
15 |
16 | {entries}
17 |
18 | """
19 |
20 | entry_template = """
21 |
22 | {title}
23 |
24 | {link}
25 | {updated}
26 | {summary}
27 |
28 | {content}
29 |
30 |
31 |
32 | """
33 |
34 | def template_dict(template, data):
35 | r = template
36 | for tag in data.keys():
37 | r = r.replace('{'+tag+'}',data[tag])
38 | return r
39 |
40 | def generate(data):
41 | feed = ""
42 | most_recent = "0000-00-00"
43 | for entry in data["entries"]:
44 | most_recent = max(most_recent, entry["updated"])
45 | feed += template_dict(entry_template, entry)
46 | data["entries"] = feed;
47 | # If no feed update time was provided, use the one of the most recent entry:
48 | if not data["updated"]: data["updated"] = most_recent
49 | return template_dict(atom_template, data)
50 |
--------------------------------------------------------------------------------