-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move TSV stuff to its own file
- Loading branch information
Showing
4 changed files
with
40 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,34 @@ | ||
import csv | ||
from datetime import date, timedelta | ||
from io import StringIO | ||
from typing import Generator | ||
|
||
import requests | ||
|
||
|
||
def get_data(date: date, fname: str) -> Generator: | ||
url_tmpl = f"https://ton.twimg.com/birdwatch-public-data/{{date}}/{fname}/{fname}-00000.tsv" | ||
url = url_tmpl.format(date=date.strftime("%Y/%m/%d")) | ||
r = requests.get(url, stream=True) | ||
r.raise_for_status() | ||
|
||
def _data_generator() -> Generator: | ||
headers = None | ||
for line in r.iter_lines(): | ||
cols = next(csv.reader(StringIO(line.decode()), delimiter="\t")) | ||
if not headers: | ||
headers = cols | ||
continue | ||
yield dict(zip(headers, cols)) | ||
|
||
return _data_generator() | ||
|
||
|
||
def get_generator(fname: str) -> Generator: | ||
today = date.today() | ||
try: | ||
return get_data(today, fname) | ||
except Exception: | ||
pass | ||
yesterday = today - timedelta(days=1) | ||
return get_data(yesterday, fname) |