Compare commits
No commits in common. "18c7f756ae7d6bb6e76754a416eb03646f3d3933" and "564f366ae2756296befba54a02ec130a3db11a80" have entirely different histories.
18c7f756ae
...
564f366ae2
|
@ -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.
|
|
32
apub2gmi.py
32
apub2gmi.py
|
@ -46,7 +46,7 @@ NAME_RES = [
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MediaPost():
|
class GlossatoryPost():
|
||||||
def __init__(self, year, month, day, file, title):
|
def __init__(self, year, month, day, file, title):
|
||||||
self.year = year
|
self.year = year
|
||||||
self.month = month
|
self.month = month
|
||||||
|
@ -87,7 +87,7 @@ def process_post(archive, obj):
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Couldn't match url {url}")
|
raise ValueError(f"Couldn't match url {url}")
|
||||||
alt = attachment["name"]
|
alt = attachment["name"]
|
||||||
return MediaPost(year, month, day, file, alt)
|
return GlossatoryPost(year, month, day, file, alt)
|
||||||
|
|
||||||
|
|
||||||
def ensure_dir(gmdir):
|
def ensure_dir(gmdir):
|
||||||
|
@ -95,26 +95,17 @@ def ensure_dir(gmdir):
|
||||||
gmdir.mkdir(parents=True)
|
gmdir.mkdir(parents=True)
|
||||||
|
|
||||||
|
|
||||||
def load_colophon(cfile):
|
def write_gemfile(gmdir, title, items):
|
||||||
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)
|
ensure_dir(gmdir)
|
||||||
gmi = gmdir / "index.gmi"
|
gmi = gmdir / "index.gmi"
|
||||||
with open(gmi, "w") as gfh:
|
with open(gmi, "w") as gfh:
|
||||||
if colophon:
|
gfh.write(HEADER)
|
||||||
gfh.write(colophon)
|
|
||||||
gfh.write("\n\n")
|
|
||||||
gfh.write(f"# {title}\n\n")
|
gfh.write(f"# {title}\n\n")
|
||||||
for link, text in items:
|
for link, text in items:
|
||||||
gfh.write(f"=> {link} {text}\n")
|
gfh.write(f"=> {link} {text}\n")
|
||||||
|
|
||||||
|
|
||||||
def apub2gmi(archive, output, colophon):
|
def apub2gmi(archive, output):
|
||||||
with open(f"{archive}/outbox.json", "r") as fh:
|
with open(f"{archive}/outbox.json", "r") as fh:
|
||||||
js = json.load(fh)
|
js = json.load(fh)
|
||||||
posts = {}
|
posts = {}
|
||||||
|
@ -136,12 +127,12 @@ def apub2gmi(archive, output, colophon):
|
||||||
print(f"Processing failed: {i}: {e}")
|
print(f"Processing failed: {i}: {e}")
|
||||||
|
|
||||||
years = [ ( f"{year}/", year ) for year in posts ]
|
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:
|
for year in posts:
|
||||||
ydir = Path(output) / year
|
ydir = Path(output) / year
|
||||||
months = [ ( f"{month}/", MNAMES[month] ) for month in posts[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]:
|
for month in posts[year]:
|
||||||
mname = MNAMES[month]
|
mname = MNAMES[month]
|
||||||
mdir = ydir / month
|
mdir = ydir / month
|
||||||
|
@ -157,7 +148,7 @@ def apub2gmi(archive, output, colophon):
|
||||||
for day in posts[year][month]:
|
for day in posts[year][month]:
|
||||||
for post in posts[year][month][day]:
|
for post in posts[year][month][day]:
|
||||||
links.append((f"{day}/{post.fname}", post.title))
|
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__":
|
if __name__ == "__main__":
|
||||||
|
@ -168,10 +159,5 @@ if __name__ == "__main__":
|
||||||
ap.add_argument(
|
ap.add_argument(
|
||||||
'-o', '--output', required=True, type=str, help="Output directory"
|
'-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()
|
args = ap.parse_args()
|
||||||
colophon = load_colophon(args.colophon)
|
apub2gmi(args.archive, args.output)
|
||||||
apub2gmi(args.archive, args.output, colophon)
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
This is a sample of some text which can be added at the top of every gemfile in the archive.
|
|
Loading…
Reference in New Issue