forked from icy/google-group-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.sh
executable file
·276 lines (248 loc) · 7.7 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
#!/bin/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 - 2015 Anh K. Huynh <kyanh@theslinux.org>
#
# 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
#
# 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.
#
_short_url() {
printf '%s\n' "${*//https:\/\/groups.google.com\/forum\/\?_escaped_fragment_=/}"
}
_links_dump() {
wget \
--user-agent="$_USER_AGENT" \
$_WGET_OPTIONS \
-O- "$@" \
| sed -e "s#['\"]#\\"$'\n#g' \
| grep -E '^https?://' \
| sort -u
}
# $1: output file [/path/to/directory/prefix]
# $2: url
_download_page() {
local _f_output
local _url="$2"
local __
__=0
while :; do
_f_output="$1.${__}"
if [[ -f "$_f_output" ]]; then
if [[ -n "${_FORCE:-}" ]]; then
echo >&2 ":: Updating '$_f_output' with '$(_short_url $_url)'"
else
echo >&2 ":: Skipping '$_f_output' (downloaded with '$(_short_url $_url)')"
if ! _url="$(grep -E -- "_escaped_fragment_=((forum)|(topic))/$_GROUP" "$_f_output")"; then
break
fi
(( __ ++ ))
continue
fi
else
echo >&2 ":: Creating '$_f_output' with '$(_short_url $_url)'"
fi
{
echo >&2 ":: Fetching data from '$_url'..."
_links_dump "$_url"
} \
| grep "https://" \
| grep "/$_GROUP" \
| awk '{print $NF}' \
> "$_f_output"
if ! _url="$(grep -E -- "_escaped_fragment_=((forum)|(topic))/$_GROUP" "$_f_output")"; then
break
fi
(( __ ++ ))
done
}
# Main routine
_main() {
mkdir -pv "$_D_OUTPUT"/{threads,msgs,mbox}/ 1>&2 || exit 1
# Download 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/forum/?_escaped_fragment_=forum/$_GROUP"
# Download 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 _url; do
_topic_id="${_url##*/}"
_download_page "$_D_OUTPUT/msgs/m.${_topic_id}" "$_url"
# <--+------->
done # |
# /
# FIXME: Sorting issue here -----------'
# Download all 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 _url; do
_id="$(echo "$_url"| sed -e "s#.*=$_GROUP/##g" -e 's#/#.#g')"
echo "__wget__ \"$_D_OUTPUT/mbox/m.${_id}\" \"$_url\""
done
}
_rss() {
mkdir -pv "$_D_OUTPUT"/{threads,msgs,mbox}/ 1>&2 || exit 1
{
echo >&2 ":: Fetching RSS data..."
wget \
--user-agent="$_USER_AGENT" \
$_WGET_OPTIONS \
-O- "https://groups.google.com/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 _url; do
_id_origin="$(echo "$_url"| sed -e "s#.*$_GROUP/##g")"
_url="https://groups.google.com/forum/message/raw?msg=$_GROUP/$_id_origin"
_id="${_id_origin//\//.}"
echo "__wget__ \"$_D_OUTPUT/mbox/m.${_id}\" \"$_url\""
done
}
# $1: Output File
# $2: The URL
__wget__() {
if [[ ! -f "$1" ]]; then
wget \
--user-agent="$_USER_AGENT" \
$_WGET_OPTIONS \
"$2" -O "$1"
__wget_hook "$1" "$2"
fi
}
# $1: Output File
# $2: The URL
__wget_hook() {
:
}
__sourcing_hook() {
source "$1" \
|| {
echo >&2 ":: Error occurred when loading hook file '$1'"
exit 1
}
}
_ship_hook() {
echo "#!/usr/bin/env bash"
echo ""
echo "export _GROUP=\"\${_GROUP:-$_GROUP}\""
echo "export _D_OUTPUT=\"\${_D_OUTPUT:-$_D_OUTPUT}\""
echo "export _USER_AGENT=\"\${_USER_AGENT:-$_USER_AGENT}\""
echo "export _WGET_OPTIONS=\"\${_WGET_OPTIONS:-$_WGET_OPTIONS}\""
echo ""
declare -f __wget_hook
if [[ -f "${_HOOK_FILE:-/x/y/z/t/u/v/m}" ]]; then
declare -f __sourcing_hook
echo "__sourcing_hook $_HOOK_FILE"
fi
declare -f __wget__
}
_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() {
_has_command wget sort awk sed \
|| {
echo >&2 ":: Some program is missing. Please make sure you have sort, wget, sed and awk"
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
export _GROUP="${_GROUP:-}"
export _D_OUTPUT="${_D_OUTPUT:-./$_GROUP/}"
export _USER_AGENT="${_USER_AGENT:-Mozilla/5.0 (X11; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0}"
export _WGET_OPTIONS="${_WGET_OPTIONS:-}"
export _RSS_NUM="${_RSS_NUM:-50}"
_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