forked from icy/google-group-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.sh
executable file
·334 lines (298 loc) · 9.66 KB
/
crawler.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env bash
#
# Purpose: Make a backup of Google Group [Google Group Crawler]
# Author : Anh K. Huynh
# Date : 2013 Sep 22nd
# License: MIT license
#
# Copyright (c) 2013 - 2020 Ky-Anh Huynh <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# For your hack ;)
#
# Forum, list of all threads (topics), LIFO
# https://groups.google.com/forum/?_escaped_fragment_=forum/archlinuxvn
#
# Topic, list of all messages in a thread (topic), FIFO
# https://groups.google.com/forum/?_escaped_fragment_=topic/archlinuxvn/wXRTQFqBtlA
#
# Raw, a MH mail message:
# https://groups.google.com/forum/message/raw?msg=archlinuxvn/_atKwaIFVGw/rnwjMJsA4ZYJ
#
# Specification:
#
# 1. https://developers.google.com/search/docs/ajax-crawling/docs/specification
# 2. (Deprecation notice) https://webmasters.googleblog.com/2015/10/deprecating-our-ajax-crawling-scheme.html
#
# Atom link
#
# https://groups.google.com/forum/feed/archlinuxvn/msgs/atom.xml?num=100
# https://groups.google.com/forum/feed/archlinuxvn/topics/atom.xml?num=50
#
# Don't use a very big `num`. Google knows that and changes to 16.
# The bad thing is that Google doesn't provide a link to a post.
# It only provides link to a topic. Hence for two links above you
# would get the same result: links to your topics...
#
# Rss link
#
# https://groups.google.com/forum/feed/archlinuxvn/msgs/rss.xml?num=50
# https://groups.google.com/forum/feed/archlinuxvn/topics/rss.xml?num=50
#
# Rss link contains link to topic. That's great.
#
# 在我的mac上可以成功运行的版本
_short_url() {
printf '%s\n' "${*//https:\/\/groups.google.com${_ORG:+\/a\/$_ORG}\/forum\/\?_escaped_fragment_=/}"
}
_links_dump() {
# shellcheck disable=2086
echo >&2 "???????"
echo >&2 $_CURL_OPTIONS
curl \
--user-agent "$_USER_AGENT" \
$_CURL_OPTIONS \
-Lso- "$@" \
-x 127.0.0.1:4780 \
| sed -e "s#['\"]#\\"$'\n#g' \
| grep -E '^https?://' \
| sort -u
echo >&2 "???????!!!"
}
# $1: output file [/path/to/directory/prefix]
# $2: url
_download_page() {
local _f_output
local _url="$2"
local _surl=
local __
_surl="$(_short_url "$_url")"
__=0
while :; do
_f_output="$1.${__}"
if [[ -f "$_f_output" ]]; then
if [[ -n "${_FORCE:-}" ]]; then
echo >&2 ":: Updating '$_f_output' with '${_surl}'"
else
echo >&2 ":: Skipping '$_f_output' (downloaded with '${_surl}')"
if ! _url="$(grep -E -- "_escaped_fragment_=((forum)|(topic)|(categories))/$_GROUP" "$_f_output")"; then
break
fi
(( __ ++ ))
continue
fi
else
echo >&2 ":: Creating '$_f_output' with '${_surl}'"
fi
{
echo >&2 ":: Fetching data from '$_url'..."
_links_dump "$_url"
} \
| grep "https://" \
| grep "/$_GROUP" \
| awk '{print $NF}' \
> "$_f_output"
# Loop detection. See also
# https://github.com/icy/google-group-crawler/issues/24
# FIXME: 2020/04: This isn't necessary after Google has changed something
if [[ $__ -ge 1 ]]; then
if diff "$_f_output" "$1.$(( __ - 1 ))" >/dev/null 2>&1; then
echo >&2 ":: =================================================="
echo >&2 ":: Loop detected. Your cookie may not work correctly."
echo >&2 ":: You may want to generate new cookie file"
echo >&2 ":: and/or remove all '#HttpOnly_' strings from it."
echo >&2 ":: =================================================="
exit 125
fi
fi
if ! _url="$(grep -E -- "_escaped_fragment_=((forum)|(topic)|(categories))/$_GROUP" "$_f_output")"; then
break
fi
(( __ ++ ))
done
}
# Main routine
_main() {
mkdir -pv "$_D_OUTPUT"/{threads,msgs,mbox}/ 1>&2 || exit 1
echo >&2 ":: Downloading all topics (thread) pages..."
# Each page contains a bunch of
# topics sorted by time (the latest updated topic comes first.)
#
# t.0 the first page (the latest update)
# t.1 the second page
# (and so on)
#
_download_page "$_D_OUTPUT/threads/t" \
"https://groups.google.com${_ORG:+/a/$_ORG}/forum/?_escaped_fragment_=categories/$_GROUP"
echo >&2 ":: Downloading list of all messages..."
#
# Each thread (topic) file (`t.<number>`) contains a list of messages
# sorted by time (the latest updated message comes first.)
#
# t.0
# msg/m.{topic_id}.0 (the latest update)
# msg/m.{topic_id}.1
# (and so on)
#
# t.1
# msg/m.{topic_id}.0 (the latest update [in this topic])
# msg/m.{topic_id}.1
# (and so on)
#
find "$_D_OUTPUT"/threads/ -type f -iname "t.[0-9]*" -exec cat {} \; \
| grep '^https://' \
| grep "/d/topic/$_GROUP" \
| sort -u \
| sed -e 's#/d/topic/#/forum/?_escaped_fragment_=topic/#g' \
| while read -r _url; do
_topic_id="${_url##*/}"
_download_page "$_D_OUTPUT/msgs/m.${_topic_id}" "$_url"
# <--+------->
done # |
# /
# FIXME: Sorting issue here -----------'
echo >&2 ":: Gnerating command to download raw messages..."
find "$_D_OUTPUT"/msgs/ -type f -iname "m.*" -exec cat {} \; \
| grep '^https://' \
| grep '/d/msg/' \
| sort -u \
| sed -e 's#/d/msg/#/forum/message/raw?msg=#g' \
| while read -r _url; do
echo >&2 $_url
_id="$(echo "$_url"| sed -e "s#.*=$_GROUP/##g" -e 's#/#.#g')"
echo >&2 $_url
#################
# echo >&2 "__curl__ \"$_D_OUTPUT/mbox/m.${_id}\" \"$_url\""
__curl__ "$_D_OUTPUT/mbox/m.${_id}" "$_url"
#################
done
}
_rss() {
mkdir -pv "$_D_OUTPUT"/{threads,msgs,mbox}/ 1>&2 || exit 1
{
echo >&2 ":: Fetching RSS data..."
# shellcheck disable=2086
curl \
--user-agent "$_USER_AGENT" \
$_CURL_OPTIONS \
-Lso- "https://groups.google.com${_ORG:+/a/$_ORG}/forum/feed/$_GROUP/msgs/rss.xml?num=${_RSS_NUM}"
} \
| grep '<link>' \
| grep 'd/msg/' \
| sort -u \
| sed \
-e 's#<link>##g' \
-e 's#</link>##g' \
| while read -r _url; do
# shellcheck disable=SC2001
_id_origin="$(sed -e "s#.*$_GROUP/##g" <<<"$_url")"
_url="https://groups.google.com${_ORG:+/a/$_ORG}/forum/message/raw?msg=$_GROUP/$_id_origin"
_id="${_id_origin//\//.}"
echo "__curl__ \"$_D_OUTPUT/mbox/m.${_id}\" \"$_url\""
done
}
# $1: Output File
# $2: The URL
__curl__() {
if [[ ! -f "$1" ]]; then
>&2 echo ":: Downloading '$1'..."
# shellcheck disable=2086
curl -Ls \
-A "$_USER_AGENT" \
$_CURL_OPTIONS \
-x 127.0.0.1:4780 \
"$2" -o "$1"
__curl_hook "$1" "$2"
else
>&2 echo ":: Skipping '$1'..."
fi
}
# $1: Output File
# $2: The URL
__curl_hook() {
:
}
__sourcing_hook() {
# shellcheck disable=1090
source "$1" \
|| {
echo >&2 ":: Error occurred when loading hook file '$1'"
exit 1
}
}
_ship_hook() {
echo "#!/usr/bin/env bash"
echo ""
echo "export _ORG=\"\${_ORG:-$_ORG}\""
echo "export _GROUP=\"\${_GROUP:-$_GROUP}\""
echo "export _D_OUTPUT=\"\${_D_OUTPUT:-$_D_OUTPUT}\""
echo "export _USER_AGENT=\"\${_USER_AGENT:-$_USER_AGENT}\""
echo "export _CURL_OPTIONS=\"\${_CURL_OPTIONS:-$_CURL_OPTIONS}\""
echo ""
declare -f __curl_hook
if [[ -f "${_HOOK_FILE:-}" ]]; then
declare -f __sourcing_hook
echo "__sourcing_hook $_HOOK_FILE"
elif [[ -n "${_HOOK_FILE:-}" ]]; then
echo >&2 ":: ${FUNCNAME[0]}: _HOOK_FILE ($_HOOK_FILE) does not exist."
exit 1
fi
declare -f __curl__
}
_help() {
echo "Please visit https://github.com/icy/google-group-crawler for details."
}
_has_command() {
# well, this is exactly `for cmd in "$@"; do`
for cmd do
command -v "$cmd" >/dev/null 2>&1 || return 1
done
}
_check() {
local _requirements=
_requirements="curl sort awk sed diff"
# shellcheck disable=2086
_has_command $_requirements \
|| {
echo >&2 ":: Some program is missing. Please make sure you have $_requirements."
return 1
}
if [[ -z "$_GROUP" ]]; then
echo >&2 ":: Please use _GROUP environment variable to specify your google group"
return 1
fi
}
# An empty function. Can you tell me why is it?
__main__() { :; }
set -u
_ORG="${_ORG:-}"
_GROUP="${_GROUP:-}"
_D_OUTPUT="${_D_OUTPUT:-./${_ORG:+${_ORG}-}${_GROUP}/}"
# _GROUP="${_GROUP//+/%2B}"
_USER_AGENT="${_USER_AGENT:-Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Mobile Safari/537.36}"
_CURL_OPTIONS="${_CURL_OPTIONS:-}"
_RSS_NUM="${_RSS_NUM:-50}"
export _ORG _GROUP _D_OUTPUT _USER_AGENT _CURL_OPTIONS _RSS_NUM
_check || exit
case ${1:-} in
"-h"|"--help") _help;;
"-sh"|"--bash") _ship_hook; _main;;
"-rss") _ship_hook; _rss;;
*) echo >&2 ":: Use '-h' or '--help' for more details";;
esac