ColaboratoryでIMIコンポーネントツールの実行
!apt install jq !npm i -g npm to update !npm install https://info.gbiz.go.jp/tools/imi_tools/resource/imi-enrichment-address/imi-enrichment-address-2.0.0.tgz %%writefile index.js const enrichment = require("imi-enrichment-address") enrichment('東京都千代田区霞が関1-3-1').then(json => { console.log(JSON.stringify(json)); }); !node index.js | jq
こちらのESモジュール版だとインストール不要で簡単
%%html <script type="module"> import IMIEnrichmentAddress from "https://code4sabae.github.io/imi-enrichment-address/IMIEnrichmentAddress.mjs"; const main = async () => { const json = await IMIEnrichmentAddress("福井県鯖江市新横江2-3-4"); alert(JSON.stringify(json, null, 2)); }; main(); </script>
%%html <script type="module"> import IMIEnrichmentHojin from "https://code4sabae.github.io/imi-enrichment-hojin/IMIEnrichmentHojin.mjs"; const main = async () => { const json = await IMIEnrichmentHojin("4000012090001"); alert(JSON.stringify(json, null, 2)); }; main(); </script>
PythonでIMIコンポーネントツールの住所変換コンポーネントで変換
インストール
こちらの住所を変換
import json import pandas as pd import requests url = "http://localhost:8080" headers = {"Content-Type": "application/json"} base = {"@type": "場所型", "住所": {"@type": "住所型", "表記": ""}} result = [] with open("address.txt", encoding="utf8") as f: for line in f.readlines(): print(line) base["住所"]["表記"] = line.strip() json_data = json.dumps(base) r = requests.post(url, json_data, headers=headers) data = r.json() result.append(data) df = pd.json_normalize(result, sep="_") df.to_csv("data.csv")
気になるところ
- 町名に町がないと「町名が見つからない」
- 甲乙丙があると丁目・番地がない
- 町名が同じ丁目と番地があると番地に変換される
- 喜田村・桜井・波止浜・東村
- 番地の後に漢数字があると番地に結合される(62)
IMIコンポーネントツール
sudo apt install nodejs npm mkdir imi-enrichment-address cd imi-enrichment-address npm install https://info.gbiz.go.jp/tools/imi_tools/resource/imi-enrichment-address/imi-enrichment-address-2.0.0.tgz node node_modules/imi-enrichment-address/bin/server.js 8080
import json from pathlib import Path import requests url = "http://localhost:8080" headers = {"Content-Type": "application/json"} json_data = json.dumps({ "@type": "場所型", "住所": { "@type": "住所型", "表記": "今治市別宮町1丁目4−1" } }) r = requests.post(url, json_data, headers=headers) data = r.json() print(data) print(Path(data["住所"]["都道府県コード"]).name) print(Path(data["住所"]["市区町村コード"]).name) print(data["地理座標"]["緯度"]) print(data["地理座標"]["経度"])
富山県ステータス
import re import datetime import pathlib import pandas as pd import requests from bs4 import BeautifulSoup import jaconv def zen2han(s): result = float(jaconv.z2h(s.rstrip("人"), digit=True, ascii=True)) return result def str2date(s): y = dt_now.year m, d = map(int, re.findall("[0-9]{1,2}", s)) return pd.Timestamp(y, m, d) JST = datetime.timezone(datetime.timedelta(hours=+9), "JST") dt_now = datetime.datetime.now(JST) url = "http://www.pref.toyama.jp/cms_sec/1205/kj00022038.html" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", } r = requests.get(url, headers=headers) r.raise_for_status() soup = BeautifulSoup(r.content, "html.parser") link = soup.find("div", id="file").find("a", text="強化・緩和の判断指標(直近1週間平均)の推移").get("href") link df = pd.read_excel(link, index_col=[0, 1, 2], skiprows=2, skipfooter=3).T df.index = df.index.map(str2date) df.index.name = "日付" df.columns = ["入院者数", "重症病床稼働率", "新規陽性者数", "感染経路不明の患者数", "陽性率", "達成状況"] df["入院者数"] = df["入院者数"].apply(zen2han) df["新規陽性者数"] = df["新規陽性者数"].apply(zen2han) df["感染経路不明の患者数"] = df["感染経路不明の患者数"].apply(zen2han) df["重症病床稼働率"] = df["重症病床稼働率"].apply(lambda x: round(x * 100, 1)) df["陽性率"] = df["陽性率"].apply(lambda x: round(x * 100, 1)) # df.to_csv("toyama_status.csv", encoding="utf_8_sig") df_ori = pd.read_csv("toyama_status.csv", index_col="日付", parse_dates=True) df_csv = df_ori.reindex(df_ori.index.union(df.index)) df_csv.update(df) df_csv.sort_index(inplace=True) df_csv.to_csv("toyama_status.csv", encoding="utf_8_sig") import json df_csv["日付"] = df_csv.index.strftime("%Y-%m-%d") data = { "statusItems": df_csv.to_dict(orient="records"), } p = pathlib.Path("status.json") p.parent.mkdir(parents=True, exist_ok=True) with p.open(mode="w", encoding="utf-8") as fw: json.dump(data, fw, ensure_ascii=False, indent=4) df_csv
Github dispatch
import json import requests url = "https://api.github.com/repos/imabari/kumamoto-covid19/dispatches" headers = {"Authorization": "token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Content-Type": "application/json"} json_data = json.dumps({"event_type": "on-demand-test"}) requests.post(url, json_data, headers=headers)
# This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions name: Python application on: repository_dispatch: types: [on-demand-test] schedule: - cron: '0 11 * * *' jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.8 uses: actions/setup-python@v1 with: python-version: 3.8 - name: Cache pip uses: actions/cache@v1 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ${{ runner.os }}- - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Run script run: | python main.py - name: deploy uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./data publish_branch: gh-pages