Created
June 1, 2017 12:23
-
-
Save kzyplus/bba248f6edce3908549d4ca1f0195b63 to your computer and use it in GitHub Desktop.
社内勉強会用のサンプルプログラムです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ERROR | E_WARNING | E_PARSE); | |
// -------------------------------------------- | |
// 対象日時チェック(コマンドライン引数かURLパラメータか) | |
$target_date = $_REQUEST['$target_date'] ? $_REQUEST['$target_date'] : ($argv[1] ? $argv[1] : date('Ymd')); | |
$year = substr($target_date,0,4); | |
$month = substr($target_date,4,2); | |
$day = substr($target_date,6,2); | |
if (false === checkdate($month,$day,$year)){ | |
echo "Incorrect date and time\n"; | |
exit; | |
} | |
// -------------------------------------------- | |
// 履歴データファイル(環境毎に適宜変更を) | |
$dbfile = dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/AppData/Local/Google/Chrome/user_data/Default/History"; | |
// -------------------------------------------- | |
// 既に日報生成済みならやらない | |
if ( file_exists(dirname(__FILE__).'/report_'.$year.$month.$day.'.txt') ){ | |
# echo "report exists...Done\n"; | |
# exit; | |
} | |
// -------------------------------------------- | |
// メールボックス接続 | |
$mailbox="{xxxxxxxxxx}INBOX"; | |
$user="xxxxxxxxxx"; | |
$pass="xxxxxxxxxx"; | |
$mbox = imap_open( $mailbox, $user ,$pass) or die("接続エラー\n"); | |
// -------------------------------------------- | |
// メールボックスのメッセージ数を取得 | |
$cnt=imap_num_recent($mbox); | |
$count = imap_num_msg($mbox); | |
// とりあえず100件 | |
for($idx=0;$idx<100;$idx++){ | |
$head = imap_header($mbox, $count - $idx); | |
// 受信日時 | |
$check_date =strtotime($head->date); | |
// 対象日時(定時の17:30以降の時間帯に地下鉄駅を利用したログを監視) | |
$min = mktime(17,30,0,$month,$day,$year); | |
$max = mktime(23,59,59,$month,$day,$year); | |
if ( $min <= $check_date AND $max >= $check_date) { | |
// IFTTTから送信されてきた件名「通勤メール」の有無をチェック | |
$subject = mb_decode_mimeheader($head->subject); | |
if (preg_match("/通勤メール/",$subject)){ | |
// 帰宅した!日報生成 | |
// SqliteでChromeの履歴データにアクセス | |
$db = new SQLite3($dbfile); | |
$sql= <<<__SQL__ | |
SELECT | |
datetime(last_visit_time / 1000000 + (strftime('%s', '1601-01-01') + (60*60*9)), 'unixepoch') as date, | |
title, | |
url | |
FROM urls | |
WHERE date(last_visit_time / 1000000 + (strftime('%s', '1601-01-01') + (60*60*9)),'unixepoch') = '$year-$month-$day'; | |
__SQL__; | |
$results = $db->query($sql); | |
while ($row = $results->fetchArray()) { | |
list($protocol,$dummy,$domain) = explode('/',$row['url']); | |
// ドメイン毎に集計 | |
$group[$domain][$row['date']]=$row['title']; | |
} | |
foreach($group as $domain => $value){ | |
$report .= "・".$domain.PHP_EOL; | |
foreach($value as $date => $title){ | |
$report .= " ".$date.' '.$title.PHP_EOL; | |
} | |
} | |
$db->close(); | |
$report = <<<__TEXT__ | |
--- 業務内容 --- | |
$report | |
--- 感想 / 今日の一言 | |
お疲れ様でした!!! | |
__TEXT__; | |
file_put_contents(dirname(__FILE__).'/report_'.$year.$month.$day.'.txt',$report); | |
break; | |
} | |
} | |
} | |
imap_close($mbox); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment