68 lines
1.7 KiB
Python
Executable File
68 lines
1.7 KiB
Python
Executable File
from pathlib import Path
|
|
import subprocess
|
|
import urllib.request
|
|
|
|
|
|
name = 'ipfs'
|
|
help_text = 'Upload the web to IPFS'
|
|
|
|
|
|
def options(parser):
|
|
parser.add_argument(
|
|
'song_folder', type=Path,
|
|
help="Folder with the song database.")
|
|
parser.add_argument(
|
|
'-a', '--api', default='http://localhost:5001',
|
|
help="IPFS API server, defaults to http://localhost:5001.")
|
|
parser.add_argument(
|
|
'-p', '--pinfile', default='~/.negromate/ipfs.hash', type=Path,
|
|
help="file to store the current ipfs hash, defaults to ~/.negromate/ipfs.")
|
|
|
|
|
|
def run(args):
|
|
# add to local
|
|
command = [
|
|
"ipfs",
|
|
"add",
|
|
"--recursive",
|
|
"--quieter",
|
|
args.song_folder,
|
|
]
|
|
final_hash = subprocess.check_output(command).decode('utf-8').strip()
|
|
|
|
# pin in server
|
|
url = "{}/api/v0/pin/add?arg={}&progress=false".format(
|
|
args.api,
|
|
final_hash,
|
|
)
|
|
urllib.request.urlopen(url)
|
|
|
|
# read previous hash and update value
|
|
pinfile = args.pinfile.expanduser()
|
|
if pinfile.exists():
|
|
with pinfile.open() as f:
|
|
previous_hash = f.read()
|
|
else:
|
|
if not pinfile.parent.exists():
|
|
pinfile.parent.mkdir()
|
|
previous_hash = None
|
|
with pinfile.open('w') as f:
|
|
f.write(final_hash)
|
|
|
|
if previous_hash is not None:
|
|
# remove previous pin on local
|
|
command = [
|
|
'ipfs',
|
|
'pin',
|
|
'rm',
|
|
previous_hash,
|
|
]
|
|
subprocess.check_call(command)
|
|
|
|
# remove previous pin on server
|
|
url = "{}/api/v0/pin/rm?arg={}".format(
|
|
args.api,
|
|
previous_hash,
|
|
)
|
|
urllib.request.urlopen(url)
|