Compare commits

..

No commits in common. "18c7f756ae7d6bb6e76754a416eb03646f3d3933" and "564f366ae2756296befba54a02ec130a3db11a80" have entirely different histories.

3 changed files with 9 additions and 29 deletions

View File

@ -1,5 +0,0 @@
# 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 MediaPost():
class GlossatoryPost():
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 MediaPost(year, month, day, file, alt)
return GlossatoryPost(year, month, day, file, alt)
def ensure_dir(gmdir):
@ -95,26 +95,17 @@ def ensure_dir(gmdir):
gmdir.mkdir(parents=True)
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):
def write_gemfile(gmdir, title, items):
ensure_dir(gmdir)
gmi = gmdir / "index.gmi"
with open(gmi, "w") as gfh:
if colophon:
gfh.write(colophon)
gfh.write("\n\n")
gfh.write(HEADER)
gfh.write(f"# {title}\n\n")
for link, text in items:
gfh.write(f"=> {link} {text}\n")
def apub2gmi(archive, output, colophon):
def apub2gmi(archive, output):
with open(f"{archive}/outbox.json", "r") as fh:
js = json.load(fh)
posts = {}
@ -136,12 +127,12 @@ def apub2gmi(archive, output, colophon):
print(f"Processing failed: {i}: {e}")
years = [ ( f"{year}/", year ) for year in posts ]
write_gemfile(Path(output), colophon, "Glossatory", years)
write_gemfile(Path(output), "Glossatory", years)
for year in posts:
ydir = Path(output) / year
months = [ ( f"{month}/", MNAMES[month] ) for month in posts[year] ]
write_gemfile(ydir, colophon, year, months)
write_gemfile(ydir, year, months)
for month in posts[year]:
mname = MNAMES[month]
mdir = ydir / month
@ -157,7 +148,7 @@ def apub2gmi(archive, output, colophon):
for day in posts[year][month]:
for post in posts[year][month][day]:
links.append((f"{day}/{post.fname}", post.title))
write_gemfile(mdir, colophon, f"{mname} {year}", links)
write_gemfile(mdir, f"{mname} {year}", links)
if __name__ == "__main__":
@ -168,10 +159,5 @@ 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()
colophon = load_colophon(args.colophon)
apub2gmi(args.archive, args.output, colophon)
apub2gmi(args.archive, args.output)

View File

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