-
Notifications
You must be signed in to change notification settings - Fork 126
Spider para casos de Rondônia #132
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
Open
berinhard
wants to merge
6
commits into
master
Choose a base branch
from
feature/spider-ro
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05ee5db
Cria spider para extrair dados de RO parseando o código JS de dados
berinhard 9b91257
Ativa o spider de Rondônia
berinhard 98c34cc
Roda o black
berinhard d45f4b4
Adiciona TODO
berinhard d6115fd
Merge branch 'master' into feature/spider-ro
berinhard 804a495
Corrige erros de flake8 em outro spider
berinhard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import json | ||
import scrapy | ||
|
||
from datetime import date | ||
from .base import BaseCovid19Spider | ||
|
||
|
||
class Covid19ROSpider(BaseCovid19Spider): | ||
name = "RO" | ||
start_urls = ["http://covid19.sesau.ro.gov.br"] | ||
|
||
def parse(self, response): | ||
# extract the displayed date (there's no option to change the date to a past one) | ||
date_container = response.xpath( | ||
"//*[text()='calendar_today']/../text()" | ||
).extract() | ||
report_date = [t.strip() for t in date_container if t.strip()][0] | ||
|
||
# get the URL for the JS file with the data | ||
js_script = response.xpath( | ||
"//attribute::*[contains(., 'estadoRO')]/../@src" | ||
).extract()[0] | ||
full_url = response.url + js_script | ||
|
||
year, month, day = [int(v) for v in report_date.split("/")[::-1]] | ||
self.add_report(date=date(year, month, day), url=full_url) | ||
|
||
yield scrapy.Request( | ||
url=full_url, | ||
meta={"row": {"date": date}}, | ||
callback=self.parse_js_data_script, | ||
) | ||
|
||
def parse_js_data_script(self, response): | ||
""" | ||
The JS code only defines a variable called 'cidades' with the required JSON data to the other | ||
JS codes work with. This parsing function cleans up the JS file to get only the JSON content. | ||
""" | ||
json_data = response.body_as_unicode().replace("var cidades = ", "").strip() | ||
content = json.loads(json_data) | ||
|
||
total_confirmed, total_deaths = 0, 0 | ||
for data in [d["properties"] for d in content["features"]]: | ||
city, confirmed, deaths = data["NOME"], data["confirmados"], data["obitos"] | ||
total_confirmed += confirmed | ||
total_deaths += deaths | ||
|
||
self.add_city_case(city=city, confirmed=confirmed, deaths=deaths) | ||
|
||
# TODO: in the future we'll might have to change this once this data is available | ||
self.add_city_case(city="Importados/Indefinidos", confirmed=None, deaths=None) | ||
self.add_state_case(confirmed=total_confirmed, deaths=total_deaths) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eu colocaria um TODO aqui só pra alertar que talvez, no futuro, possamos alterar esses
None
caso eles comecem a divulgar o valor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feito