import subprocess
import shlex
import sys
import re


def run_command(command):
	file  = open('status.txt','w', encoding='utf-8')
	file.close()
	filename = None
	process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)
	while True:
		output = process.stdout.readline()
		
		if output == '' and process.poll() is not None:
			break
		if output:
			file  = open('status.txt','a', encoding='utf-8')
			file.write(output)
			m = re.search('/var/www/virtual/themanha/html/(.+)', output)
			if m:
				filename = list(m.groups())[0]
			file.close()
	rc = process.poll()
	return rc, filename
	
#~ file  = open('status.txt','w', encoding='utf-8')	
#~ file.close()

#~ process = subprocess.Popen(shlex.split("aria2c 'http://40.rdeb.io/d/GXAO754SNWWEM/Penny%20On%20M.A.R.S.%20-%20S01E08%20Just%20Acting%20%5BOn%20Demand%5D%201080i%20HDMania.ts' -l 'status.txt' --log-level=notice"), 
	#~ stdout=subprocess.PIPE)
#~ process.wait()

#~ for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
	#~ file  = open('status.txt','a', encoding='utf-8')
	#~ file.write(line)
	#~ file.close()
	
rc, fname  = run_command("aria2c 'http://40.rdeb.io/d/G7HN44JBX5SIW/Penny%20On%20M.A.R.S.%20-%20Rain%20and%20Shine%201080i%20HDMania.ts'")

print([fname])