import urllib.request
import urllib.error
import WebScrapper
#Check if doing with requests is better

listaWeb = WebScrapper.getDownloadLink()
errorList = []
for i in range(len(listaWeb)):
	filePath = './PDFs/' + str(listaWeb[i].split('/')[-1])
	if(not (filePath.endswith('.pdf'))):
		filePath = filePath + '.pdf'
	urlDL = 'http://www.smttaju.com.br' + str(listaWeb[i])
	print(filePath,urlDL)
	try:
		urllib.request.urlretrieve(urlDL,filePath)
	except urllib.error.HTTPError as err:
		errorList.append(urlDL)
	except UnicodeEncodeError:
		errorList.append(urlDL)
	except Exception as e:
		errorList.append(urlDL)
		print("Exception occured \n %s" %e)
	urllib.request.urlcleanup()

"""	
List of WebPages that got error, 404 and ASCII Errors
errorList = ['http://www.smttaju.com.br/attachments/article/2538/075 - Litoral Norte - Mercado.pdf',
'http://www.smttaju.com.br/attachments/article/2761/408-2 - Paraíso Sul - D.I.A. 02.23.05.17.pdf', 'http://www.smttaju.com.br/attachments/article/2586/801 - Luiz Alves - Campus.pdf',
'http://www.smttaju.com.br/attachments/article/2705/902 - Lot. Carajás - Term. Marcos Freire.08.08.17.pdf', 'http://www.smttaju.com.br/attachments/article/2091/200 CIC1.pdf',
'http://www.smttaju.com.br/attachments/article/2030/032.1 - Tijuquinha Osvaldo Aranha 01.pdf', 'http://www.smttaju.com.br/attachments/article/2484/009 - Terminal Marcos Freire - Atalaia.pdf',
'http://www.smttaju.com.br/attachments/article/2087/100 CS1.pdf', 'http://www.smttaju.com.br/attachments/article/2049/008 - Porto Sul - Bairro Industrial.pdf',
'http://www.smttaju.com.br/attachments/article/2045/003 - João Alves - Orlando Dantas.pdf', 'http://www.smttaju.com.br/attachments/article/2760/408-1 - Paraíso Sul - D.I.A. 01.23.05.17.pdf',
'http://www.smttaju.com.br/attachments/article/2092/200 CIC2.pdf', 'http://www.smttaju.com.br/attachments/article/2125/504 - 17 de Março - Zona Sul via Aquarius.pdf',
'http://www.smttaju.com.br/attachments/article/2413/402-3 - Santa Lúcia - D.I.A. 03.pdf', 'http://www.smttaju.com.br/attachments/article/2131/612 - São Carlos - Mercado.pdf',
'http://www.smttaju.com.br/attachments/article/2152/1001 - Corujão 01.pdf', 'http://www.smttaju.com.br/attachments/article/2088/100 CS2.pdf',
'http://www.smttaju.com.br/attachments/article/2569/103 - Conj. Maria do Carmo - Maracaju.pdf', 'http://www.smttaju.com.br/attachments/article/2124/503 - Conj. Beira Mar - Zona Sul.pdf',
'http://www.smttaju.com.br/attachments/article/2122/501 - Povoado São José - Zona Sul.pdf', 'http://www.smttaju.com.br/attachments/article/2142/711 - D.I.A. - Nova Saneamento.pdf',
'http://www.smttaju.com.br/attachments/article/2089/101 - Parque São José - Maracaju.pdf', 'http://www.smttaju.com.br/attachments/article/2145/714 - Quissamã - Osvaldo Aranha.pdf',
'http://www.smttaju.com.br/attachments/article/2143/712 - Quissamã - Mercado.pdf', 'http://www.smttaju.com.br/attachments/article/2163/1003C - Corujão 03C.pdf',
'http://www.smttaju.com.br/attachments/article/2162/1003B - Corujão 03B.pdf', 'http://www.smttaju.com.br/attachments/article/2161/1003A - Corujão 03A.pdf',
'http://www.smttaju.com.br/attachments/article/2160/1002C - Corujão 02C.pdf', 'http://www.smttaju.com.br/attachments/article/2159/1002B - Corujão 02B.pdf',
'http://www.smttaju.com.br/attachments/article/2158/1002A - Corujão 02A.pdf', 'http://www.smttaju.com.br/attachments/article/2157/1001C - Corujão 01C.pdf',
'http://www.smttaju.com.br/attachments/article/2156/1001B - Corujão 01B.pdf', 'http://www.smttaju.com.br/attachments/article/2155/1001A - Corujão 01A.pdf',
'http://www.smttaju.com.br/attachments/article/2154/1003 - Corujão 03.pdf', 'http://www.smttaju.com.br/attachments/article/2153/1002 - Corujão 02.pdf']
"""
