LoginSignup
3
1

More than 5 years have passed since last update.

Twitterで出退勤管理

Last updated at Posted at 2018-05-24

はじめに

最近、出勤時間が乱れてる、Twitterを使って出退勤管理ツールを作ろう。
ツールとしては出退勤時の何かしらのアクションを拾ってそれをトリガーにして出退勤時間をTwitterを通して知らせるという感じで。
とりあえず形にしました。所要時間約3時間。

Twitter API

インストールは以下のコマンドで

pip install requests requests_oauthlib

コンシューマーキーなどの取得は公式ページの構成が多少変わっているがこれを参照。

コード

なぐり書きですが、コードを…

tweet.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
from requests_oauthlib import OAuth1Session
import datetime
import time

CK = 'xxxxxxxx'                     # Consumer Key
CS = 'xxxxxxxx'         # Consumer Secret
AT = 'xxxxxxxx'  # Access Token
AS =  'xxxxxxxx'         # Accesss Token Secert

# ツイート投稿用のURL
url = "https://api.twitter.com/1.1/statuses/update.json"

twitter = OAuth1Session(CK, CS, AT, AS)

d=datetime.datetime

def tweet_attendant():
    att=d.now()
    atten=att.strftime("%Y/%m/%d %H:%M:%S")
    tweet = "出勤時刻は"+atten+"です。おはようございます。" #キーボード入力の取得
    params = {"status" : tweet}
    res = twitter.post(url, params = params) #post送信
    if res.status_code == 200: #正常投稿出来た場合
        print("Success.")
    else: #正常投稿出来なかった場合
        print("Failed. : %d"% res.status_code)

    writeCSV(att)

def tweet_leavework():
    leave=d.now()
    with open('output.csv', 'r') as f:
        reader = csv.reader(f)
        data = [x for x in reader]
    att="".join(data[-1])
    duration=leave-d.strptime(str(att), '%Y-%m-%d %H:%M:%S.%f')


    tweet="今日の勤務時間は"+str(duration.days)+"日と"+str(duration.seconds/3600)+"時間"+str(duration.seconds/60)+"分"+str(duration.seconds-duration.seconds/60*60)+"秒でした。お疲れ様でした。"
    print(tweet)
    params = {"status" : tweet}
    res = twitter.post(url, params = params) #post送信
    if res.status_code == 200: #正常投稿出来た場合
        print("Success.")
    else: #正常投稿出来なかった場合
        print("Failed. : %d"% res.status_code)


def writeCSV(a):
    with open('output.csv', 'r') as f:
        reader = csv.reader(f)
        data = [x for x in reader]

    with open('output.csv', 'w') as f:
        writer = csv.writer(f, lineterminator='\n')

# データをリストに保持
        csvlist=[]
        csvlist.append(a)
    # 出力
        writer.writerows(data)
        writer.writerow(csvlist)
GUI.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter
import tweet as tw


root = Tkinter.Tk()
root.title(u"Software Title")
root.geometry("400x300")

#
# ボタンが押されるとここが呼び出される
#
def Attendant(event):
    #エントリーの中身を削除
    tw.tweet_attendant()
    print("attendant")

def Leave(event):
    tw.tweet_leavework()
    print("leave")

ButtonA = Tkinter.Button(text=u'出勤', width=10)
ButtonA.bind("<Button-1>",Attendant) 
ButtonA.pack()
ButtonA.place(x=105, y=30)

ButtonB = Tkinter.Button(text=u'退勤', width=10)
ButtonB.bind("<Button-1>",Leave) 

ButtonB.pack()
ButtonB.place(x=105, y=60)

root.mainloop()

とにかく出勤時には出勤時間をoutput.csvに保存して、退勤時にはその最新の出勤時間と退勤時間との差をとって通知してます。
今回、出退勤時のアクションとしてはGUIのボタン入力にしました。

実行

Screenshot from 2018-05-24 21-49-18.png

python GUI.pyを実行するとこんな感じのウィンドウがでて、出勤を押すと"出勤時刻は2018/05/24 20:12:23です。おはようございます。",退勤を押すと"今日の勤務時間は0日と0時間16分4秒でした。お疲れ様でした。"てな感じでTweetされます。

終わりに

今はPC上で動かしているがラズパイ使って動かして出退勤時のアクションを何らかのセンサ情報にしたい。
つまり、名札をひっくり返したら通知されるてきな感じにしたい。

重大なミスがあり投稿しなおしました。コメント欄にてご指摘頂きありがとうございました。助かりました。

3
1
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
3
1