-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfiliacao_download.py
155 lines (141 loc) · 3.51 KB
/
filiacao_download.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import random
import string
import tempfile
from urllib.parse import urljoin
from pathlib import Path
import scrapy
import settings
PARTIES = [
"avante",
"cidadania",
"dc",
"dem",
"mdb",
"novo",
"patriota",
"pcb",
"pcdob",
"pco",
"pdt",
"phs",
"pl",
"pmb",
"pmn",
"pode",
"pp",
"ppl",
"pros",
"prp",
"prtb",
"psb",
"psc",
"psd",
"psdb",
"psl",
"psol",
"pstu",
"pt",
"ptb",
"ptc",
"pv",
"rede",
"republicanos",
"solidariedade",
"up",
]
STATES = [
"ac",
"al",
"am",
"ap",
"ba",
"ce",
"df",
"es",
"go",
"ma",
"mg",
"ms",
"mt",
"pa",
"pb",
"pe",
"pi",
"pr",
"rj",
"rn",
"ro",
"rr",
"rs",
"sc",
"se",
"sp",
"to",
"zz",
]
def random_string(length):
return "".join(random.choice(string.ascii_letters) for _ in range(length))
def random_file():
tmp = tempfile.NamedTemporaryFile(delete=False, suffix="txt")
with open(tmp.name, mode="w") as fobj:
fobj.write("test\n")
return Path(tmp.name)
class FiliadosFileListSpider(scrapy.Spider):
name = "filiados-file-list"
base_url = "http://agencia.tse.jus.br/estatistica/sead/"
def filename(self, party, state):
return f"eleitorado/filiados/uf/filiados_{party}_{state}.zip"
def download_filename(self, party, state):
return settings.DOWNLOAD_PATH / self.filename(party, state)
def url(self, party, state):
return urljoin(self.base_url, self.filename(party, state))
def start_requests(self):
for party in PARTIES:
for state in STATES:
download_filename = self.download_filename(party, state)
url = self.url(party, state)
if not download_filename.exists():
yield scrapy.Request(
url=url,
meta={
"filename": download_filename,
"party": party,
"state": state,
},
callback=self.save_zip,
)
else:
# Hack to yield an already downloaded file from here
temp_filename = random_file()
yield scrapy.Request(
"file://" + str(temp_filename),
meta={
"row": {
"filename": download_filename,
"party": party,
"state": state,
"url": url,
},
"dont_cache": True,
"temp_filename": temp_filename,
},
callback=self.yield_row,
)
def yield_row(self, response):
meta = response.meta
yield meta["row"]
os.unlink(meta["temp_filename"])
def save_zip(self, response):
meta = response.request.meta
filename = meta["filename"]
if not filename.parent.exists():
filename.parent.mkdir(parents=True)
with open(filename, mode="wb") as fobj:
fobj.write(response.body)
yield {
"filename": meta["filename"],
"party": meta["party"],
"state": meta["state"],
"url": response.url,
}