-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_delete_dialogs_with_users.sh
executable file
·104 lines (84 loc) · 2.04 KB
/
vk_delete_dialogs_with_users.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
#!/bin/bash
### INITIALIZATION ###
# List of dependencies
DEPS="jq"
# VK-related
echo -n "Reading config... "
source vk.conf && echo "done"
echo "Checking dependencies... "
for dep in $DEPS; do
if ! command -v $dep 2>&1 >/dev/null; then
echo "$dep not found, quitting."
exit 1
fi
done
echo -e "Initialization completed.\n"
### FUNCTIONS ###
function usage() {
echo -e "Usage: $(basename $0) [FILE]\nIf FILE is omitted, vk_users_from_dialogs.csv will be processed."
}
#######################################
# Delete dialog with the specified user
# Globals:
# ACCESS_TOKEN
# Arguments:
# $1 - user_id of the user
# Returns:
# None
#######################################
function delete_dialog() {
local user_id="$1"
local user_name="$(grep -oP "(?<=^${user_id},).*" $src_csv)"
echo -n "$user_name (id: $user_id)... "
response=$(
curl "https://api.vk.com/method/messages.deleteDialog?access_token=${ACCESS_TOKEN}&v=5.76&user_id=${user_id}" 2>/dev/null \
| jq .response
)
if [ $response = "1" ]; then
echo "success"
else
echo "fail"
fi
}
#######################################
# Cleanup files from the backup dir
# Globals:
# src_csv
# Arguments:
# None
# Returns:
# None
#######################################
function delete_dialogs() {
local user_id
while read user_id; do
delete_dialog $user_id
done < <(grep -oE '^[0-9]*' ${src_csv}) # Iterate over numeric user_ids from the 1st column of source CSV
}
### MAIN ###
if [ -n "$1" ]; then
src_csv="$1"
else
src_csv="vk_users_from_dialogs.csv"
fi
if [ ! -f "${src_csv}" ]; then
usage
echo "File \"${src_csv}\" was not found, quitting."
exit 1
fi
# Remove Windows line endings (CR) for compatibility
sed -i -r 's/\r//g' ${src_csv}
users_count=$(cat ${src_csv} | wc -l)
{
echo "Going to delete dialogs with ${users_count} users:"
awk -F, '{print $2 " (id: " $1 ")"}' ${src_csv}
} | more
# Ask for confirmation before proceeding
echo
read -p "Continue? [y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
delete_dialogs 2070642
fi
exit 0