from subprocess import check_output
import re
from shutil import which


def getMaxConnPerServer():
	x = which('aria2c')

	if x is None:
		print('You have to add aria2c to your path first')
		exit(25)	
	res = check_output(['aria2c','--help=#basic']).decode()

	lines = res.split('\n')

	result = None

	found_max = 0
	for line in lines:
		#~ print([line])
		if 'max-connection-per-server' in line:
			found_max += 1
		if 'Possible Values' in line:
			if found_max == 2:
				result = line.strip()
				break
				
	if result is not None:
		x = re.fullmatch('Possible Values: 1-(\d+)', result)
		if x is not None:
			return x.group(1)
		
if __name__ == '__main__':
		max = getMaxConnPerServer()
		if max is not None:
			print(f'The maximum connections per server are: {max}')
		else:
			print('Analysing failed... contact dev...')