Skip to content

Instantly share code, notes, and snippets.

@kusano
Created August 22, 2024 18:08
Show Gist options
  • Save kusano/8f4c836474e15be764be6ff3ecdb976f to your computer and use it in GitHub Desktop.
Save kusano/8f4c836474e15be764be6ff3ecdb976f to your computer and use it in GitHub Desktop.
# Slackのアーカイブ中のトークンの付いたURLをダウンロードする。
# python3 slack_download.py 'xxx Slack export mmm dd yyyy - mmm dd yyyy/' out/ 'xoxe-...'
import sys
import os
import json
import urllib.parse
import urllib.request
indir = sys.argv[1]
outdir = sys.argv[2]
token = sys.argv[3]
def download(data):
if type(data)==list:
for d in data:
download(d)
if type(data)==dict:
for d in data.values():
download(d)
if type(data)==str:
if data.startswith("https://") and token in data:
url = data
print(url)
path = [outdir]+urllib.parse.urlparse(data).path.split("/")
os.makedirs(os.path.join(*path[:-1]), exist_ok=True)
urllib.request.urlretrieve(url, os.path.join(*path))
for dirpath, dirnames, filenames in os.walk(indir):
for filename in filenames:
path = os.path.join(dirpath, filename)
_, ext = os.path.splitext(path)
if ext.lower()==".json":
with open(path) as file:
data = json.load(file)
download(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment