Skip to content

Commit 7c7dbe4

Browse files
committed
improved getcoins.py
1 parent e3699b7 commit 7c7dbe4

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

contrib/signet/getcoins.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import requests
99
import subprocess
1010
import sys
11+
import xml.etree.ElementTree
1112

1213
DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim'
1314
DEFAULT_GLOBAL_CAPTCHA = 'https://signetfaucet.com/captcha'
@@ -88,20 +89,17 @@ def bitcoin_cli(rpc_command_and_params):
8889
try:
8990
return subprocess.check_output(argv).strip().decode()
9091
except FileNotFoundError:
91-
print('The binary', args.cmd, 'could not be found.')
92-
exit(1)
92+
raise SystemExit(f"The binary {args.cmd} could not be found")
9393
except subprocess.CalledProcessError:
9494
cmdline = ' '.join(argv)
95-
print(f'-----\nError while calling "{cmdline}" (see output above).')
96-
exit(1)
95+
raise SystemExit(f"-----\nError while calling {cmdline} (see output above).")
9796

9897

9998
if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET:
10099
# Get the hash of the block at height 1 of the currently active signet chain
101100
curr_signet_hash = bitcoin_cli(['getblockhash', '1'])
102101
if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
103-
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')
104-
exit(1)
102+
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')
105103
else:
106104
# For custom faucets, don't request captcha by default.
107105
if args.captcha == DEFAULT_GLOBAL_CAPTCHA:
@@ -120,28 +118,32 @@ def bitcoin_cli(rpc_command_and_params):
120118
if args.captcha != '': # Retrieve a captcha
121119
try:
122120
res = session.get(args.captcha)
123-
except:
124-
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
125-
exit(1)
121+
res.raise_for_status()
122+
except requests.exceptions.RequestException as e:
123+
raise SystemExit(f"Unexpected error when contacting faucet: {e}")
124+
125+
# Size limitation
126+
svg = xml.etree.ElementTree.fromstring(res.content)
127+
if svg.attrib.get('width') != '150' or svg.attrib.get('height') != '50':
128+
raise SystemExit("Captcha size doesn't match expected dimensions 150x50")
126129

127130
# Convert SVG image to PPM, and load it
128131
try:
129-
rv = subprocess.run([args.imagemagick, '-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True)
132+
rv = subprocess.run([args.imagemagick, 'svg:-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True)
130133
except FileNotFoundError:
131-
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.')
132-
exit(1)
134+
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.")
135+
133136
img = PPMImage(io.BytesIO(rv.stdout))
134137

135138
# Terminal interaction
136139
print_image(img)
137-
print('Enter captcha: ', end='')
138-
data['captcha'] = input()
140+
print(f"Captcha from URL {args.captcha}")
141+
data['captcha'] = input('Enter captcha: ')
139142

140143
try:
141144
res = session.post(args.faucet, data=data)
142145
except:
143-
print('Unexpected error when contacting faucet:', sys.exc_info()[0])
144-
exit(1)
146+
raise SystemExit(f"Unexpected error when contacting faucet: {sys.exc_info()[0]}")
145147

146148
# Display the output as per the returned status code
147149
if res:

0 commit comments

Comments
 (0)