complete ipfs command
This commit is contained in:
		
							parent
							
								
									a35abdf37e
								
							
						
					
					
						commit
						50a627c65a
					
				| 
						 | 
					@ -1,13 +1,17 @@
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
 | 
					import getpass
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
import urllib.request
 | 
					import urllib.request
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from negromate.songs import logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
name = 'ipfs'
 | 
					name = 'ipfs'
 | 
				
			||||||
help_text = 'Upload the web to IPFS'
 | 
					help_text = 'Upload the web to IPFS'
 | 
				
			||||||
initial_config = {
 | 
					initial_config = {
 | 
				
			||||||
    'api': 'http://negromate.rocks:5001',
 | 
					    'api': 'http://ipfs.negromate.rocks',
 | 
				
			||||||
    'pinfile': '~/.negromate/ipfs.hash',
 | 
					    'pinfile': '~/.negromate/ipfs.hash',
 | 
				
			||||||
 | 
					    'realm': 'IPFS Gitea Negromate',
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +24,9 @@ def options(parser, config, **kwargs):
 | 
				
			||||||
    parser.add_argument(
 | 
					    parser.add_argument(
 | 
				
			||||||
        '-a', '--api', default=config[name]['api'],
 | 
					        '-a', '--api', default=config[name]['api'],
 | 
				
			||||||
        help="IPFS API server, defaults to {}.".format(config[name]['api']))
 | 
					        help="IPFS API server, defaults to {}.".format(config[name]['api']))
 | 
				
			||||||
 | 
					    parser.add_argument(
 | 
				
			||||||
 | 
					        '-r', '--realm', default=config[name]['realm'],
 | 
				
			||||||
 | 
					        help="IPFS API basic authentication realm, defaults to {}.".format(config[name]['realm']))
 | 
				
			||||||
    parser.add_argument(
 | 
					    parser.add_argument(
 | 
				
			||||||
        '-p', '--pinfile', default=config[name]['pinfile'], type=Path,
 | 
					        '-p', '--pinfile', default=config[name]['pinfile'], type=Path,
 | 
				
			||||||
        help="file to store the current ipfs hash, defaults to {}".format(
 | 
					        help="file to store the current ipfs hash, defaults to {}".format(
 | 
				
			||||||
| 
						 | 
					@ -27,43 +34,63 @@ def options(parser, config, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def run(args, **kwargs):
 | 
					def run(args, **kwargs):
 | 
				
			||||||
 | 
					    # Setup HTTP Basic authentication
 | 
				
			||||||
 | 
					    user = input('Username: ')
 | 
				
			||||||
 | 
					    password = getpass.getpass('Password:')
 | 
				
			||||||
 | 
					    auth_handler = urllib.request.HTTPBasicAuthHandler()
 | 
				
			||||||
 | 
					    auth_handler.add_password(
 | 
				
			||||||
 | 
					        realm=args.realm,
 | 
				
			||||||
 | 
					        uri=args.api,
 | 
				
			||||||
 | 
					        user=user,
 | 
				
			||||||
 | 
					        passwd=password)
 | 
				
			||||||
 | 
					    opener = urllib.request.build_opener(auth_handler)
 | 
				
			||||||
 | 
					    urllib.request.install_opener(opener)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # add to local
 | 
					    # add to local
 | 
				
			||||||
    command = [
 | 
					    command = [
 | 
				
			||||||
        "ipfs",
 | 
					        "ipfs",
 | 
				
			||||||
        "add",
 | 
					        "add",
 | 
				
			||||||
        "--recursive",
 | 
					        "--recursive",
 | 
				
			||||||
        "--quieter",
 | 
					        "--quieter",
 | 
				
			||||||
        args.song_folder,
 | 
					        args.song_folder.expanduser(),
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
    final_hash = subprocess.check_output(command).decode('utf-8').strip()
 | 
					    new_hash = subprocess.check_output(command).decode('utf-8').strip()
 | 
				
			||||||
 | 
					    logger.info('New hash: {}'.format(new_hash))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # pin in server
 | 
					    # pin in server
 | 
				
			||||||
    url = "{}/api/v0/pin/add?arg={}&progress=false".format(
 | 
					    data = urllib.parse.urlencode({
 | 
				
			||||||
        args.api,
 | 
					        'arg': new_hash,
 | 
				
			||||||
        final_hash,
 | 
					        'progress': 'false',
 | 
				
			||||||
    )
 | 
					    })
 | 
				
			||||||
    urllib.request.urlopen(url)
 | 
					    url = '{}/api/v0/pin/add?{}'.format(args.api, data)
 | 
				
			||||||
 | 
					    logger.debug("server pin request: {}".format(url))
 | 
				
			||||||
 | 
					    request = urllib.request.Request(url, method='POST')
 | 
				
			||||||
 | 
					    urllib.request.urlopen(request)
 | 
				
			||||||
 | 
					    logger.info('Hash pinned on server.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # update ipns on server
 | 
					    # update ipns on server
 | 
				
			||||||
    url = "{}/api/v0/name/publish?arg={}&resolve=yes".format(
 | 
					    data = urllib.parse.urlencode({
 | 
				
			||||||
        args.api,
 | 
					        'arg': new_hash,
 | 
				
			||||||
        final_hash,
 | 
					        'resolve': 'true',
 | 
				
			||||||
    )
 | 
					    })
 | 
				
			||||||
    urllib.request.urlopen(url)
 | 
					    url = "{}/api/v0/name/publish?{}".format(args.api, data)
 | 
				
			||||||
 | 
					    logger.debug("server ipns request: {}".format(url))
 | 
				
			||||||
 | 
					    request = urllib.request.Request(url, method='POST')
 | 
				
			||||||
 | 
					    urllib.request.urlopen(request)
 | 
				
			||||||
 | 
					    logger.info('IPNS name updated.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # read previous hash and update value
 | 
					    # read previous hash and update value
 | 
				
			||||||
    pinfile = args.pinfile.expanduser()
 | 
					    pinfile = args.pinfile.expanduser()
 | 
				
			||||||
    if pinfile.exists():
 | 
					    if pinfile.exists():
 | 
				
			||||||
        with pinfile.open() as f:
 | 
					        with pinfile.open() as f:
 | 
				
			||||||
            previous_hash = f.read()
 | 
					            previous_hash = f.read()
 | 
				
			||||||
 | 
					        logger.info('Previous hash: {}'.format(previous_hash))
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        if not pinfile.parent.exists():
 | 
					        if not pinfile.parent.exists():
 | 
				
			||||||
            pinfile.parent.mkdir()
 | 
					            pinfile.parent.mkdir()
 | 
				
			||||||
        previous_hash = None
 | 
					        previous_hash = None
 | 
				
			||||||
    with pinfile.open('w') as f:
 | 
					 | 
				
			||||||
        f.write(final_hash)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if previous_hash is not None:
 | 
					    if previous_hash is not None and previous_hash != new_hash:
 | 
				
			||||||
        # remove previous pin on local
 | 
					        # remove previous pin on local
 | 
				
			||||||
        command = [
 | 
					        command = [
 | 
				
			||||||
            'ipfs',
 | 
					            'ipfs',
 | 
				
			||||||
| 
						 | 
					@ -72,10 +99,17 @@ def run(args, **kwargs):
 | 
				
			||||||
            previous_hash,
 | 
					            previous_hash,
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        subprocess.check_call(command)
 | 
					        subprocess.check_call(command)
 | 
				
			||||||
 | 
					        logger.info('Previous hash unpinned on local')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # remove previous pin on server
 | 
					        # remove previous pin on server
 | 
				
			||||||
        url = "{}/api/v0/pin/rm?arg={}".format(
 | 
					        data = urllib.parse.urlencode({
 | 
				
			||||||
            args.api,
 | 
					            'arg': previous_hash,
 | 
				
			||||||
            previous_hash,
 | 
					        })
 | 
				
			||||||
        )
 | 
					        url = "{}/api/v0/pin/rm?{}".format(args.api, data)
 | 
				
			||||||
        urllib.request.urlopen(url)
 | 
					        logger.debug("server unpin request: {}".format(url))
 | 
				
			||||||
 | 
					        request = urllib.request.Request(url, method='POST')
 | 
				
			||||||
 | 
					        urllib.request.urlopen(request)
 | 
				
			||||||
 | 
					        logger.info('Previous hash unpinned on server')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with pinfile.open('w') as f:
 | 
				
			||||||
 | 
					        f.write(new_hash)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue