Skip to content

Instantly share code, notes, and snippets.

@simonw
Created July 14, 2020 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/df48b83c2d38bca60c65343ce016688d to your computer and use it in GitHub Desktop.
Save simonw/df48b83c2d38bca60c65343ce016688d to your computer and use it in GitHub Desktop.
How to create a SQLite database of UK power outages using sqlite-utils
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"incidents = requests.get(\"https://www.ukpowernetworks.co.uk/Incidents/GetIncidents\").json()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Incidents', 'TwitterFeeds', 'PollingInterval', 'ServiceDown', 'IsCached'])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"incidents.keys()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'PanelContentUrl': '/power-cut/map/unplanned?incidentId=INCD-36248-C',\n",
" 'PowerCutType': 'Unplanned',\n",
" 'PostcodesRemaining': [],\n",
" 'PostcodeData': [],\n",
" 'ServiceDown': False}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"incidents['Incidents'][0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"base = 'https://www.ukpowernetworks.co.uk'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def fix_incident(incident):\n",
" return {\n",
" \"url\": base + incident[\"PanelContentUrl\"],\n",
" \"postcode\": incident[\"PostcodeData\"][0][\"Postcode\"],\n",
" \"latitude\": incident[\"PostcodeData\"][0][\"Center\"][\"lat\"],\n",
" \"longitude\": incident[\"PostcodeData\"][0][\"Center\"][\"lng\"],\n",
" \"service_down\": incident[\"ServiceDown\"],\n",
" \"type\": incident[\"PowerCutType\"],\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'url': 'https://www.ukpowernetworks.co.uk/power-cut/map/unplanned?incidentId=INCD-238340-J',\n",
" 'postcode': 'DA14 4',\n",
" 'latitude': 51.43033,\n",
" 'longitude': 0.11887,\n",
" 'service_down': False,\n",
" 'type': 'Unplanned'}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fix_incident(incidents['Incidents'][1])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import sqlite_utils"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"db = sqlite_utils.Database(\"/tmp/power.db\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Table incidents (url, postcode, latitude, longitude, service_down, type)>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db[\"incidents\"].insert_all(\n",
" fix_incident(incident) for incident in incidents[\"Incidents\"] if incident[\"PostcodeData\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment