Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Lynch 18c7f756ae Added a README.md 2024-05-04 14:19:04 +10:00
Mike Lynch 2e098f7909 Made the colophon on each index page be a file and provided a sample. 2024-05-04 14:14:36 +10:00
3 changed files with 29 additions and 9 deletions

5
README.md 100644
View File

@ -0,0 +1,5 @@
# apub2gmi.py
This is a script which takes an archive exported from a Mastodon account, looks for media attachments and uses them to build an archive for a Gemini server.
I use it to update the [Glossatory archives](gemini://gemini.mikelynch.org/glossatory/) and there's still a few things hard-coded in it which I haven't moved out to a config file, specifically the regular expressions which match attachment URLs and pull bits of text out for the index links.

View File

@ -46,7 +46,7 @@ NAME_RES = [
class GlossatoryPost():
class MediaPost():
def __init__(self, year, month, day, file, title):
self.year = year
self.month = month
@ -87,7 +87,7 @@ def process_post(archive, obj):
else:
raise ValueError(f"Couldn't match url {url}")
alt = attachment["name"]
return GlossatoryPost(year, month, day, file, alt)
return MediaPost(year, month, day, file, alt)
def ensure_dir(gmdir):
@ -95,17 +95,26 @@ def ensure_dir(gmdir):
gmdir.mkdir(parents=True)
def write_gemfile(gmdir, title, items):
def load_colophon(cfile):
if cfile:
with open(cfile, "r") as cfh:
colophon = cfh.readlines()
return "".join(colophon)
return None
def write_gemfile(gmdir, colophon, title, items):
ensure_dir(gmdir)
gmi = gmdir / "index.gmi"
with open(gmi, "w") as gfh:
gfh.write(HEADER)
if colophon:
gfh.write(colophon)
gfh.write("\n\n")
gfh.write(f"# {title}\n\n")
for link, text in items:
gfh.write(f"=> {link} {text}\n")
def apub2gmi(archive, output):
def apub2gmi(archive, output, colophon):
with open(f"{archive}/outbox.json", "r") as fh:
js = json.load(fh)
posts = {}
@ -127,12 +136,12 @@ def apub2gmi(archive, output):
print(f"Processing failed: {i}: {e}")
years = [ ( f"{year}/", year ) for year in posts ]
write_gemfile(Path(output), "Glossatory", years)
write_gemfile(Path(output), colophon, "Glossatory", years)
for year in posts:
ydir = Path(output) / year
months = [ ( f"{month}/", MNAMES[month] ) for month in posts[year] ]
write_gemfile(ydir, year, months)
write_gemfile(ydir, colophon, year, months)
for month in posts[year]:
mname = MNAMES[month]
mdir = ydir / month
@ -148,7 +157,7 @@ def apub2gmi(archive, output):
for day in posts[year][month]:
for post in posts[year][month][day]:
links.append((f"{day}/{post.fname}", post.title))
write_gemfile(mdir, f"{mname} {year}", links)
write_gemfile(mdir, colophon, f"{mname} {year}", links)
if __name__ == "__main__":
@ -159,5 +168,10 @@ if __name__ == "__main__":
ap.add_argument(
'-o', '--output', required=True, type=str, help="Output directory"
)
ap.add_argument(
'-c', '--colophon', required=False, type=str,
help="File with text to be included at the top of each index page"
)
args = ap.parse_args()
apub2gmi(args.archive, args.output)
colophon = load_colophon(args.colophon)
apub2gmi(args.archive, args.output, colophon)

1
colophon.gmi 100644
View File

@ -0,0 +1 @@
This is a sample of some text which can be added at the top of every gemfile in the archive.