LoginSignup
0
2

More than 3 years have passed since last update.

kintoneの一日のAPIリクエスト数を監視するプログラム - Python

Last updated at Posted at 2021-04-15

定義

kintoneの使用頻度が高そうな二つのアプリの一日のAPIリクエスト数を毎朝8:55分にExcelに保存する

モジュールのインポート

import os, time, datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
import openpyxl as excel

ログインしてアプリ管理ページに飛ぶまで

driver = webdriver.Chrome()
driver.get("https://your-domain.cybozu.com/k/admin/app/index")    
usr = driver.find_element_by_name("username")
usr.send_keys("your-user-name")
pwd = driver.find_element_by_name("password")
pwd.send_keys("your-password")
pwd.submit()
time.sleep(3)

テーブルの中を取得

テーブルの値は、<tr>(行ごと)のループの中で<td>の配列のインデックスを指定することで得られる。
tds[0]がアプリID、tds[8]が一日のAPIリクエスト数。report_obj に値を保存する。
applikannri.png

tableElem = driver.find_element_by_class_name("gaia-admin-app-table-body")
trs = tableElem.find_elements_by_tag_name("tr")

report_obj = {"your-application-name1": "", "your-application-name1": ""}
for i in range(0, len(trs)):
    tds = trs[i].find_elements_by_tag_name("td")
    if tds[0].text == "1": # your-application-id1
        report_obj["your-application-name1"] = tds[8].text
    elif tds[0].text == "2": # your-application-id1
        report_obj["your-application-name2"] = tds[8].text

Excelファイルに保存する

image.png

book = excel.load_workbook(r"D:\your-folder-path\your-excel-file-name.xlsx") # rにすることでバックスラッシュをリテラルで保つ
sheet = book.active
next_row = sheet.max_row + 1

# 日時
sheet.cell(row = next_row, column = 2, value = datetime.datetime.now())
# 曜日
weekday = datetime.date.today().weekday()
jaWds = ["月", "火", "水", "木", "金", "土", "日"]
sheet.cell(row = next_row, column = 3, value = jaWds[weekday])
# your-application1
sheet.cell(row = next_row, column = 4, value = int(report_obj["your-application-name1"]))
# your-application2
sheet.cell(row = next_row, column = 5, value = int(report_obj["your-application-name2"]))

book.save(r"D:\your-folder-path\your-excel-file-name.xlsx")
print("ok")

タスクスケジューラー

このファイルをタスクスケジューラーに登録して朝8:55分に実行させる。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2