forked from chrisniael/auto-work-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-work-report.sh
executable file
·175 lines (145 loc) · 4.05 KB
/
auto-work-report.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
URL=$1
EMAIL=$2
PASSWD=$3
function usage_info() {
echo "./auto-work-report.sh <url> <email> <passwd>"
}
if [ -z "$URL" ] || [ -z "$EMAIL" ] || [ -z "$PASSWD" ]
then
usage_info
exit 1
fi
# 去除 URL 最后一个字符 /
URL_LAST_CHAR=${URL:0-1}
if [ "$URL_LAST_CHAR" == "/" ]
then
URL=${URL%?}
fi
# login
# get today report
# if not exist then post
function login()
{
local url=$1
local cookie_file=$2
local email=$3
local passwd=$4
# Open login page
local csrf_token=$(curl -k -s --cookie-jar $cookie_file --request GET \
--url ${url}/login | grep "csrf-token" \
| awk -F '"' '{print $4}')
# Request login
curl -k -s -b $cookie_file --cookie-jar $cookie_file --request POST \
--url ${url}/login \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data "email=${email}&password=${passwd}&_token=${csrf_token}" \
| grep "home" > /dev/null
if [ $? -ne 0 ]
then
return 1
fi
return 0
}
# Get someday's report content
function get_someday_report()
{
local url=$1
local cookie_file=$2
local date_str=$3
local get_res=$(curl -k -s -b $cookie_file --cookie-jar $cookie_file \
--request GET \
--url ${url}/home?day=${date_str} \
| grep "内容")
get_res=${get_res#*内容\">}
local today_report_content=$(echo ${get_res%</textarea>} | awk '{$1=$1;print}')
echo $today_report_content
return 0
}
function write_report()
{
local url=$1
local cookie_file=$2
local date_str=$3
local today_report_content=$4
# Visit home page
csrf_token=$(curl -k -s -b $cookie_file --cookie-jar $cookie_file \
--request GET \
--url ${url}/home | grep "csrf-token" \
| awk -F '"' '{print $4}')
echo "[trace] csrf_token=$csrf_token"
# Post report
curl -k -s -b $cookie_file --cookie-jar $cookie_file --request POST \
--url ${url}/service/note/save \
--header "X-CSRF-TOKEN: ${csrf_token}" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data "day=${date_str}&content=${today_report_content}" | grep "return_message\":\"success" > /dev/null
return 0
}
cookie_file=$(mktemp)
echo "[info] temp file, name=$cookie_file"
login $URL $cookie_file $EMAIL $PASSWD
login_ret_code=$?
if [ $login_ret_code -ne 0 ]
then
echo "[error] login failed!"
# Remove temp file
rm -rf $cookie_file
exit $login_ret_code
else
echo "[info] login success."
fi
# Get today date
date_str=$(date +%Y-%m-%d)
# 星期几,1 代表星期一
weekday_n=$(date +%u)
echo "[info] today, date=$date_str, weekday_n=$weekday_n"
if [ $weekday_n -ge 6 ]
then
echo "[info] today is weekend, don't need work report."
# Remove temp file
rm -rf $cookie_file
exit 0
fi
# Get today report content
today_report_content=$(get_someday_report $URL $cookie_file $date_str)
echo "[info] today report, date=$date_str, content=$today_report_content, size=${#today_report_content}"
if [ ${#today_report_content} -gt 0 ]
then
echo "[warning] today has written the work report, don't need report again."
# Remove temp file
rm -rf $cookie_file
exit 0
fi
# Get report content in the past
today_report_content=
yesterday_date_str=$date_str
for i in {1..7}
do
yesterday_timestamp=$(date +%s -d "${yesterday_date_str}")
yesterday_timestamp=$((yesterday_timestamp-24*60*60))
yesterday_date_str=$(date +%Y-%m-%d -d @${yesterday_timestamp})
past_report_content=$(get_someday_report $URL $cookie_file $yesterday_date_str)
if [ ${#past_report_content} -gt 0 ]
then
echo "[info] find past report, date_str=${yesterday_date_str}, content=$past_report_content, size=${#past_report_content}"
today_report_content=${past_report_content}
break
fi
done
if [ -z "$today_report_content" ]
then
echo "[error] could not find previous work report, date=$date_str"
# Remove temp file
rm -rf $cookie_file
exit 1
fi
write_report $URL $cookie_file $date_str "$today_report_content"
if [ $? -eq 0 ]
then
echo "[info] writ work report success, date=$date_str, content=$today_report_content"
else
echo "[error] writ work report failed, date=$date_str"
fi
# Remove temp file
rm -rf $cookie_file