import csv
import datetime
import pathlib
import urllib.parse
import pandas as pd
import requests
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
d = {
"ST": 1,
"DA": 1,
"SC": 1,
"DC": 1,
"OF": 1,
"OW": "FB_H",
"IT": "G",
"NA": "楽天モバイル",
}
parm = urllib.parse.urlencode(d, encoding="shift-jis")
r = requests.get("https://www.tele.soumu.go.jp/musen/list", parm)
r.raise_for_status()
cr = csv.reader(r.text.splitlines(), delimiter=",")
data = list(cr)
df0 = pd.DataFrame(data[1:]).dropna(how="all")
update = datetime.datetime.strptime(data[0][0], "%Y-%m-%d").date()
len(df0) == int(data[0][1])
df1 = df0[25].str.strip().str.split(r"\\n", 2, expand=True)
se = df1.loc[df1[0].str.contains("携帯電話(その他基地局等"), 2]
df2 = (
se.str.strip()
.str.replace(r"\\n", "")
.str.extractall("(.+?)\(([0-9,]+?)\)")
.rename(columns={0: "市区町村名", 1: "開設局数"})
.reset_index(drop=True)
)
df2["市区町村名"] = df2["市区町村名"].str.strip()
df2["開設局数"] = df2["開設局数"].str.strip().str.replace(",", "").astype(int)
flag = df2["市区町村名"].str.endswith(("都", "道", "府", "県"))
df2["都道府県名"] = df2["市区町村名"].where(flag).fillna(method="ffill")
df2
df_prefs = df2[flag].reset_index(drop=True)
df_prefs
df_prefs.to_csv("prefs.csv", encoding="utf_8_sig")
df_cities = (
df2[~flag].reset_index(drop=True).reindex(columns=["都道府県名", "市区町村名", "開設局数"])
)
df_cities
df_cities.to_csv("cities.csv", encoding="utf_8_sig")
df_nara = df_cities[df_cities["都道府県名"] == "愛媛県"].copy()
df_nara["市区町村名"] = df_nara["市区町村名"].str.replace(
"^(越智郡|上浮穴郡|伊予郡|喜多郡|西宇和郡|北宇和郡|南宇和郡)", "", regex=True
)
df_nara.set_index("市区町村名", inplace=True)
df_nara.drop("都道府県名", axis=1, inplace=True)
idx = (
"松山市",
"今治市",
"宇和島市",
"八幡浜市",
"新居浜市",
"西条市",
"大洲市",
"伊予市",
"四国中央市",
"西予市",
"東温市",
"上島町",
"久万高原町",
"松前町",
"砥部町",
"内子町",
"伊方町",
"松野町",
"鬼北町",
"愛南町",
)
df_nara = df_nara.reindex(idx, fill_value=0)
df_nara["開設局数"].sum()
df_nara.loc["計"] = df_nara["開設局数"].sum()
print(update.isoformat())
dt_3d = datetime.date.today() - datetime.timedelta(days=3)
if update > dt_3d:
print("3日以内に更新されました")
p = pathlib.Path("latest.csv")
df_nara.rename(columns={"開設局数": update.isoformat()}, inplace=True)
if p.exists():
print("latest.csvを更新します")
df_last = pd.read_csv(p, index_col=0)
dt_last = datetime.date.fromisoformat(df_last.columns[0])
if update > dt_last:
print("データがアップデートされました")
df_last.to_csv(f"{df_last.columns[0]}.csv", encoding="utf_8_sig")
df_diff = df_last.join(df_nara)
df_diff["差分"] = df_diff.diff(axis=1).iloc[:, -1].astype(int)
df_diff.to_csv("diff.csv", encoding="utf_8_sig")
df3 = df_diff[df_diff["差分"] != 0]
mes = []
for i, row in df3.iterrows():
mes.append(f"{i} {row[1]}({row[2]:+})")
twit = "\n".join(mes)
print(twit)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status(twit)
else:
print("latest.csvを作成しました")
df_nara.to_csv("latest.csv", encoding="utf_8_sig")