Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created January 29, 2023 18:24
Show Gist options
  • Save elfsternberg/2008433a291dfd6d700f28d60b66431e to your computer and use it in GitHub Desktop.
Save elfsternberg/2008433a291dfd6d700f28d60b66431e to your computer and use it in GitHub Desktop.
Opens the 'jbak' (GDBM) file produced by the Dreamwidth backup tool, and converts each entry into a markdown file suitable for Zola or Hugo
from dbm import gnu
from collections import namedtuple
from markdownify import markdownify as md
from slugify import slugify
EVENT = b"event:event:"
elfs = gnu.open("./elfs.jbak", "r")
def key_extraction(key):
found = key.split(b":")
return ":".join([i.decode("utf-8") for i in found[0:2]])
def event_number(event):
found = event.split(b":")
return int(found[2].decode("utf-8"))
event_numbers = [
event_number(event)
for event in elfs.keys()
if event[0 : len(EVENT)] == EVENT
]
events = sorted(event_numbers)
# event:prop:2784:current_music
roots = [
b"event:allowmask",
b"event:anum",
b"event:event",
b"event:eventtime",
b"event:ids",
b"event:lastgrab",
b"event:lastsync",
b"event:prop",
b"event:proplist",
b"event:realtime",
b"event:security",
b"event:subject",
]
Post = namedtuple("Post", "subject body date music tags")
def extract_post(key):
bkey = bytes(str(key), "utf-8")
body = str(elfs[b"event:event:" + bkey], "utf-8")
date = str(elfs[b"event:eventtime:" + bkey], "utf-8")
subject = ""
try:
subject = str(elfs[b"event:subject:" + bkey], "utf-8")
except:
None
music = ""
tags = ""
try:
music = str(elfs[b"event:prop:" + bkey + b":current_music"], "utf-8")
except:
None
try:
tags = str(elfs[b"event:prop:" + bkey + b":taglist"], "utf-8")
except:
None
return Post(subject, body, date, music, tags)
for event in events:
post = extract_post(event)
day = post.date.split(" ")[0]
filename = "{}.md".format(day)
if post.subject:
filename = "{}-{}.md".format(day, slugify(post.subject))
subject = "(no title)"
if post.subject:
subject = post.subject
with open(filename, "w") as out:
out.write("+++\n")
out.write('title = "{}"\n'.format(subject))
out.write("date = {}Z\n".format(post.date.replace(" ", "T")))
if post.tags:
tags = [t.strip() for t in post.tags.split(",")]
out.write(
"tags = [{}]\n".format(
", ".join(['"{}"'.format(t) for t in tags])
)
)
if post.music:
out.write("[extra]\n")
out.write('current_music = "{}"\n'.format(post.music))
out.write("+++\n")
out.write(md(post.body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment