💾 Archived View for gemini.ctrl-c.club › ~michaeldark › aux-programs › src › DeedumBookmarkExport.py captured on 2024-09-29 at 03:37:02.

View Raw

More Information

-=-=-=-=-=-=-

import base64
import xml.etree.ElementTree as ET
import sys

# Parse xml passed as arg, extract bookmark string
try:
    fn = sys.argv[1]
    tree = ET.parse(fn)
    b64string = tree.findall('.//*[@name="flutter.bookmarks"]')[0].text
    if b64string == "":
        raise Exception()
except:
    print("Could not parse given xml file: %s" % fn)

# Note xml parsing removes '%#10;' delimiters
decodedBytes = base64.b64decode(b64string)

# Manually parse links from serialized list
i = 87                      # ASSUME: java prefix is fixed size
state = 'nextLink'
bookmarks = []

while state != 'done':
    # Iterate byte
    if i >= len(decodedBytes):
        state == 'done'     # cut off whatever else was being done
    else:
        byte = decodedBytes[i]
        i = i + 1
            
    # Advance state machine
    match state:
        case 'nextLink':
            if chr(byte) == 't':
                state = 'lenChar1'
            else:
                state = 'done'
        case 'lenChar1':
            linkLen = int(byte) * 256
            state = 'lenChar2'
        case 'lenChar2':
            linkLen = linkLen + int(byte)
            urlString = ""
            state = 'urlString'
        case 'urlString':
            urlString = urlString + chr(byte)
            linkLen = linkLen - 1
            if linkLen <= 0:
                bookmarks.append(urlString)
                state = 'nextLink'
        case _:
            state = 'done'

# Output our bookmarks
for b in bookmarks:
    print(b)