Created
July 3, 2021 16:23
-
-
Save jubnzv/8d4112afe263eca377792426a2197fe4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import tempfile | |
import subprocess | |
import requests | |
import sys | |
import re | |
import os | |
from typing import List | |
def get_links(filepath) -> List[str]: | |
links = [] | |
with open(filepath, 'r') as f: | |
link_re = re.compile(r'\[.*\]\((.*)\)') | |
for line in f: | |
m = re.search(link_re, line) | |
if m: | |
uri = m.groups()[0] | |
if "github.com" in uri: | |
links.append(uri) | |
return links | |
def download_links(filepath, source_dir): | |
links = get_links(filepath) | |
if not links: | |
return | |
try: | |
os.makedirs(source_dir) | |
except FileExistsError: | |
pass | |
os.chdir(source_dir) | |
for link in links: | |
subprocess.run(["git", "clone", link]) | |
def download_uri(uri): | |
r = requests.get(uri, allow_redirects=True) | |
f = tempfile.NamedTemporaryFile(delete=False) | |
f.write(r.content) | |
f.close() | |
return f.name | |
def download(md, source_dir): | |
if md.startswith("http"): | |
filepath = download_uri(md) | |
else: | |
filepath = md | |
return download_links(os.path.expanduser(filepath), | |
os.path.expanduser(source_dir)) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print(f"Usage: {sys.argv[0]} <markdown file> <destination directory>") | |
print("Example:") | |
print(f" {sys.argv[0]} 'https://raw.githubusercontent.com/rockerBOO/awesome-neovim/main/README.md' ~/Sources/") | |
sys.exit(1) | |
download(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment