Created
September 13, 2015 20:17
-
-
Save schuhumi/fce924289aea23d88144 to your computer and use it in GitHub Desktop.
sort local refs of your ipfs by filetype
This file contains hidden or 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/env python3 | |
import os | |
import subprocess | |
refsdir = "./localrefs" | |
extensions = { "ASCII text":"txt", | |
"C source":"c", | |
"C++ source":"cpp", | |
"HTML document":"html", | |
"JPEG image data":"jpg", | |
"PDF document":"pdf", | |
"PNG image data":"png", | |
"SVG Scalable Vector Graphics image":"svg", | |
"UTF-8 Unicode text":"txt", | |
"WebM":"webm"} | |
localrefs = subprocess.check_output(["ipfs", "refs", "local"]).decode("utf-8").split() | |
print(len(localrefs), "local refs found") | |
if not os.path.exists(refsdir): | |
os.makedirs(refsdir) | |
ctr = 0 | |
for ref in localrefs: | |
ctr += 1 | |
print("Fetching ",ctr,"of",len(localrefs),"(",ref,")...") | |
try: | |
content = subprocess.check_output(["ipfs", "cat", ref]) | |
with open("./thisfile", "wb") as f: | |
f.write(content) | |
filetype = subprocess.check_output(["file", "-b", "./thisfile"]).decode("utf-8").split("/")[0].split(",")[0].strip() | |
print("\tFiletype is: ", filetype) | |
if len(filetype)<1: | |
filetype = "Unknown" | |
if not os.path.exists(refsdir+"/"+filetype): | |
os.makedirs(refsdir+"/"+filetype) | |
if filetype in extensions: | |
ext = extensions[filetype] | |
else: | |
ext = "" | |
os.rename("./thisfile", refsdir+"/"+filetype+"/"+ref+"."+ext) | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment