-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
169 lines (147 loc) · 5.47 KB
/
fetch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import argparse
import datetime as dt
import json
import logging
import os
from pathlib import Path
from typing import Dict, Optional, List
from urllib.parse import urljoin
import requests
from dotenv import load_dotenv, find_dotenv
from requests import HTTPError
from retry import retry
load_dotenv(find_dotenv())
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
endpoints = [
{"endpoint": "admie_dailyenergybalanceanalysis", "date_from": "2020-11-30"},
{"endpoint": "admie_realtimescadares", "date_from": "2020-11-30"},
{"endpoint": "admie_realtimescadasystemload", "date_from": "2020-11-30"},
{"endpoint": "cadastre_natura_plot", "date_from": "2020-11-02"},
{"endpoint": "cadastre_plot", "date_from": "2020-11-02"},
{"endpoint": "eeep_casino_tickets"},
{"endpoint": "eett_telecom_indicators"},
{"endpoint": "efet_inspections"},
{"endpoint": "electricity_consumption", "date_from": "2020-08-09"},
{"endpoint": "elte_auditors"},
{"endpoint": "grnet_atlas"},
{"endpoint": "grnet_eudoxus"},
{"endpoint": "hcg_incidents"},
{"endpoint": "internet_traffic", "date_from": "2019-10-18"},
{"endpoint": "mcp_crime"},
{"endpoint": "mcp_financial_crimes"},
{"endpoint": "mcp_forest_fires", "date_from": "2000-01-01"},
{"endpoint": "mcp_traffic_accidents"},
{"endpoint": "mcp_traffic_violations"},
{"endpoint": "mcp_urban_incidents", "date_from": "2014-01-01"},
{"endpoint": "mindev_realtors"},
{"endpoint": "minedu_dep"},
{"endpoint": "minedu_students_school"},
{"endpoint": "minenv_inspectors"},
{"endpoint": "minhealth_dentists"},
{"endpoint": "minhealth_doctors"},
{"endpoint": "minhealth_pharmacies"},
{"endpoint": "minhealth_pharmacists"},
{"endpoint": "minint_election_age"},
{"endpoint": "minint_election_distribution"},
{"endpoint": "minjust_law_firms"},
{"endpoint": "minjust_lawyers"},
{"endpoint": "mintour_agencies"},
{"endpoint": "oaed_unemployment", "date_from": "2010-02-01"},
{"endpoint": "oasa_ridership", "date_from": "2020-08-08"},
{"endpoint": "oee_accountants"},
{"endpoint": "road_traffic_attica", "date_from": "2020-11-05"},
{"endpoint": "sailing_traffic", "date_from": "2017-01-03"},
]
class RetryException(Exception):
pass
class Fetcher:
BASE_URL = "https://data.gov.gr/api/v1/"
RETRY_ARGS = {"tries": 1_000, "delay": 60, "exceptions": RetryException}
def __init__(
self,
object_: str,
from_date: Optional[dt.date],
to_date: Optional[dt.date],
filepath: Path,
):
self.output_file = filepath
self.access_token = os.environ["DATA_GOV_GR_TOKEN"]
self.object_ = object_
self.from_date = from_date
self.to_date = to_date
logging.info(f"Will fetch {object_}, from:{from_date} to:{to_date}")
@property
def params(self) -> Optional[Dict]:
if not self.from_date and not self.to_date:
return None
return {
"date_from": self.from_date.isoformat(),
"date_to": self.to_date.isoformat(),
}
@property
def headers(self):
return {
"Authorization": "Token " + self.access_token,
"Accept": "application/json",
"Content-Type": "application/json",
}
def get_endpoint_for(self, object_: str, *args) -> str:
path_parts = (object_, *args)
return urljoin(self.BASE_URL, "/".join(path_parts))
@retry(**RETRY_ARGS)
def query(self) -> List[Dict]:
logging.info(f"Querying {self.object_}...")
response = requests.get(
url=self.get_endpoint_for("query", self.object_),
headers=self.headers,
params=self.params,
)
try:
response.raise_for_status()
except HTTPError as exc:
if response.status_code in {429, 504}:
raise RetryException(exc)
return json.loads(response.content)
def sink(self, data: List[Dict]) -> None:
if not data:
logging.warning("No data")
return
logging.info(f"Writing to file {self.output_file}...")
self.output_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.output_file, "w") as out:
for dictionary in data:
json.dump(dictionary, out, ensure_ascii=False, sort_keys=True)
out.write("\n")
def fetch(self):
self.sink(self.query())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Download all datasets from data.gov.gr",
usage="python fetch.py --data data/directory/path",
)
parser.add_argument(
"--data",
dest="data",
type=Path,
help="The folder where all data should be stored",
)
args = parser.parse_args()
for endpoint in endpoints:
obj = endpoint["endpoint"]
# Run without date params
if "date_from" not in endpoint:
file = args.data / obj / f"{obj}.json"
fetcher = Fetcher(obj, None, None, file)
fetcher.fetch()
continue
# Run with date params
date = dt.date.fromisoformat(endpoint["date_from"])
while date < dt.date.today():
file = args.data / obj / date.isoformat() / f"{obj}.json"
fetcher = Fetcher(obj, date, date, file)
fetcher.fetch()
date += dt.timedelta(days=1)