Brazillian freight calculator (Correios)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import urllib
from django.conf import CEP_LOJA
from xml.dom import minidom

URL_CORREIOS = 'http://www.correios.com.br/encomendas/precos/calculo.cfm?servico=40010&CepOrigem=%s&CepDestino=%s&Peso=%f&ValorDeclarado=0&resposta=xml' 
		
def calcularFrete(cepDestino, peso):
	url = URL_CORREIOS % (CEP_LOJA, cepDestino, peso/1000.0)
	dom = minidom.parse(urllib.urlopen(url))
	codigoErro = dom.getElementsByTagName("codigo")[0].childNodes[0].data
	if codigoErro=='0':
		return dom.getElementsByTagName("preco_postal")[0].childNodes[0].data
	return False

More like this

  1. models.py with django_dag models for parts hierarchy by j_syk 2 years, 8 months ago
  2. Translation statistics gatherer by ramiro 5 years, 8 months ago
  3. Admin action for a generic "CSV Export" by javinievas 3 years, 1 month ago
  4. Template filter that divides a list into exact columns by davmuz 2 years, 3 months ago
  5. Credit calculator(ua-ru) by fordexa 5 years, 7 months ago

Comments

darek (on August 1, 2008):

In English please.

#

youell (on August 3, 2008):

@darek I think you mean:

Em inglês por favor.

:)

#

aarond10ster (on August 3, 2008):

Looks like a Brazillian freight calculator at a guess. peso means weight. Still, not really conforming to django DRY. That URL shouldn't be hard coded and the screen/DOM scraping is a little short of robust..

#

renatopedigoni (on August 3, 2008):

aarond10ster

What should I do instead putting the hardcoded url? And what about reading the XML output?

Thanks

- darek / youell

Now in english :)

#

aarond10ster (on August 5, 2008):

Hey renatopedigoni,

Sorry, I just re-read my post. I wasn't trying to sound too negative. Its a good contribution! The service you are using is not designed to be used like this so I was just pointing out that it might be a little bit brittle if used on a big site.

About the URL, its a style thing but for me, if the URL doesn't change, it should probably be inside the function or if it does change, it should be in settings.URL_CORREIOS. I don't like global variables - even with python file-level namespaces ;)

#

(Forgotten your password?)