ページ編集記録
#public
Googleデータポータル
Googleデータポータルの完成形
https://lookerstudio.google.com/reporting/83e09ddc-4565-442e-81bd-a9df4833df4f
これは私のPrivateプロジェクトの記録を可視化したものである
Scrapboxプロジェクト管理
https://scrapbox.io/files/641987ff447b36001b3d435e.png
以下を可視化
累計ページ数推移
ページ作成数推移
ページ更新数推移
言葉は磨くもの
ページを作成するではなく、更新していくことが重要
前提
Macbook
Scrapboxプロジェクト管理と同じような方法で実現している
手順
1. 以下のページに従ってGCPとGoogle SpreadSheetの準備を行う。
https://www.teijitaisya.com/python-gsheets/#index_id0
GCPで新しいプロジェクトを作成
Google Drive APIとGoogle Sheets APIを追加
サービスアカウントの作成(ロールは編集者)
JSON秘密鍵ファイルを作成して、Client Emailをコピー、Jsonファイルのpathを取得
Spreadsheetを作成してClient Emailに共有
SPREADSHEET_KEYを取得
コードに追記する情報
Jsonファイルのpath
SPREADSHEET_KEY
2. Goole データポータルでグラフの準備をする
Looker Studioに登録
https://lookerstudio.google.com/
スプシと接続
グラフを作成
記事数
記事数推移
更新数、作成数推移
3. 実行ファイルを準備する
sc2ssファイルを作成。このファイルを実行するとSpreadSheetからGoogleデータポータルにデータが反映される
code: folder構成
├── exportedフォルダ・・・ここにexportしたjsonファイルを入れる
├── outputフォルダ・・・ここに処理後のファイルが入る
└── sc2ss.py・・・処理コード
code:sc2ss.py
import glob
import os
import copy
import json
import time
from time import strftime
import pandas as pd
import datetime as dt
from datetime import datetime
import gspread
from google.oauth2.service_account import Credentials
import gspread
from gspread_dataframe import get_as_dataframe, set_with_dataframe
exported_path="xxxxxx" #exportedフォルダのpath
list_of_files = glob.glob(exported_path+'*')
latest_file = max(list_of_files, key=os.path.getctime)
a = open(latest_file)#ファイル読み込み
b = json.load(a)
title_list=[date"title" for date in b"pages"]
created_date_str_list=[strftime("%Y-%m-%d", time.localtime(date"created")) for date in b"pages"]
updated_date_str_list=[strftime("%Y-%m-%d", time.localtime(date"updated")) for date in b"pages"]
created_date_list=datetime.strptime(i, '%Y-%m-%d') for i in created_date_str_list
updated_date_list=datetime.strptime(i, '%Y-%m-%d') for i in updated_date_str_list
df=pd.DataFrame()
df"title"=title_list
df"created"=created_date_list
df"updated"=updated_date_list
start_ts=df.groupby("created").count().index0
end_ts=df.groupby("updated").count().index-1
start_str = start_ts.strftime("%Y-%m-%d")
end_str = end_ts.strftime("%Y-%m-%d")
def list_date(start_date='2020-01-01', end_date='2020-01-31'):
# str型をdatetime型に変換する.
start_date =dt.datetime.strptime(start_date, '%Y-%m-%d')
end_date = dt.datetime.strptime(end_date, '%Y-%m-%d')
# 開始日と終了日の間隔を計算する.
days = (end_date - start_date).days
# 間隔の分だけfor文を回し,日を追加する.
tmp_date = start_date
date = tmp_date
for i in range(days):
tmp_date += dt.timedelta(days=1)
date.append(tmp_date)
# date_str=i.strftime("%Y-%m-%d") for i in date
return date
date_list = list_date(start_str, end_str)
df_date=pd.DataFrame()
df_date"date"=date_list
df_date=df_date.set_index("date")
df_created=pd.DataFrame()
df_created"date"=list(df.groupby("created").count().index)
df_created"作成数"=list(df.groupby("created").count()"title")
df_created=df_created.set_index("date")
df_updated=pd.DataFrame()
df_updated"date"=list(df.groupby("updated").count().index)
df_updated"更新数"=list(df.groupby("updated").count()"title")
df_updated=df_updated.set_index("date")
df_date=df_date.join(df_created, df_updated).fillna(0)
created_num_list=[]
num=0
for i in list(df_date"作成数"):
num=num+i
created_num_list.append(num)
df_date"ページ数"=created_num_list
df_date=df_date.reset_index()
# お決まりの文句
# 2つのAPIを記述しないとリフレッシュトークンを3600秒毎に発行し続けなければならない
scope = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
#ダウンロードしたjsonファイル名をクレデンシャル変数に設定。
json_path="xxxxxxxx"
credentials = Credentials.from_service_account_file(json_path, scopes=scope)
#OAuth2の資格情報を使用してGoogle APIにログイン。
gc = gspread.authorize(credentials)
#スプレッドシートIDを変数に格納する。
SPREADSHEET_KEY = 'xxxxxxxx'
# スプレッドシート(ブック)を開く
workbook = gc.open_by_key(SPREADSHEET_KEY)
# スプレッドシート(ブック)を開く
workbook = gc.open_by_key(SPREADSHEET_KEY)
# シートの一覧を取得する。(リスト形式)
worksheets = workbook.worksheets()
print(worksheets)
# シートを開く
worksheet = workbook.worksheet('シート1')
# sheetに反映
set_with_dataframe(worksheet, df_date)
4. Automatorでexportedフォルダにフォルダアクションを設定
Automatorの"シェルスクリプトを実行"で以下を設定
python sc2ss.pyのPATH
https://liginc.co.jp/osora/archives/416