Skip to content

Instantly share code, notes, and snippets.

@darkr4y
Last active December 22, 2023 16:07
Show Gist options
  • Save darkr4y/cc532b252f5587cc424051eb13c7981d to your computer and use it in GitHub Desktop.
Save darkr4y/cc532b252f5587cc424051eb13c7981d to your computer and use it in GitHub Desktop.
export obsidian plugins list
import requests
#### import httpx
import urllib
json_list = [
"https://github.com/replete/obsidian-minimal-theme-css-snippets/blob/main/%5Bui%5D%20Compact%20Tabs.css",
"https://github.com/efemkay/obsidian-modular-css-layout/blob/main/MCL%20Gallery%20Cards.css",
"https://github.com/efemkay/obsidian-modular-css-layout/blob/main/MCL%20Multi%20Column.css",
"https://github.com/efemkay/obsidian-modular-css-layout/blob/main/MCL%20Wide%20Views.css",
"https://github.com/SlRvb/Obsidian--ITS-Theme/blob/main/Snippets/S%20-%20Images%20Adjustments.css",
"https://github.com/kepano/flexoki-obsidian/blob/main/theme.css",
"https://github.com/damiankorcz/Prism-Theme/blob/main/src/scss/Editor/callouts.scss",
]
import os, sys
def get_current_script_path():
if "__file__" in globals():
script_path = os.path.abspath(__file__)
else:
script_path = os.path.abspath(sys.argv[0])
return script_path
def show_all_path():
print("current path: " + os.getcwd())
print("current file: " + get_current_script_path())
for path in sys.path:
print(path)
def validate_target_directory(target_directory):
if ".obsidian/snippets" not in target_directory:
raise ValueError("目标路径必须包含 .obsidian/snippets")
def replace_github_url(url):
return url.replace("github.com", "hub.fgit.cf")
def get_file_length(url):
with requests.head(url, allow_redirects=True) as response:
return int(response.headers.get("content-length", 0))
def download_file(url, target_directory):
if url.endswith(".css"):
url = urllib.parse.unquote(url)
print(f"正在载入 {0} ...", url)
with requests.get(url, allow_redirects=True) as response:
file_length = len(response.content)
file_path = os.path.join(target_directory, os.path.basename(url))
if os.path.exists(file_path):
with open(file_path, "rb") as f:
local_file_length = len(f.read())
if local_file_length != file_length:
with open(file_path, "wb") as f:
f.write(response.content)
print(url + " 更新成功")
else:
with open(file_path, "wb") as f:
f.write(response.content)
print(url + " 下载成功")
if __name__ == "__main__":
show_all_path()
target_directory = ".obsidian/snippets"
validate_target_directory(target_directory)
for url in json_list:
modified_url = replace_github_url(url)
file_length = get_file_length(modified_url)
download_file(modified_url, target_directory)
import os
import json
import argparse
from urllib.parse import urlparse
def extract_manifest_data(manifest_path):
if os.path.isfile(manifest_path):
with open(manifest_path, "r") as f:
manifest_data = json.load(f)
name = manifest_data["name"]
id = manifest_data["id"]
description = manifest_data["description"]
funding_url = ""
if "fundingUrl" in manifest_data:
funding_url = manifest_data["fundingUrl"]
author_url = ""
if "authorUrl" in manifest_data:
author_url = manifest_data["authorUrl"]
repository = "https://obsidian.md/plugins?id=" + id
author = ""
if "author" in manifest_data:
# safe_name = manifest_data['author'].strip().replace(" ", "").lower()
# repository = "https://github.com/" + safe_name + "/" + id
author = manifest_data["author"]
if "github.com" in author_url:
parsed_url = urlparse(author_url)
print(parsed_url)
splited = [x for x in parsed_url.path.split("/") if x]
print(splited)
if len(splited) > 1:
repository = author_url
else:
repository = author_url + "/" + id
return {
"name": name,
"id": id,
"description": description,
"author": author,
"repository": repository,
"authorurl": author_url,
}
return None
def find_manifest_files(dir_path):
manifest_files = []
for root, dirs, files in os.walk(dir_path):
if "plugins" in root:
for file in files:
if file == "manifest.json":
manifest_files.append(os.path.join(root, file))
return manifest_files
def write_to_json_file(data, output_dir):
with open(os.path.join(output_dir, "output.json"), "w") as f:
json.dump(data, f, indent=4)
def write_to_markdown_file(data, output_dir):
file_path = os.path.join(output_dir, "output.md")
with open(file_path, "w") as f:
for entry in data:
safe_name = (
entry["name"]
.replace("/", " ")
.replace("\\", " ")
.replace(":", " ")
.replace("*", " ")
.replace("?", " ")
.replace('"', " ")
.replace("<", " ")
.replace(">", " ")
.replace("|", " ")
)
# frontmatter = f"---\nname: {entry['name']}\nid: {entry['id']}\ndescription: {entry['description']}\nrepository: {entry['repository']}\nadded: 2023-02-01\nstatus: i\nmobile: \nrating: \nissues: \nusage: \nalternative: \nsettings: \n---\n"
frontmatter = f"\n\nname: {entry['name']}\nid: {entry['id']}\nauthor: {entry['author']}\ndescription: {entry['description']}\nrepository: {entry['repository']}\ncontact: {entry['authorurl']}\n\n\n\n---\n\n"
f.write(frontmatter)
def main(dir_path, output_dir, output_format):
data = []
manifest_files = find_manifest_files(dir_path)
for manifest_path in manifest_files:
manifest_data = extract_manifest_data(manifest_path)
if manifest_data:
data.append(manifest_data)
if not output_dir:
output_dir = os.path.dirname(__file__)
if output_format == "json":
write_to_json_file(data, output_dir)
else:
write_to_markdown_file(data, output_dir)
print(f"plugin count: {len(data)} :)")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument(
"dir_path",
type=str,
help="the directory where the manifest.json files are located",
)
parser.add_argument(
"--output_dir", type=str, help="the directory to store the output files"
)
parser.add_argument(
"--json", action="store_true", help="output data in JSON format"
)
args = parser.parse_args()
output_format = "json" if args.json else "md"
main(args.dir_path, args.output_dir, output_format)
import os
import json
import re
def get_value_from_json(file_path, key):
"""
Read JSON file and get value for a specific key
"""
with open(file_path, 'r') as json_file:
data = json.load(json_file)
return data.get(key, None)
def process_plugins(plugins, plugin_type, enabled_plugins, disabled_plugins):
"""
Process the list of plugins and categorize them as enabled or disabled
"""
for plugin_name, plugin_id in plugins:
plugin_name += f" ({plugin_type})"
if plugin_id in community_plugins_list:
enabled_plugins.append(plugin_name)
else:
disabled_plugins.append(plugin_name)
def print_plugins_info(title, plugins):
"""
Print the list of plugins with a specific title
"""
print(title)
plugins.sort()
for name in plugins:
name = re.sub(r'[\/\\<>"|?*]', '-', name)
print(f"- [[{name}]]: ")
community_plugin_folder_path = os.path.join(@vault_path, ".obsidian/plugins")
core_plugins_file_path = os.path.join(@vault_path, ".obsidian/core-plugins-migration.json")
community_plugins_file_path = os.path.join(@vault_path, ".obsidian/community-plugins.json")
plugin_folders = [name for name in os.listdir(community_plugin_folder_path) if os.path.isdir(os.path.join(community_plugin_folder_path, name))]
with open(core_plugins_file_path, 'r') as f:
core_plugins_dict = json.load(f)
with open(community_plugins_file_path, 'r') as f:
community_plugins_list = json.load(f)
key_name = 'name'
key_id = 'id'
community_plugin_name_list = [(get_value_from_json(os.path.join(community_plugin_folder_path, plugin, "manifest.json"), key_name),
get_value_from_json(os.path.join(community_plugin_folder_path, plugin, "manifest.json"), key_id)) for plugin in plugin_folders]
enabled_plugins = list()
disabled_plugins = list()
for key, value in core_plugins_dict.items():
key += " (core)"
if value:
enabled_plugins.append(key)
else:
disabled_plugins.append(key)
process_plugins(community_plugin_name_list, "community", enabled_plugins, disabled_plugins)
print_plugins_info("Enabled Plugins", enabled_plugins)
print_plugins_info("Disabled Plugins", disabled_plugins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment