-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_helpers.sh
More file actions
120 lines (102 loc) · 3.67 KB
/
api_helpers.sh
File metadata and controls
120 lines (102 loc) · 3.67 KB
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
source $(dirname "$0")/credentials.sh
url_api=https://www.thpatch.net/w/api.php
echo > token.txt
function curl_get
{
curl --no-progress-meter --retry 5 -b cookies.txt -c cookies.txt -A 'thpatch_api_script' "$url_api?format=json&$1"
}
function curl_post
{
curl --no-progress-meter --retry 5 -b cookies.txt -c cookies.txt -A 'thpatch_api_script' "$url_api" --data-urlencode format=json "$@"
}
function thpatch_login
{
curl_get "action=query&meta=tokens&type=login" | jq .query.tokens.logintoken -r | tr -s '\\\\' > token.txt
ret=$(curl_post --data-urlencode action=clientlogin --data-urlencode "username=$username" --data-urlencode "password=$password" --data-urlencode "logintoken=$(cat token.txt)" --data-urlencode 'loginreturnurl=http://example.org/')
if [ "$(echo "$ret" | jq -r .clientlogin.status)" != "PASS" ]; then
echo "FATAL ERROR: login failed" >&2
echo "$ret" | jq --color-output . >&2
exit 1
fi
curl_get "action=query&meta=tokens&type=csrf" | jq .query.tokens.csrftoken -r | tr -s '\\\\' > token.txt
}
function thpatch_csrf
{
if [ "$(cat token.txt)" = "" ]; then
thpatch_login
fi
ret=$(curl_post --data-urlencode "token=$(cat token.txt)" "$@")
if [ "$(echo "$ret" | jq -r .error.code)" = "badtoken" ]; then
thpatch_login
ret=$(curl_post --data-urlencode "token=$(cat token.txt)" "$@")
fi
echo "$ret"
}
# Usage:
# thpatch_move source destination
function thpatch_move
{
ret=$(thpatch_csrf --data-urlencode action=move --data-urlencode "from=$1" --data-urlencode "to=$2" --data-urlencode noredirect=true)
if [ "$(echo "$ret" | jq -r .move.to)" != "$2" ]; then
echo "ERROR moving $1 to $2"
echo "$ret" | jq .
fi
}
# Usage:
# thpatch_edit page_name new_content.txt
# Remove the `nocreate=true` if you want to create a page
function thpatch_edit
{
ret=$(thpatch_csrf --data-urlencode action=edit --data-urlencode "title=$1" --data-urlencode "text@$2" --data-urlencode nocreate=true)
if [ "$(echo "$ret" | jq -r .edit.result)" != "Success" ]; then
echo "ERROR editing $1"
echo "$ret" | jq .
fi
}
# Usage:
# thpatch_delete page_id additional_parameters
function thpatch_delete
{
page="$1"
shift
ret=$(thpatch_csrf --data-urlencode action=delete --data-urlencode "pageid=$page" "$@")
if [ "$(echo "$ret" | jq -r .delete)" = "null" ]; then
echo "ERROR removing page $page"
echo "$ret" | jq .
fi
}
# Usage:
# thpatch_delete_from_url page_url additional_parameters
function thpatch_delete_from_url
{
title=$(echo "$1" | sed -e 's|^https://www.thpatch.net/wiki/||')
shift
ret=$(thpatch_csrf --data-urlencode action=delete --data-urlencode "title=$title" "$@")
if [ "$(echo "$ret" | jq -r .delete)" = "null" ]; then
echo "ERROR removing page $title"
echo "$ret" | jq .
fi
}
# Usage:
# thpatch_search something
# You can add more arguments.
# For example:
# thpatch_search something&ns=1|2
# For the results, you can use this if you want the page names:
# thpatch_search something | jq -r '.query.search[]'.title
# Or if you want the page ID:
# thpatch_search something | jq -r '.query.search[]'.pageid
function thpatch_search
{
curl_get "action=query&list=search&srwhat=text&srlimit=500&srsearch=$1"
}
# Usage:
# thpatch_block_user username
function thpatch_block_user
{
ret=$(thpatch_csrf --data-urlencode action=block --data-urlencode "user=$1" --data-urlencode "reason=Spam account" --data-urlencode autoblock=true --data-urlencode noemail=true --data-urlencode allowusertalk=false)
if [ "$(echo "$ret" | jq -r .block)" = "null" ]; then
echo "ERROR blocking user $1"
echo "$ret" | jq .
fi
}