Commit 351ffe7256225a953497fd29d368b7212617dd31
- Diff rendering mode:
- inline
- side by side
failburn.py
(56 / 1)
|   | |||
| 1 | 1 | #!/usr/bin/python | |
| 2 | # | ||
| 2 | 3 | ||
| 3 | 4 | import csv | |
| 5 | import urllib | ||
| 6 | import urllib2 | ||
| 7 | import time | ||
| 4 | 8 | ||
| 9 | # fake user-agent | ||
| 10 | headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; ' \ | ||
| 11 | 'rv:1.9.0.2) Gecko/2008092313 Ubuntu/8.04 (hardy) Firefox/3.1.6'} | ||
| 12 | |||
| 5 | 13 | sources = ['Inidoneas', 'Suspensas'] | |
| 6 | 14 | ||
| 15 | def _donations(cnpj, name): | ||
| 16 | """Search for donations from the CNPJ/Name""" | ||
| 17 | |||
| 7 | 18 | for source in sources: | |
| 8 | 19 | print source + ':' | |
| 9 | 20 | content = csv.reader(open(source + '.csv'), delimiter=';') | |
| 21 | |||
| 22 | # little dirty trick to skip the first row (first next() gets the row, | ||
| 23 | # second moves to the second row, so the for starts there.) | ||
| 10 | 24 | content.next() | |
| 25 | content.next() | ||
| 11 | 26 | ||
| 12 | 27 | for row in content: # skip the headers | |
| 13 | print row[0], row[1].decode('iso-8859-1') | ||
| 28 | print row[0], row[1].decode('iso-8859-1'), | ||
| 29 | |||
| 30 | # remove ".", "/" and "-". Do not translate any other characters. | ||
| 31 | cnpj_clean = row[0].translate(None, './-') | ||
| 32 | |||
| 33 | form = { | ||
| 34 | 'sgUe': '', | ||
| 35 | 'acao': 'Resumo', | ||
| 36 | 'nomeFornecDoador': 'Doador', | ||
| 37 | 'rdTipo': 'receita', | ||
| 38 | 'nmDoador': row[1], | ||
| 39 | 'cdCpfCnpjDoador': cnpj_clean, | ||
| 40 | 'cdEspRecurso': '-1' # any | ||
| 41 | } | ||
| 42 | |||
| 43 | request = urllib2.Request(url='http://www4.tse.gov.br/' \ | ||
| 44 | 'spce2008ConsultaFinanciamento/' \ | ||
| 45 | 'consultaReceitaDespesaCandidatoServlet.do') | ||
| 46 | |||
| 47 | for key in headers: | ||
| 48 | #print 'Header', key, '=', headers[key] | ||
| 49 | request.add_header(key, headers[key]) | ||
| 50 | |||
| 51 | body = urllib.urlencode(form) | ||
| 52 | #print body | ||
| 53 | |||
| 54 | request.add_data(body) | ||
| 55 | |||
| 56 | response = urllib2.urlopen(request) | ||
| 57 | data = response.read() | ||
| 58 | response.close() | ||
| 59 | |||
| 60 | name = cnpj_clean + '.result' | ||
| 61 | output = file(name, 'w') | ||
| 62 | output.write(data) | ||
| 63 | output.close() | ||
| 64 | |||
| 65 | print ' --> done' | ||
| 66 | |||
| 67 | time.sleep(5) # So we don't "burn" the server. | ||
| 68 | |||
| 14 | 69 | ||
| 15 | 70 |

