-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.zsh
executable file
·138 lines (125 loc) · 3.16 KB
/
linux.zsh
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
#!/bin/zsh
d=${0:a:h}/json
team=archlinuxjp
repo=slacklog
general=C0GFH3RHC
random=C0GFH3RHU
now=`date '+%Y%m01'`
date=20191201
#date=20201201
limit=200
if [ -f ./config.json ];then
token=`cat ./config.json|jq -r ".SLACK_TOKEN"`
fi
test_time(){
file=$d/20210101_general.json
if [ ! -f $file ];then
exit
fi
t=`cat $file |jq -r ".messages|.[].ts"`
n=`echo "$t"|wc -l`
for ((i=1;i<=$n;i++))
do
o=`echo "$t"|awk "NR==$i"|cut -d . -f 1`
echo $o
#date -r "$o" +"%Y-%m-%d %H:%M:%S"
date -d "@$o" +"%Y-%m-%d %H:%M:%S"
done
}
user_json(){
# https://api.slack.com/methods/users.list
url=https://slack.com/api/users.list
rm $d/user.json
if [ ! -f $d/user.json ];then
curl -X GET -H 'Content-type: application/json' "$url?token=$token&limit=1000" >! $d/user.json
fi
}
slack_history(){
if [ ! -f $json.json ];then
curl -X GET -H 'Content-type: application/json' "$url?token=$token&channel=$general&latest=$latest&oldest=$oldest&limit=$limit" |jq . >! $json.json
fi
# next-page
# 応答には、値を含む最上位のresponse_metadata属性が含まれnext_cursorます。cursor後続のリクエストでこの値をパラメータとして使用することでlimit、仮想ページごとにコレクション ページをナビゲートできます。
if [ -f $json.json ];then
n=`cat $json.json|jq -r ".response_metadata.next_cursor"`
fi
if [ "$n" != "null" ];then
i=2
while :
do
jsons=$json-$i
if [ ! -f $jsons.json ];then
curl -X GET -H 'Content-type: application/json' "$url?token=$token&channel=$general&latest=$latest&oldest=$oldest&limit=$limit&cursor=$n" |jq . >! $jsons.json
fi
if [ -f $jsons.json ];then
n=`cat $jsons.json|jq -r ".response_metadata.next_cursor"`
fi
if [ "$n" = "null" ]; then
break
fi
((i++))
done
fi
unset n
unset i
}
slack_log(){
while :
do
# https://api.slack.com/methods/conversations.history
url=https://slack.com/api/conversations.history
#oldest=`date -j -f "%Y%m%d" "$date" +%s`
#latest=`date -j -v+1m -f "%Y%m%d" "$date" +%s`
#date=`date -r $latest +"%Y%m01"`
#date_o=`date -r $oldest +"%Y%m01"`
oldest=`date -d "$date" +%s`
latest=`date -d "$date 1 months" +%s`
date=`date -d "@$latest" +"%Y%m01"`
date_o=`date -d "@$oldest" +"%Y%m01"`
json=$d/${date_o}_general
slack_history
json=$d/${date_o}_random
slack_history
if [ "$now" = "$date" ]; then
break
fi
done
}
name_rewrite(){
t=`ls $d/20*.json`
n=`echo "$t"|wc -l`
for ((i=1;i<=$n;i++))
do
tt=`echo "$t"|awk "NR==$i"`
ttt=`cat $tt |jq -r ".messages|.[].user"|sort|uniq`
if [ -n "$ttt" ];then
nn=`echo "$ttt"|wc -l`
for ((ii=1;ii<=$nn;ii++))
do
id=`echo "$ttt"|awk "NR==$ii"`
name=`cat $d/user.json|jq -r ".members|.[]|select(.id == \"$id\")|.name"`
if [ -z "$name" ];then
echo ok $tt
break
fi
if [ "$id" != "null" ] && [ -n "$name" ];then
echo $id $name
#echo sed -i \"\" \"s/$id/$name/g\" $tt
#sed -i "" "s/$id/$name/g" $tt
echo sed -i \"s/$id/$name/g\" $tt
sed -i "s/$id/$name/g" $tt
fi
done
fi
done
}
latest_json(){
f=$d/latest.json
d=`ls $d/20*.json|tail -n 1`
d=`echo ${d##*/}|cut -d _ -f 1`
echo "{\"latest\":\"$d\"}" >! $f
}
user_json
slack_log
name_rewrite
latest_json