mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-06 14:19:59 -05:00
Merge bitcoin/bitcoin#24180: script: Handle request exception and check svg image in getcoins.py
7c7dbe4398
improved getcoins.py (sh15h4nk) Pull request description: Improvement to getcoins.py: - handled request excecptions - added limit handler to SVG size - restricted format - added caption to captcha (From the url) Fixes https://github.com/bitcoin/bitcoin/issues/24119 ACKs for top commit: laanwj: Tested ACK7c7dbe4398
Tree-SHA512: b0311b8d69c604db80b0802659c874760f7c2aa79c1f27637f8c03d0d95516d184514ff226e1ba3e1dcf506f5be4013a2d71e66369e03739a7add0e0ba80190c
This commit is contained in:
commit
3eec29ed3a
1 changed files with 18 additions and 16 deletions
|
@ -8,6 +8,7 @@ import io
|
||||||
import requests
|
import requests
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import xml.etree.ElementTree
|
||||||
|
|
||||||
DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim'
|
DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim'
|
||||||
DEFAULT_GLOBAL_CAPTCHA = 'https://signetfaucet.com/captcha'
|
DEFAULT_GLOBAL_CAPTCHA = 'https://signetfaucet.com/captcha'
|
||||||
|
@ -88,20 +89,17 @@ def bitcoin_cli(rpc_command_and_params):
|
||||||
try:
|
try:
|
||||||
return subprocess.check_output(argv).strip().decode()
|
return subprocess.check_output(argv).strip().decode()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('The binary', args.cmd, 'could not be found.')
|
raise SystemExit(f"The binary {args.cmd} could not be found")
|
||||||
exit(1)
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
cmdline = ' '.join(argv)
|
cmdline = ' '.join(argv)
|
||||||
print(f'-----\nError while calling "{cmdline}" (see output above).')
|
raise SystemExit(f"-----\nError while calling {cmdline} (see output above).")
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
|
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
|
||||||
# Get the hash of the block at height 1 of the currently active signet chain
|
# Get the hash of the block at height 1 of the currently active signet chain
|
||||||
curr_signet_hash = bitcoin_cli(['getblockhash', '1'])
|
curr_signet_hash = bitcoin_cli(['getblockhash', '1'])
|
||||||
if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
|
if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
|
||||||
print('The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet to use this functionality.\n')
|
raise SystemExit('The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet to use this functionality.\n')
|
||||||
exit(1)
|
|
||||||
else:
|
else:
|
||||||
# For custom faucets, don't request captcha by default.
|
# For custom faucets, don't request captcha by default.
|
||||||
if args.captcha == DEFAULT_GLOBAL_CAPTCHA:
|
if args.captcha == DEFAULT_GLOBAL_CAPTCHA:
|
||||||
|
@ -120,28 +118,32 @@ session = requests.Session()
|
||||||
if args.captcha != '': # Retrieve a captcha
|
if args.captcha != '': # Retrieve a captcha
|
||||||
try:
|
try:
|
||||||
res = session.get(args.captcha)
|
res = session.get(args.captcha)
|
||||||
except:
|
res.raise_for_status()
|
||||||
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
|
except requests.exceptions.RequestException as e:
|
||||||
exit(1)
|
raise SystemExit(f"Unexpected error when contacting faucet: {e}")
|
||||||
|
|
||||||
|
# Size limitation
|
||||||
|
svg = xml.etree.ElementTree.fromstring(res.content)
|
||||||
|
if svg.attrib.get('width') != '150' or svg.attrib.get('height') != '50':
|
||||||
|
raise SystemExit("Captcha size doesn't match expected dimensions 150x50")
|
||||||
|
|
||||||
# Convert SVG image to PPM, and load it
|
# Convert SVG image to PPM, and load it
|
||||||
try:
|
try:
|
||||||
rv = subprocess.run([args.imagemagick, '-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True)
|
rv = subprocess.run([args.imagemagick, 'svg:-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('The binary', args.imagemagick, 'could not be found. Please make sure ImageMagick (or a compatible fork) is installed and that the correct path is specified.')
|
raise SystemExit(f"The binary {args.imagemagick} could not be found. Please make sure ImageMagick (or a compatible fork) is installed and that the correct path is specified.")
|
||||||
exit(1)
|
|
||||||
img = PPMImage(io.BytesIO(rv.stdout))
|
img = PPMImage(io.BytesIO(rv.stdout))
|
||||||
|
|
||||||
# Terminal interaction
|
# Terminal interaction
|
||||||
print_image(img)
|
print_image(img)
|
||||||
print('Enter captcha: ', end='')
|
print(f"Captcha from URL {args.captcha}")
|
||||||
data['captcha'] = input()
|
data['captcha'] = input('Enter captcha: ')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = session.post(args.faucet, data=data)
|
res = session.post(args.faucet, data=data)
|
||||||
except:
|
except:
|
||||||
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
|
raise SystemExit(f"Unexpected error when contacting faucet: {sys.exc_info()[0]}")
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Display the output as per the returned status code
|
# Display the output as per the returned status code
|
||||||
if res:
|
if res:
|
||||||
|
|
Loading…
Add table
Reference in a new issue