joshuasundance
commited on
Commit
·
795da8d
1
Parent(s):
40c40c8
Scraping / parquet notebooks
Browse files[very messy] notebooks showing the process of scraping the data, restructuring it, and saving as parquet
- failed_scrape_11142023.ipynb +317 -0
- scrape_2_11142023.ipynb +339 -0
- to_parquet.ipynb +335 -0
failed_scrape_11142023.ipynb
ADDED
@@ -0,0 +1,317 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "ec529532-5b5f-4f09-b925-6fab408978fe",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"Defaulting to user installation because normal site-packages is not writeable\n",
|
14 |
+
"Requirement already satisfied: aiofiles in /home/appuser/.local/lib/python3.11/site-packages (23.2.1)\n",
|
15 |
+
"Requirement already satisfied: tqdm in /home/appuser/.local/lib/python3.11/site-packages (4.66.1)\n",
|
16 |
+
"\n",
|
17 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
|
18 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
|
19 |
+
]
|
20 |
+
}
|
21 |
+
],
|
22 |
+
"source": [
|
23 |
+
"!pip install aiofiles tqdm"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 2,
|
29 |
+
"id": "ddfc2d01-6c44-496c-a65b-9a9f44534a2e",
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"import asyncio\n",
|
34 |
+
"import json\n",
|
35 |
+
"import os\n",
|
36 |
+
"import re\n",
|
37 |
+
"\n",
|
38 |
+
"import aiofiles\n",
|
39 |
+
"import aiohttp\n",
|
40 |
+
"import pandas as pd\n",
|
41 |
+
"import requests\n",
|
42 |
+
"import tqdm.asyncio\n",
|
43 |
+
"\n",
|
44 |
+
"from restgdf.utils.crawl import fetch_all_data\n",
|
45 |
+
"from restgdf.utils.getinfo import service_metadata, get_metadata\n",
|
46 |
+
"\n",
|
47 |
+
"\n",
|
48 |
+
"api_root = \"http://restgdf_api:8080\"\n",
|
49 |
+
"ms_root = f\"{api_root}/mappingsupport\"\n",
|
50 |
+
"ms_endpoints = {name: f\"{ms_root}/{name}\" for name in ('state', 'county', 'town')} | {\"root\": f\"{api_root}/mappingsupport\"}\n",
|
51 |
+
"\n",
|
52 |
+
"\n",
|
53 |
+
"def get_df() -> pd.DataFrame:\n",
|
54 |
+
" resp = requests.get(ms_endpoints['root'])\n",
|
55 |
+
" data = resp.json()\n",
|
56 |
+
" return pd.DataFrame(data)\n",
|
57 |
+
"\n",
|
58 |
+
"\n",
|
59 |
+
"def get_unique_roots(df: pd.DataFrame) -> pd.Series:\n",
|
60 |
+
" pat = re.compile(r\"(?P<root>^.*/rest/services?).*?$\", re.IGNORECASE)\n",
|
61 |
+
" return (\n",
|
62 |
+
" df\n",
|
63 |
+
" .join(\n",
|
64 |
+
" df['ArcGIS-url']\n",
|
65 |
+
" .dropna()\n",
|
66 |
+
" .str.strip()\n",
|
67 |
+
" .str.extract(pat)\n",
|
68 |
+
" ['root']\n",
|
69 |
+
" .dropna()\n",
|
70 |
+
" )\n",
|
71 |
+
" ['root']\n",
|
72 |
+
" .dropna()\n",
|
73 |
+
" .drop_duplicates()\n",
|
74 |
+
" .sort_values()\n",
|
75 |
+
" )\n"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": 3,
|
81 |
+
"id": "10fb3d81-86a8-44e2-a60f-e909a5836b1d",
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [
|
84 |
+
{
|
85 |
+
"name": "stdout",
|
86 |
+
"output_type": "stream",
|
87 |
+
"text": [
|
88 |
+
"CPU times: user 34.2 ms, sys: 3.42 ms, total: 37.6 ms\n",
|
89 |
+
"Wall time: 241 ms\n"
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"source": [
|
94 |
+
"%%time\n",
|
95 |
+
"\n",
|
96 |
+
"df = get_df()\n",
|
97 |
+
"\n",
|
98 |
+
"unique_roots = get_unique_roots(df)"
|
99 |
+
]
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"cell_type": "code",
|
103 |
+
"execution_count": 4,
|
104 |
+
"id": "3c66c7b0-f9a6-404e-a353-9457b5c47332",
|
105 |
+
"metadata": {},
|
106 |
+
"outputs": [
|
107 |
+
{
|
108 |
+
"data": {
|
109 |
+
"text/html": [
|
110 |
+
"<div>\n",
|
111 |
+
"<style scoped>\n",
|
112 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
113 |
+
" vertical-align: middle;\n",
|
114 |
+
" }\n",
|
115 |
+
"\n",
|
116 |
+
" .dataframe tbody tr th {\n",
|
117 |
+
" vertical-align: top;\n",
|
118 |
+
" }\n",
|
119 |
+
"\n",
|
120 |
+
" .dataframe thead th {\n",
|
121 |
+
" text-align: right;\n",
|
122 |
+
" }\n",
|
123 |
+
"</style>\n",
|
124 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
125 |
+
" <thead>\n",
|
126 |
+
" <tr style=\"text-align: right;\">\n",
|
127 |
+
" <th></th>\n",
|
128 |
+
" <th>url</th>\n",
|
129 |
+
" <th>hash</th>\n",
|
130 |
+
" </tr>\n",
|
131 |
+
" </thead>\n",
|
132 |
+
" <tbody>\n",
|
133 |
+
" <tr>\n",
|
134 |
+
" <th>0</th>\n",
|
135 |
+
" <td>http://136.234.13.165/arcgis/rest/services</td>\n",
|
136 |
+
" <td>8489457957960488436</td>\n",
|
137 |
+
" </tr>\n",
|
138 |
+
" <tr>\n",
|
139 |
+
" <th>1</th>\n",
|
140 |
+
" <td>http://216.167.160.20/arcgis107/rest/services</td>\n",
|
141 |
+
" <td>4291004727045614516</td>\n",
|
142 |
+
" </tr>\n",
|
143 |
+
" <tr>\n",
|
144 |
+
" <th>2</th>\n",
|
145 |
+
" <td>http://ags.co.okaloosa.fl.us/arcgis/rest/services</td>\n",
|
146 |
+
" <td>10213391155691883489</td>\n",
|
147 |
+
" </tr>\n",
|
148 |
+
" <tr>\n",
|
149 |
+
" <th>3</th>\n",
|
150 |
+
" <td>http://arcgis.dcnr.nv.gov/arcgis/rest/services</td>\n",
|
151 |
+
" <td>8177309747274201625</td>\n",
|
152 |
+
" </tr>\n",
|
153 |
+
" <tr>\n",
|
154 |
+
" <th>4</th>\n",
|
155 |
+
" <td>http://arcgis2.williamsoncounty-tn.gov/arcgis/...</td>\n",
|
156 |
+
" <td>1860738680214392857</td>\n",
|
157 |
+
" </tr>\n",
|
158 |
+
" </tbody>\n",
|
159 |
+
"</table>\n",
|
160 |
+
"</div>"
|
161 |
+
],
|
162 |
+
"text/plain": [
|
163 |
+
" url hash\n",
|
164 |
+
"0 http://136.234.13.165/arcgis/rest/services 8489457957960488436\n",
|
165 |
+
"1 http://216.167.160.20/arcgis107/rest/services 4291004727045614516\n",
|
166 |
+
"2 http://ags.co.okaloosa.fl.us/arcgis/rest/services 10213391155691883489\n",
|
167 |
+
"3 http://arcgis.dcnr.nv.gov/arcgis/rest/services 8177309747274201625\n",
|
168 |
+
"4 http://arcgis2.williamsoncounty-tn.gov/arcgis/... 1860738680214392857"
|
169 |
+
]
|
170 |
+
},
|
171 |
+
"execution_count": 4,
|
172 |
+
"metadata": {},
|
173 |
+
"output_type": "execute_result"
|
174 |
+
}
|
175 |
+
],
|
176 |
+
"source": [
|
177 |
+
"unique_root_hashes = pd.util.hash_pandas_object(unique_roots)\n",
|
178 |
+
"\n",
|
179 |
+
"unique_root_df = pd.DataFrame(zip(unique_roots, unique_root_hashes), columns=['url','hash'])\n",
|
180 |
+
"\n",
|
181 |
+
"unique_root_df.head()"
|
182 |
+
]
|
183 |
+
},
|
184 |
+
{
|
185 |
+
"cell_type": "code",
|
186 |
+
"execution_count": 5,
|
187 |
+
"id": "17917509-20e6-458c-8cfd-a0403ed73779",
|
188 |
+
"metadata": {},
|
189 |
+
"outputs": [],
|
190 |
+
"source": [
|
191 |
+
"unique_root_df.to_csv('unique_roots.csv', index=False)"
|
192 |
+
]
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"cell_type": "code",
|
196 |
+
"execution_count": 6,
|
197 |
+
"id": "9b3ef1af-4cbe-45cd-9986-1b45f8392099",
|
198 |
+
"metadata": {},
|
199 |
+
"outputs": [],
|
200 |
+
"source": [
|
201 |
+
"done_dict = {(r.url, r.hash): False for r in unique_root_df.itertuples(index=False)}"
|
202 |
+
]
|
203 |
+
},
|
204 |
+
{
|
205 |
+
"cell_type": "code",
|
206 |
+
"execution_count": 7,
|
207 |
+
"id": "2a70822d-e090-40af-b631-0b352e4a6a43",
|
208 |
+
"metadata": {},
|
209 |
+
"outputs": [],
|
210 |
+
"source": [
|
211 |
+
"async def scrape_to_files(url_hash_dict: dict[tuple, bool], output_directory: str) -> dict[tuple, bool]:\n",
|
212 |
+
" done_dict = url_hash_dict\n",
|
213 |
+
" async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:\n",
|
214 |
+
"\n",
|
215 |
+
" async def fetch_to_file(url: str, hash: int) -> None:\n",
|
216 |
+
" filepath = os.path.join(output_directory, f\"{hash}.json\")\n",
|
217 |
+
" if os.path.exists(filepath):\n",
|
218 |
+
" return None\n",
|
219 |
+
" try:\n",
|
220 |
+
" data = await fetch_all_data(session, url, return_feature_count=False)\n",
|
221 |
+
" if 'error' in data:\n",
|
222 |
+
" data['error'] = str(data['error'])\n",
|
223 |
+
" async with aiofiles.open(filepath, 'w') as outf:\n",
|
224 |
+
" await outf.write(json.dumps(data))\n",
|
225 |
+
" done_dict[(url, hash)] = True\n",
|
226 |
+
" except Exception as e:\n",
|
227 |
+
" done_dict[(url, hash)] = e\n",
|
228 |
+
" \n",
|
229 |
+
" tasks = [fetch_to_file(url, hash) for url, hash in done_dict.keys()]\n",
|
230 |
+
" results = await tqdm.asyncio.tqdm_asyncio.gather(*tasks)\n",
|
231 |
+
" return done_dict\n"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"cell_type": "code",
|
236 |
+
"execution_count": 8,
|
237 |
+
"id": "bf720487-a42b-4c39-b6bd-f9ab2e01cd0b",
|
238 |
+
"metadata": {},
|
239 |
+
"outputs": [],
|
240 |
+
"source": [
|
241 |
+
"output_dir = os.path.abspath(\"output\")\n",
|
242 |
+
"\n",
|
243 |
+
"if not os.path.exists(output_dir):\n",
|
244 |
+
" os.mkdir(output_dir)"
|
245 |
+
]
|
246 |
+
},
|
247 |
+
{
|
248 |
+
"cell_type": "code",
|
249 |
+
"execution_count": 9,
|
250 |
+
"id": "ef74d321-6ef5-40d8-90b3-a990f32c1ea1",
|
251 |
+
"metadata": {},
|
252 |
+
"outputs": [],
|
253 |
+
"source": [
|
254 |
+
"import nest_asyncio\n",
|
255 |
+
"\n",
|
256 |
+
"nest_asyncio.apply()"
|
257 |
+
]
|
258 |
+
},
|
259 |
+
{
|
260 |
+
"cell_type": "code",
|
261 |
+
"execution_count": 10,
|
262 |
+
"id": "b1859b15-3d0d-4ef7-8ecd-6c57c6e3b600",
|
263 |
+
"metadata": {},
|
264 |
+
"outputs": [
|
265 |
+
{
|
266 |
+
"name": "stderr",
|
267 |
+
"output_type": "stream",
|
268 |
+
"text": [
|
269 |
+
" 2%|▏ | 39/2038 [00:06<02:55, 11.41it/s] Can not load response cookies: Illegal key 'Domain=.sherburne.mn.us,AGS_ROLES'\n",
|
270 |
+
"100%|██████████| 2038/2038 [24:38<00:00, 1.38it/s] "
|
271 |
+
]
|
272 |
+
},
|
273 |
+
{
|
274 |
+
"name": "stdout",
|
275 |
+
"output_type": "stream",
|
276 |
+
"text": [
|
277 |
+
"CPU times: user 21min 43s, sys: 35.1 s, total: 22min 18s\n",
|
278 |
+
"Wall time: 24min 38s\n"
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"name": "stderr",
|
283 |
+
"output_type": "stream",
|
284 |
+
"text": [
|
285 |
+
"\n"
|
286 |
+
]
|
287 |
+
}
|
288 |
+
],
|
289 |
+
"source": [
|
290 |
+
"%%time\n",
|
291 |
+
"\n",
|
292 |
+
"results = asyncio.run(scrape_to_files(done_dict, output_dir))"
|
293 |
+
]
|
294 |
+
}
|
295 |
+
],
|
296 |
+
"metadata": {
|
297 |
+
"kernelspec": {
|
298 |
+
"display_name": "Python 3 (ipykernel)",
|
299 |
+
"language": "python",
|
300 |
+
"name": "python3"
|
301 |
+
},
|
302 |
+
"language_info": {
|
303 |
+
"codemirror_mode": {
|
304 |
+
"name": "ipython",
|
305 |
+
"version": 3
|
306 |
+
},
|
307 |
+
"file_extension": ".py",
|
308 |
+
"mimetype": "text/x-python",
|
309 |
+
"name": "python",
|
310 |
+
"nbconvert_exporter": "python",
|
311 |
+
"pygments_lexer": "ipython3",
|
312 |
+
"version": "3.11.6"
|
313 |
+
}
|
314 |
+
},
|
315 |
+
"nbformat": 4,
|
316 |
+
"nbformat_minor": 5
|
317 |
+
}
|
scrape_2_11142023.ipynb
ADDED
@@ -0,0 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "ec529532-5b5f-4f09-b925-6fab408978fe",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"Defaulting to user installation because normal site-packages is not writeable\n",
|
14 |
+
"Requirement already satisfied: aiofiles in /home/appuser/.local/lib/python3.11/site-packages (23.2.1)\n",
|
15 |
+
"Requirement already satisfied: tqdm in /home/appuser/.local/lib/python3.11/site-packages (4.66.1)\n",
|
16 |
+
"Collecting tenacity\n",
|
17 |
+
" Obtaining dependency information for tenacity from https://files.pythonhosted.org/packages/f4/f1/990741d5bb2487d529d20a433210ffa136a367751e454214013b441c4575/tenacity-8.2.3-py3-none-any.whl.metadata\n",
|
18 |
+
" Downloading tenacity-8.2.3-py3-none-any.whl.metadata (1.0 kB)\n",
|
19 |
+
"Downloading tenacity-8.2.3-py3-none-any.whl (24 kB)\n",
|
20 |
+
"Installing collected packages: tenacity\n",
|
21 |
+
"Successfully installed tenacity-8.2.3\n",
|
22 |
+
"\n",
|
23 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
|
24 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
|
25 |
+
]
|
26 |
+
}
|
27 |
+
],
|
28 |
+
"source": [
|
29 |
+
"!pip install aiofiles tqdm tenacity"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": 2,
|
35 |
+
"id": "ddfc2d01-6c44-496c-a65b-9a9f44534a2e",
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"import asyncio\n",
|
40 |
+
"import json\n",
|
41 |
+
"import os\n",
|
42 |
+
"import re\n",
|
43 |
+
"\n",
|
44 |
+
"import aiofiles\n",
|
45 |
+
"import aiohttp\n",
|
46 |
+
"import pandas as pd\n",
|
47 |
+
"import requests\n",
|
48 |
+
"import tqdm.asyncio\n",
|
49 |
+
"import asyncio\n",
|
50 |
+
"import aiohttp\n",
|
51 |
+
"import json\n",
|
52 |
+
"import os\n",
|
53 |
+
"from tenacity import retry, stop_after_attempt, wait_exponential\n",
|
54 |
+
"# import logging\n",
|
55 |
+
"from tqdm.asyncio import tqdm_asyncio\n",
|
56 |
+
"\n",
|
57 |
+
"# Setup logging\n",
|
58 |
+
"# logging.basicConfig(level=logging.INFO)\n",
|
59 |
+
"# logger = logging.getLogger(__name__)\n",
|
60 |
+
"\n",
|
61 |
+
"from restgdf.utils.crawl import fetch_all_data\n",
|
62 |
+
"from restgdf.utils.getinfo import service_metadata, get_metadata\n",
|
63 |
+
"\n",
|
64 |
+
"\n",
|
65 |
+
"api_root = \"http://restgdf_api:8080\"\n",
|
66 |
+
"ms_root = f\"{api_root}/mappingsupport\"\n",
|
67 |
+
"ms_endpoints = {name: f\"{ms_root}/{name}\" for name in ('state', 'county', 'town')} | {\"root\": f\"{api_root}/mappingsupport\"}\n",
|
68 |
+
"\n",
|
69 |
+
"@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))\n",
|
70 |
+
"def get_df() -> pd.DataFrame:\n",
|
71 |
+
" resp = requests.get(ms_endpoints['root'])\n",
|
72 |
+
" data = resp.json()\n",
|
73 |
+
" return pd.DataFrame(data)\n",
|
74 |
+
"\n",
|
75 |
+
"\n",
|
76 |
+
"def get_unique_roots(df: pd.DataFrame) -> pd.Series:\n",
|
77 |
+
" pat = re.compile(r\"(?P<root>^.*/rest/services?).*?$\", re.IGNORECASE)\n",
|
78 |
+
" return (\n",
|
79 |
+
" df\n",
|
80 |
+
" .join(\n",
|
81 |
+
" df['ArcGIS-url']\n",
|
82 |
+
" .dropna()\n",
|
83 |
+
" .str.strip()\n",
|
84 |
+
" .str.extract(pat)\n",
|
85 |
+
" ['root']\n",
|
86 |
+
" .dropna()\n",
|
87 |
+
" )\n",
|
88 |
+
" ['root']\n",
|
89 |
+
" .dropna()\n",
|
90 |
+
" .drop_duplicates()\n",
|
91 |
+
" .sort_values()\n",
|
92 |
+
" )\n"
|
93 |
+
]
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"execution_count": 6,
|
98 |
+
"id": "10fb3d81-86a8-44e2-a60f-e909a5836b1d",
|
99 |
+
"metadata": {},
|
100 |
+
"outputs": [
|
101 |
+
{
|
102 |
+
"name": "stdout",
|
103 |
+
"output_type": "stream",
|
104 |
+
"text": [
|
105 |
+
"CPU times: user 97 µs, sys: 6.16 ms, total: 6.25 ms\n",
|
106 |
+
"Wall time: 7.61 ms\n"
|
107 |
+
]
|
108 |
+
}
|
109 |
+
],
|
110 |
+
"source": [
|
111 |
+
"%%time\n",
|
112 |
+
"\n",
|
113 |
+
"# df = get_df()\n",
|
114 |
+
"\n",
|
115 |
+
"# unique_roots = get_unique_roots(df)"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"cell_type": "code",
|
120 |
+
"execution_count": 7,
|
121 |
+
"id": "3c66c7b0-f9a6-404e-a353-9457b5c47332",
|
122 |
+
"metadata": {},
|
123 |
+
"outputs": [
|
124 |
+
{
|
125 |
+
"data": {
|
126 |
+
"text/html": [
|
127 |
+
"<div>\n",
|
128 |
+
"<style scoped>\n",
|
129 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
130 |
+
" vertical-align: middle;\n",
|
131 |
+
" }\n",
|
132 |
+
"\n",
|
133 |
+
" .dataframe tbody tr th {\n",
|
134 |
+
" vertical-align: top;\n",
|
135 |
+
" }\n",
|
136 |
+
"\n",
|
137 |
+
" .dataframe thead th {\n",
|
138 |
+
" text-align: right;\n",
|
139 |
+
" }\n",
|
140 |
+
"</style>\n",
|
141 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
142 |
+
" <thead>\n",
|
143 |
+
" <tr style=\"text-align: right;\">\n",
|
144 |
+
" <th></th>\n",
|
145 |
+
" <th>url</th>\n",
|
146 |
+
" <th>hash</th>\n",
|
147 |
+
" </tr>\n",
|
148 |
+
" </thead>\n",
|
149 |
+
" <tbody>\n",
|
150 |
+
" <tr>\n",
|
151 |
+
" <th>0</th>\n",
|
152 |
+
" <td>http://136.234.13.165/arcgis/rest/services</td>\n",
|
153 |
+
" <td>8489457957960488436</td>\n",
|
154 |
+
" </tr>\n",
|
155 |
+
" <tr>\n",
|
156 |
+
" <th>1</th>\n",
|
157 |
+
" <td>http://216.167.160.20/arcgis107/rest/services</td>\n",
|
158 |
+
" <td>4291004727045614516</td>\n",
|
159 |
+
" </tr>\n",
|
160 |
+
" <tr>\n",
|
161 |
+
" <th>2</th>\n",
|
162 |
+
" <td>http://ags.co.okaloosa.fl.us/arcgis/rest/services</td>\n",
|
163 |
+
" <td>10213391155691883489</td>\n",
|
164 |
+
" </tr>\n",
|
165 |
+
" <tr>\n",
|
166 |
+
" <th>3</th>\n",
|
167 |
+
" <td>http://arcgis.dcnr.nv.gov/arcgis/rest/services</td>\n",
|
168 |
+
" <td>8177309747274201625</td>\n",
|
169 |
+
" </tr>\n",
|
170 |
+
" <tr>\n",
|
171 |
+
" <th>4</th>\n",
|
172 |
+
" <td>http://arcgis2.williamsoncounty-tn.gov/arcgis/...</td>\n",
|
173 |
+
" <td>1860738680214392857</td>\n",
|
174 |
+
" </tr>\n",
|
175 |
+
" </tbody>\n",
|
176 |
+
"</table>\n",
|
177 |
+
"</div>"
|
178 |
+
],
|
179 |
+
"text/plain": [
|
180 |
+
" url hash\n",
|
181 |
+
"0 http://136.234.13.165/arcgis/rest/services 8489457957960488436\n",
|
182 |
+
"1 http://216.167.160.20/arcgis107/rest/services 4291004727045614516\n",
|
183 |
+
"2 http://ags.co.okaloosa.fl.us/arcgis/rest/services 10213391155691883489\n",
|
184 |
+
"3 http://arcgis.dcnr.nv.gov/arcgis/rest/services 8177309747274201625\n",
|
185 |
+
"4 http://arcgis2.williamsoncounty-tn.gov/arcgis/... 1860738680214392857"
|
186 |
+
]
|
187 |
+
},
|
188 |
+
"execution_count": 7,
|
189 |
+
"metadata": {},
|
190 |
+
"output_type": "execute_result"
|
191 |
+
}
|
192 |
+
],
|
193 |
+
"source": [
|
194 |
+
"# unique_root_hashes = pd.util.hash_pandas_object(unique_roots)\n",
|
195 |
+
"\n",
|
196 |
+
"# unique_root_df = pd.DataFrame(zip(unique_roots, unique_root_hashes), columns=['url','hash'])\n",
|
197 |
+
"\n",
|
198 |
+
"unique_root_df = pd.read_csv('unique_roots.csv')\n",
|
199 |
+
"\n",
|
200 |
+
"unique_root_df.head()"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": 5,
|
206 |
+
"id": "17917509-20e6-458c-8cfd-a0403ed73779",
|
207 |
+
"metadata": {},
|
208 |
+
"outputs": [],
|
209 |
+
"source": [
|
210 |
+
"# unique_root_df.to_csv('unique_roots.csv', index=False)"
|
211 |
+
]
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"cell_type": "code",
|
215 |
+
"execution_count": 8,
|
216 |
+
"id": "9b3ef1af-4cbe-45cd-9986-1b45f8392099",
|
217 |
+
"metadata": {},
|
218 |
+
"outputs": [],
|
219 |
+
"source": [
|
220 |
+
"done_dict = {(r.url, r.hash): False for r in unique_root_df.itertuples(index=False)}"
|
221 |
+
]
|
222 |
+
},
|
223 |
+
{
|
224 |
+
"cell_type": "code",
|
225 |
+
"execution_count": 9,
|
226 |
+
"id": "2a70822d-e090-40af-b631-0b352e4a6a43",
|
227 |
+
"metadata": {},
|
228 |
+
"outputs": [],
|
229 |
+
"source": [
|
230 |
+
"async def scrape_to_files(url_hash_dict: dict[tuple, bool], output_directory: str) -> dict[tuple, bool]:\n",
|
231 |
+
" done_dict = url_hash_dict\n",
|
232 |
+
" async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:\n",
|
233 |
+
"\n",
|
234 |
+
" @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))\n",
|
235 |
+
" async def fetch_to_file(url: str, hash: int, semaphore) -> None:\n",
|
236 |
+
" filepath = os.path.join(output_directory, f\"{hash}.json\")\n",
|
237 |
+
" if os.path.exists(filepath):\n",
|
238 |
+
" return None\n",
|
239 |
+
" try:\n",
|
240 |
+
" async with semaphore:\n",
|
241 |
+
" data = await fetch_all_data(session, url, return_feature_count=False)\n",
|
242 |
+
" if 'error' in data:\n",
|
243 |
+
" data['error'] = str(data['error'])\n",
|
244 |
+
" async with aiofiles.open(filepath, 'w') as outf:\n",
|
245 |
+
" await outf.write(json.dumps(data))\n",
|
246 |
+
" done_dict[(url, hash)] = True\n",
|
247 |
+
" except Exception as e:\n",
|
248 |
+
" done_dict[(url, hash)] = e\n",
|
249 |
+
"\n",
|
250 |
+
" semaphore = asyncio.Semaphore(10)\n",
|
251 |
+
" tasks = [fetch_to_file(url, hash, semaphore) for url, hash in done_dict.keys()]\n",
|
252 |
+
" results = await tqdm.asyncio.tqdm_asyncio.gather(*tasks)\n",
|
253 |
+
" return done_dict\n"
|
254 |
+
]
|
255 |
+
},
|
256 |
+
{
|
257 |
+
"cell_type": "code",
|
258 |
+
"execution_count": 10,
|
259 |
+
"id": "bf720487-a42b-4c39-b6bd-f9ab2e01cd0b",
|
260 |
+
"metadata": {},
|
261 |
+
"outputs": [],
|
262 |
+
"source": [
|
263 |
+
"output_dir = os.path.abspath(\"output_tryagain\")\n",
|
264 |
+
"\n",
|
265 |
+
"if not os.path.exists(output_dir):\n",
|
266 |
+
" os.mkdir(output_dir)"
|
267 |
+
]
|
268 |
+
},
|
269 |
+
{
|
270 |
+
"cell_type": "code",
|
271 |
+
"execution_count": 11,
|
272 |
+
"id": "ef74d321-6ef5-40d8-90b3-a990f32c1ea1",
|
273 |
+
"metadata": {},
|
274 |
+
"outputs": [],
|
275 |
+
"source": [
|
276 |
+
"import nest_asyncio\n",
|
277 |
+
"\n",
|
278 |
+
"nest_asyncio.apply()"
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"cell_type": "code",
|
283 |
+
"execution_count": 12,
|
284 |
+
"id": "b1859b15-3d0d-4ef7-8ecd-6c57c6e3b600",
|
285 |
+
"metadata": {},
|
286 |
+
"outputs": [
|
287 |
+
{
|
288 |
+
"name": "stderr",
|
289 |
+
"output_type": "stream",
|
290 |
+
"text": [
|
291 |
+
" 86%|████████▌ | 1753/2038 [1:54:37<21:53, 4.61s/it] Can not load response cookies: Illegal key 'Domain=.sherburne.mn.us,AGS_ROLES'\n",
|
292 |
+
"100%|██████████| 2038/2038 [2:16:40<00:00, 4.02s/it]"
|
293 |
+
]
|
294 |
+
},
|
295 |
+
{
|
296 |
+
"name": "stdout",
|
297 |
+
"output_type": "stream",
|
298 |
+
"text": [
|
299 |
+
"CPU times: user 1h 15min 46s, sys: 2min 35s, total: 1h 18min 22s\n",
|
300 |
+
"Wall time: 2h 16min 40s\n"
|
301 |
+
]
|
302 |
+
},
|
303 |
+
{
|
304 |
+
"name": "stderr",
|
305 |
+
"output_type": "stream",
|
306 |
+
"text": [
|
307 |
+
"\n"
|
308 |
+
]
|
309 |
+
}
|
310 |
+
],
|
311 |
+
"source": [
|
312 |
+
"%%time\n",
|
313 |
+
"\n",
|
314 |
+
"results = asyncio.run(scrape_to_files(done_dict, output_dir))"
|
315 |
+
]
|
316 |
+
}
|
317 |
+
],
|
318 |
+
"metadata": {
|
319 |
+
"kernelspec": {
|
320 |
+
"display_name": "Python 3 (ipykernel)",
|
321 |
+
"language": "python",
|
322 |
+
"name": "python3"
|
323 |
+
},
|
324 |
+
"language_info": {
|
325 |
+
"codemirror_mode": {
|
326 |
+
"name": "ipython",
|
327 |
+
"version": 3
|
328 |
+
},
|
329 |
+
"file_extension": ".py",
|
330 |
+
"mimetype": "text/x-python",
|
331 |
+
"name": "python",
|
332 |
+
"nbconvert_exporter": "python",
|
333 |
+
"pygments_lexer": "ipython3",
|
334 |
+
"version": "3.11.6"
|
335 |
+
}
|
336 |
+
},
|
337 |
+
"nbformat": 4,
|
338 |
+
"nbformat_minor": 5
|
339 |
+
}
|
to_parquet.ipynb
ADDED
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"id": "3fbb8cd1-679c-4f8a-8889-43ed516bfedd",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"Defaulting to user installation because normal site-packages is not writeable\n",
|
14 |
+
"Collecting aiofiles\n",
|
15 |
+
" Obtaining dependency information for aiofiles from https://files.pythonhosted.org/packages/c5/19/5af6804c4cc0fed83f47bff6e413a98a36618e7d40185cd36e69737f3b0e/aiofiles-23.2.1-py3-none-any.whl.metadata\n",
|
16 |
+
" Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
|
17 |
+
"Collecting jsonlines\n",
|
18 |
+
" Obtaining dependency information for jsonlines from https://files.pythonhosted.org/packages/f8/62/d9ba6323b9202dd2fe166beab8a86d29465c41a0288cbe229fac60c1ab8d/jsonlines-4.0.0-py3-none-any.whl.metadata\n",
|
19 |
+
" Downloading jsonlines-4.0.0-py3-none-any.whl.metadata (1.6 kB)\n",
|
20 |
+
"Requirement already satisfied: attrs>=19.2.0 in /home/appuser/.local/lib/python3.11/site-packages (from jsonlines) (23.1.0)\n",
|
21 |
+
"Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
|
22 |
+
"Downloading jsonlines-4.0.0-py3-none-any.whl (8.7 kB)\n",
|
23 |
+
"Installing collected packages: jsonlines, aiofiles\n",
|
24 |
+
"Successfully installed aiofiles-23.2.1 jsonlines-4.0.0\n",
|
25 |
+
"\n",
|
26 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
|
27 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
|
28 |
+
]
|
29 |
+
}
|
30 |
+
],
|
31 |
+
"source": [
|
32 |
+
"!pip install aiofiles"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": 3,
|
38 |
+
"id": "5c72c2e7-d825-4a9f-a179-fbbe07c93000",
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [
|
41 |
+
{
|
42 |
+
"data": {
|
43 |
+
"text/plain": [
|
44 |
+
"1684"
|
45 |
+
]
|
46 |
+
},
|
47 |
+
"execution_count": 3,
|
48 |
+
"metadata": {},
|
49 |
+
"output_type": "execute_result"
|
50 |
+
}
|
51 |
+
],
|
52 |
+
"source": [
|
53 |
+
"import os\n",
|
54 |
+
"from glob import glob\n",
|
55 |
+
"import aiofiles\n",
|
56 |
+
"\n",
|
57 |
+
"output_dir = os.path.abspath(\"./output_tryagain\")\n",
|
58 |
+
"\n",
|
59 |
+
"jsonfiles = glob(os.path.join(output_dir, \"*.json\"))\n",
|
60 |
+
"\n",
|
61 |
+
"len(jsonfiles)"
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "code",
|
66 |
+
"execution_count": 2,
|
67 |
+
"id": "f6fa4495-d6a3-470f-9a79-3838f0e38794",
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [],
|
70 |
+
"source": [
|
71 |
+
"# import json\n",
|
72 |
+
"\n",
|
73 |
+
"# decodeerrors = {}\n",
|
74 |
+
"# deleted = []\n",
|
75 |
+
"# nocontent = []\n",
|
76 |
+
"# with_metadata = {}\n",
|
77 |
+
"\n",
|
78 |
+
"# async def layers_with_metadata():\n",
|
79 |
+
"# async def _check(f):\n",
|
80 |
+
"# delete = False\n",
|
81 |
+
"# async with aiofiles.open(f, 'r') as infile:\n",
|
82 |
+
"# content = await infile.read()\n",
|
83 |
+
"# if content:\n",
|
84 |
+
"# data = json.loads(content)\n",
|
85 |
+
"# if 'metadata' not in data:\n",
|
86 |
+
"# delete = True\n",
|
87 |
+
"# else:\n",
|
88 |
+
"# with_metadata[f] = data\n",
|
89 |
+
"# else:\n",
|
90 |
+
"# nocontent.append(f)\n",
|
91 |
+
"# delete = True\n",
|
92 |
+
"# if delete:\n",
|
93 |
+
"# deleted.append(f)\n",
|
94 |
+
"# os.remove(f)\n",
|
95 |
+
"# return await asyncio.gather(*[_check(f) for f in jsonfiles])\n",
|
96 |
+
"\n",
|
97 |
+
"# import asyncio\n",
|
98 |
+
"# import nest_asyncio\n",
|
99 |
+
"\n",
|
100 |
+
"# nest_asyncio.apply()"
|
101 |
+
]
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"cell_type": "code",
|
105 |
+
"execution_count": 4,
|
106 |
+
"id": "34514f76-bf06-4793-8085-56dc266c6d04",
|
107 |
+
"metadata": {},
|
108 |
+
"outputs": [],
|
109 |
+
"source": [
|
110 |
+
"import asyncio\n",
|
111 |
+
"import json\n",
|
112 |
+
"\n",
|
113 |
+
"async def read_data():\n",
|
114 |
+
" async def _read(f):\n",
|
115 |
+
" async with aiofiles.open(f, 'r') as infile:\n",
|
116 |
+
" content = await infile.read()\n",
|
117 |
+
" return json.loads(content)\n",
|
118 |
+
" tasks = [_read(f) for f in jsonfiles]\n",
|
119 |
+
" return await asyncio.gather(*tasks)\n"
|
120 |
+
]
|
121 |
+
},
|
122 |
+
{
|
123 |
+
"cell_type": "code",
|
124 |
+
"execution_count": 6,
|
125 |
+
"id": "2879f4d6-dfc6-42f7-ac33-25457c62c204",
|
126 |
+
"metadata": {},
|
127 |
+
"outputs": [],
|
128 |
+
"source": [
|
129 |
+
"import nest_asyncio\n",
|
130 |
+
"\n",
|
131 |
+
"nest_asyncio.apply()"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 88,
|
137 |
+
"id": "1b8c5363-774d-4cac-8cb5-a8746f2d113c",
|
138 |
+
"metadata": {},
|
139 |
+
"outputs": [
|
140 |
+
{
|
141 |
+
"name": "stdout",
|
142 |
+
"output_type": "stream",
|
143 |
+
"text": [
|
144 |
+
"CPU times: user 2min 41s, sys: 23 s, total: 3min 4s\n",
|
145 |
+
"Wall time: 2min 50s\n"
|
146 |
+
]
|
147 |
+
}
|
148 |
+
],
|
149 |
+
"source": [
|
150 |
+
"%%time\n",
|
151 |
+
"\n",
|
152 |
+
"data = asyncio.run(read_data())"
|
153 |
+
]
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"cell_type": "code",
|
157 |
+
"execution_count": 89,
|
158 |
+
"id": "dee1ba14-ba44-435f-abc1-91f6cbee6153",
|
159 |
+
"metadata": {},
|
160 |
+
"outputs": [],
|
161 |
+
"source": [
|
162 |
+
"docs = {\n",
|
163 |
+
" \"servers\": [],\n",
|
164 |
+
" \"services\": [],\n",
|
165 |
+
" \"layers\": [],\n",
|
166 |
+
"}\n",
|
167 |
+
"\n",
|
168 |
+
"for server in data:\n",
|
169 |
+
" server_md = server['metadata']\n",
|
170 |
+
" server_url = server_md['url']\n",
|
171 |
+
" server_hash = hash(server_md['url'])\n",
|
172 |
+
" server_md['hash'] = server_hash\n",
|
173 |
+
" for a in ('folders', 'services'):\n",
|
174 |
+
" if a in server_md:\n",
|
175 |
+
" del server_md[a]\n",
|
176 |
+
" docs[\"servers\"].append(server_md)\n",
|
177 |
+
"\n",
|
178 |
+
" for service in server['services']:\n",
|
179 |
+
" service_md = service['metadata']\n",
|
180 |
+
" service_name = service_md['name'] = service['name']\n",
|
181 |
+
" service_url = service_md['url'] = service['url']\n",
|
182 |
+
" service_md['server_url'] = server_url\n",
|
183 |
+
" service_hash = hash(service_url)\n",
|
184 |
+
" service_md['hash'] = service_hash\n",
|
185 |
+
" service_md['server_hash'] = server_hash\n",
|
186 |
+
" layers = service_md['layers']\n",
|
187 |
+
" del service_md['layers']\n",
|
188 |
+
" docs['services'].append(service_md)\n",
|
189 |
+
"\n",
|
190 |
+
" for layer_md in layers:\n",
|
191 |
+
" layer_md['server_url'] = server_url\n",
|
192 |
+
" layer_md['service_name'] = service_name\n",
|
193 |
+
" layer_md['service_url'] = service_url\n",
|
194 |
+
" layer_hash = hash(layer_md['url'])\n",
|
195 |
+
" layer_md['hash'] = layer_hash\n",
|
196 |
+
" layer_md['server_hash'] = server_hash\n",
|
197 |
+
" layer_md['service_hash'] = service_hash\n",
|
198 |
+
" \n",
|
199 |
+
" docs['layers'].append(layer_md)"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"cell_type": "code",
|
204 |
+
"execution_count": 124,
|
205 |
+
"id": "072b1b2e-1309-4e9e-96b0-30c665706e90",
|
206 |
+
"metadata": {},
|
207 |
+
"outputs": [],
|
208 |
+
"source": [
|
209 |
+
"layerdict = {}\n",
|
210 |
+
"for l in docs['layers']:\n",
|
211 |
+
" if 'type' in l:\n",
|
212 |
+
" if l['type'] not in layerdict:\n",
|
213 |
+
" layerdict[l['type']] = []\n",
|
214 |
+
" layerdict[l['type']].append(l)"
|
215 |
+
]
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"cell_type": "code",
|
219 |
+
"execution_count": 125,
|
220 |
+
"id": "5824e617-1211-4642-9714-3b5be74885d5",
|
221 |
+
"metadata": {},
|
222 |
+
"outputs": [
|
223 |
+
{
|
224 |
+
"data": {
|
225 |
+
"text/plain": [
|
226 |
+
"dict_keys(['Raster Layer', 'Group Layer', 'Feature Layer', 'Annotation Layer', 'Annotation SubLayer', 'Mosaic Layer', 'Raster Catalog Layer', 'Terrain Layer', 'Network Analysis Layer', 'Network Dataset Layer', 'Parcel Layer', 'Topology Layer', 'Mosiac Layer', 'Image Service Layer', 'Annotation Sublayer', 'Dimension Layer', 'Tin Layer', 'Table'])"
|
227 |
+
]
|
228 |
+
},
|
229 |
+
"execution_count": 125,
|
230 |
+
"metadata": {},
|
231 |
+
"output_type": "execute_result"
|
232 |
+
}
|
233 |
+
],
|
234 |
+
"source": [
|
235 |
+
"layerdict.keys()"
|
236 |
+
]
|
237 |
+
},
|
238 |
+
{
|
239 |
+
"cell_type": "code",
|
240 |
+
"execution_count": 163,
|
241 |
+
"id": "30b154d9-5f90-41c6-a004-f19dcd00a132",
|
242 |
+
"metadata": {},
|
243 |
+
"outputs": [
|
244 |
+
{
|
245 |
+
"name": "stdout",
|
246 |
+
"output_type": "stream",
|
247 |
+
"text": [
|
248 |
+
"CPU times: user 37 ms, sys: 1.28 ms, total: 38.3 ms\n",
|
249 |
+
"Wall time: 51.8 ms\n"
|
250 |
+
]
|
251 |
+
}
|
252 |
+
],
|
253 |
+
"source": [
|
254 |
+
"%%time\n",
|
255 |
+
"\n",
|
256 |
+
"server_df = pd.DataFrame(docs['servers']).astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str)\n",
|
257 |
+
"server_df.dropna(axis=1, how='all').to_parquet('servers.parquet')"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
{
|
261 |
+
"cell_type": "code",
|
262 |
+
"execution_count": 164,
|
263 |
+
"id": "10cbce4a-8184-48ec-8bae-6bb3b8395ddf",
|
264 |
+
"metadata": {},
|
265 |
+
"outputs": [
|
266 |
+
{
|
267 |
+
"name": "stdout",
|
268 |
+
"output_type": "stream",
|
269 |
+
"text": [
|
270 |
+
"CPU times: user 34.3 s, sys: 1.94 s, total: 36.3 s\n",
|
271 |
+
"Wall time: 37.3 s\n"
|
272 |
+
]
|
273 |
+
}
|
274 |
+
],
|
275 |
+
"source": [
|
276 |
+
"%%time\n",
|
277 |
+
"\n",
|
278 |
+
"services_df = pd.DataFrame(docs['services']).astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str)\n",
|
279 |
+
"services_df.astype(str).to_parquet('services.parquet')"
|
280 |
+
]
|
281 |
+
},
|
282 |
+
{
|
283 |
+
"cell_type": "code",
|
284 |
+
"execution_count": 158,
|
285 |
+
"id": "44e8267c-b07d-4b0d-93f3-662fb6659776",
|
286 |
+
"metadata": {},
|
287 |
+
"outputs": [],
|
288 |
+
"source": [
|
289 |
+
"layer_df = pd.DataFrame(docs['layers'])"
|
290 |
+
]
|
291 |
+
},
|
292 |
+
{
|
293 |
+
"cell_type": "code",
|
294 |
+
"execution_count": 165,
|
295 |
+
"id": "dc247c57-3e17-4cb4-b69b-71ee052fa0ee",
|
296 |
+
"metadata": {},
|
297 |
+
"outputs": [
|
298 |
+
{
|
299 |
+
"name": "stdout",
|
300 |
+
"output_type": "stream",
|
301 |
+
"text": [
|
302 |
+
"CPU times: user 2min 36s, sys: 25.7 s, total: 3min 1s\n",
|
303 |
+
"Wall time: 3min 10s\n"
|
304 |
+
]
|
305 |
+
}
|
306 |
+
],
|
307 |
+
"source": [
|
308 |
+
"%%time\n",
|
309 |
+
"\n",
|
310 |
+
"layer_df.astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str).to_parquet('layers.parquet')"
|
311 |
+
]
|
312 |
+
}
|
313 |
+
],
|
314 |
+
"metadata": {
|
315 |
+
"kernelspec": {
|
316 |
+
"display_name": "Python 3 (ipykernel)",
|
317 |
+
"language": "python",
|
318 |
+
"name": "python3"
|
319 |
+
},
|
320 |
+
"language_info": {
|
321 |
+
"codemirror_mode": {
|
322 |
+
"name": "ipython",
|
323 |
+
"version": 3
|
324 |
+
},
|
325 |
+
"file_extension": ".py",
|
326 |
+
"mimetype": "text/x-python",
|
327 |
+
"name": "python",
|
328 |
+
"nbconvert_exporter": "python",
|
329 |
+
"pygments_lexer": "ipython3",
|
330 |
+
"version": "3.11.6"
|
331 |
+
}
|
332 |
+
},
|
333 |
+
"nbformat": 4,
|
334 |
+
"nbformat_minor": 5
|
335 |
+
}
|