Skip to content

Instantly share code, notes, and snippets.

@lezuber
Last active May 31, 2023 06:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lezuber/aa034d82b3a21d7d96fcc73a9b906b25 to your computer and use it in GitHub Desktop.
Save lezuber/aa034d82b3a21d7d96fcc73a9b906b25 to your computer and use it in GitHub Desktop.
Download Logseq repository images to local asset folder
#!/usr/bin/env python
# coding: utf-8
# What is this?
# This script is for logseq webapp users that want to switch to the local version.
# It will download all images that are linked inside your graph and store them in the local "/assets/" folder.
# Remarks
# 1. Only supports Markdown files (for now)
# 2. Filnames are not preserved. The filenames will be a news random string. (Let me know if you need it otherwise.)
# 2. Make sure you close Logseq Desktop app and disconnect web app before starting!
# 3. Use at your own risk! Verify everything worked before permanently moving to the new local version!
# How to use
# 1. Download your logseq repository from Github to your local machine.
# 2. Run the script with "-f <Path to that repository>"
# 3. Reccomended if you know how: Check the uncommitted changes to the repository. This is an easy way to check if everything worked as intended.
# 3. Open that folder in the logseq client.
import glob
import re
import uuid
import urllib.request
from urllib.error import HTTPError
import shutil
from pathlib import Path
import argparse
regex_markdownFile = re.compile(r'!\[[^\]]*\]\(([^\)]*)\)') # markdown file syntax
def save_file_locally(logseq_dir, url):
try:
with urllib.request.urlopen(url) as r:
file_ending = "." + r.info().get_content_subtype()
filename = str(uuid.uuid4().hex) + str(uuid.uuid4().hex) + str(uuid.uuid4().hex) + file_ending
with open(logseq_dir+"assets/"+filename, 'wb') as out_file:
shutil.copyfileobj(r, out_file)
return "../assets/"+ filename
except HTTPError as err:
print(F"Error! Could not load file from {url}!")
print(err)
return False
except ValueError as err:
print(F"Error! Could not load file for regex match: {url}!")
print(err)
return False
def run_dl(logseq_dir):
for fPath in glob.glob(f"{logseq_dir}**/*.md", recursive=True):
matches = []
with open(fPath) as _file:
filedata = _file.read()
_file.seek(0)
for i, line in enumerate(_file.readlines()):
matches.extend(regex_markdownFile.findall(line))
for match in matches:
new_url = save_file_locally(logseq_dir=logseq_dir, url=match)
if new_url:
filedata = filedata.replace(match, new_url)
with open(fPath, 'w') as _file:
_file.write(filedata)
print(fPath)
if __name__ == "__main__":
# Program arguments.
parser = argparse.ArgumentParser(
description="Script to download the images of your git logseq graph to local asset folder."
)
parser.add_argument(
"-f",
type=str,
default=None,
metavar="<logseq graph>",
help="Absolute path to your local repository copy of your logseq graph.",
)
args = parser.parse_args()
if not args.f:
print("Please give me the absolute path to your local repository copy of your logseq graph with:")
print("-f <Path>")
logseq_dir = args.f
print(f"Starting download of files to: {logseq_dir}")
run_dl(logseq_dir)
@barlevalon
Copy link

Thanks for this! Helped me a ton. 🥇
Note that the path given by the user must have a trailing slash, otherwise nothing happens (because of line 53).

@ozanozyegen
Copy link

Thanks a lot. I've changed two things to make it work for me.
1- Replace line 30 to

regex_markdownFile = re.compile(r'!\[[^\]]*\]\((http.+)\)') # markdown file syntax

2- Add utf-8 to file open to overcome encoding errors. So run_dl becomes:

def run_dl(logseq_dir):
    for fPath in glob.glob(f"{logseq_dir}**/*.md", recursive=True):
        matches = []
        print(fPath)
        with open(fPath, encoding='utf-8') as _file:
            filedata = _file.read()
            _file.seek(0)
            for i, line in enumerate(_file.readlines()):
                matches.extend(regex_markdownFile.findall(line))
        for match in matches:
            new_url = save_file_locally(logseq_dir=logseq_dir, url=match)
            if new_url:
                filedata = filedata.replace(match, new_url)

        with open(fPath, 'w', encoding='utf-8') as _file:
            _file.write(filedata)
        print(fPath)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment