Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajuste de São Paulo, SP para funcionar nos moldes atuais do QD (de 2017-06-01 em diante) #775

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions data_collection/gazette/spiders/sp/sp_sao_paulo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import locale
import re
from datetime import date, datetime
from datetime import date

import scrapy
from dateutil.rrule import DAILY, rrule
Expand All @@ -9,6 +8,20 @@
from gazette.spiders.base import BaseGazetteSpider

RE_MAX_PAGE_NUM = re.compile(r"\d+ de (\d+)")
FULL_MONTH_NAME = {
1: "Janeiro",
2: "Fevereiro",
3: "Março",
4: "Abril",
5: "Maio",
6: "Junho",
7: "Julho",
8: "Agosto",
9: "Setembro",
10: "Outubro",
11: "Novembro",
12: "Dezembro",
}


class SpSaoPauloSpider(BaseGazetteSpider):
Expand All @@ -19,9 +32,7 @@ class SpSaoPauloSpider(BaseGazetteSpider):
start_date = date(2017, 6, 1)

def start_requests(self):
# Need to have the month's name in portuguese for the pdf url
locale.setlocale(locale.LC_TIME, "pt_BR.UTF-8")
for day in rrule(freq=DAILY, dtstart=self.start_date, until=date.today()):
for day in rrule(freq=DAILY, dtstart=self.start_date, until=self.end_date):
url = f"{self.BASE_URL}/nav_v6/header.asp?txtData={day.strftime('%d/%m/%Y')}&cad=1"
yield scrapy.Request(url, cb_kwargs=dict(day=day.date()))

Expand All @@ -39,7 +50,14 @@ def parse(self, response, day):
max_page = self.get_max_page(response)
if not max_page:
return
day_url = f"{self.BASE_URL}/doflash/prototipo/{day.strftime('%Y')}/{day.strftime('%B')}/{day.strftime('%d')}/cidade/pdf"
day_url = (
f"{self.BASE_URL}"
"/doflash/prototipo"
f"/{day.strftime('%Y')}"
f"/{FULL_MONTH_NAME[day.month]}"
f"/{day.strftime('%d')}"
"/cidade/pdf"
)
urls = [f"{day_url}/pg_{page:04}.pdf" for page in range(1, max_page + 1)]

yield Gazette(
Expand All @@ -48,5 +66,4 @@ def parse(self, response, day):
is_extra_edition=False,
territory_id=self.TERRITORY_ID,
power="executive",
scraped_at=datetime.utcnow(),
)
Loading