rsrc/extra/docs/README.md
2026-10-26 21:51:18 +00:00

36 lines
1.6 KiB
Markdown

1.
dl helper
to current dir
` ./dl.py 'https://github.com/user/repo/two/blob/main/file.txt' `
to custom dir
` ./dl.py 'https://gitea.example.com/user/repo/two/src/branch/path/file.txt' ~/downloads `
```python3
cat > dl.py <<'py'
#!/usr/bin/env python3
import os,re,sys,urllib.request,urllib.error
r,g,y,c,z='\033[31m','\033[32m','\033[33m','\033[36m','\033[0m'
die=lambda m,n=1:(print(f'{r}error:{z} {m}',file=sys.stderr),sys.exit(n))
len(sys.argv)in(2,3)or die(f'usage: {sys.argv[0]} <url> [outdir]')
u=sys.argv[1];d=sys.argv[2]if len(sys.argv)>2 else'.';os.makedirs(d,exist_ok=True)
m=re.match(r'^https?://github\.com/([^/]+)/([^/]+)/blob/([^/]+)/(.*)$',u);u=f'https://raw.githubusercontent.com/{m[1]}/{m[2]}/{m[3]}/{m[4]}'if m else u
m=re.match(r'^(https?://[^/]+/.+)/(src|blob)/([^/]+)/(.*)$',u);u=f'{m[1]}/raw/{m[3]}/{m[4]}'if m else u
n=re.sub(r'_+','_',re.sub(r'[^a-zA-Z0-9._+-]+','_',((u.split('?',1)[0].split('#',1)[0].rstrip('/').split('/')[-1]or'downloaded_file').replace('%20','_').replace(' ','_')))).strip('._')or'downloaded_file'
p=os.path.join(d,n);b,e=os.path.splitext(n);i=1
while os.path.exists(p):p=os.path.join(d,f'{b}_{i}{e}');i+=1
try:
q=urllib.request.Request(u,headers={'User-Agent':'pyraw/1.0'})
with urllib.request.urlopen(q,timeout=20)as r1,open(p,'wb')as f:f.write(r1.read())
print(f'{g}saved:{z} {c}{p}{z}')
except urllib.error.HTTPError as x:die(f'http {x.code} for {y}{u}{z}',2)
except urllib.error.URLError as x:die(f'network failure: {x.reason}',3)
except PermissionError:die(f'cannot write to {y}{d}{z}',4)
except Exception as x:die(str(x),5)
py
chmod +x dl.py
```