-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump-csv.py
44 lines (33 loc) · 1.21 KB
/
dump-csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sys
import csv
import requests
import demjson
reload(sys)
sys.setdefaultencoding('utf-8')
if len(sys.argv) == 3:
tarjeta_inicio = int(sys.argv[1])
delta = int(sys.argv[2])
else:
tarjeta_inicio = 10000000
delta = 100
tarjeta_fin = delta + tarjeta_inicio
url = "http://www.gpssumo.com/movimientos/get_movimientos/"
session = requests.session()
with open('tarjetas.csv', 'wb') as myfile:
out = csv.writer(myfile, quoting=csv.QUOTE_ALL)
encabezado = ["tarjeta", "hora", "transaccion", "tipo", "recorrido", "fecha", "saldo", "cant_pasajes", "boleto", "importe", "saldo_viajes", "linea", "unidad"]
out.writerow(encabezado)
for tarjeta in range(tarjeta_inicio, tarjeta_fin):
response = session.get(url + str(tarjeta))
transacciones_tarjeta = demjson.decode(response.text.strip())
try:
iterator = iter(transacciones_tarjeta)
except TypeError:
print tarjeta,' no existente o error'
else:
print tarjeta
for transaccion in transacciones_tarjeta:
row = [tarjeta]
for key, value in transaccion.iteritems():
row.append(value)
out.writerow(row)