-
Notifications
You must be signed in to change notification settings - Fork 0
/
cb.sh
executable file
·144 lines (123 loc) · 3.8 KB
/
cb.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
#!/bin/sh
# BUGS:
# if /bin/sh is dash the variable substitution ${this##*$'\n'} does not work.
# author: luc
# version: 5
version1 () {
base_url='http://crunchbanglinux.org/forums/users/-/-1/num_posts/DESC/page/'
search_url='http://crunchbanglinux.org/forums/userlist.php?username='
rss_url='http://crunchbanglinux.org/forums/feed/rss/'
CON=`printf "\033[31m"`
COFF=`printf "\033[0m"`
this=
user=
#declare -i page= count= posts=
page=
count=
posts=
# reverse info (supply a position at $2 and get the username
if [ "$1" = -r ] ; then
((page = $2 / 50 + 1, count = $2 % 50, count == 0 ? count = 50, --page : 0))
this=`curl --silent "${base_url}${page}" | sed -n '/table/,/table/{/td class="tc[02]/s/.*">\([^><]*\).*/\1/p;}' | sed -n "$((count * 2 - 1)){p;n;p;}"`
user=${this%$'\n'*}
posts=${this##*$'\n'}
elif ! curl --silent "${search_url}${1:-luc}" | grep "td class=\"tc0.*${1:-luc}"; then
echo "The user ${1:-luc} does not seem to exist." 1>&2
exit 1
else
user=${1:-luc}
while page=$(($page+1)) ; do
this=`curl --silent "${base_url}${page}/" | sed "/td class.*$user/{n;n;q;}" | sed -n '/table/,${/td class="tc[02]/s/.*">\([^><]*\).*/\1/p;}'`
echo "$this" | grep "$user" && break
done
count=$((`echo "$this" | wc -l` / 2))
#posts=${this##*$'\n'}
#posts="${posts/,}"
fi >/dev/null
#echo "$user is #$(((page - 1) * 50 + count)) with $posts posts."
echo "$user is #$(((page - 1) * 50 + count)) with ${this##*$'\n'} posts."
}
# get the list of recently active topics from CB-website. (with author, title
# and link)
#
#curl --silent $URL | \
# sed -n '/<item>$/,/<\/item>$/ {
# s/^[[:space:]]*//;
# /^<title>/ {
# s/<title><!\[CDATA\[\(.*\)\]\]><\/title>$/'"$CON"'\1'"$COFF"'/
# h
# }
# /^<link>/ {
# s/^<link>\(.*\)\/new\/posts\/<\/link>$/\1\
# /
# H
# }
# /^<author>/ {
# s/<author>.*(\(.*\)).*<\/author>$/\1: /
# G
# s/\n//
# p
# }
# } '
version2_load_vars () {
base=http://crunchbang.org/forums/userlist.php
sorted="${base}?sort_by=num_posts&sort_dir=DESC&p="
search="${base}?username="
#page="${base}?p="
default_user=luc
}
version2_clean_input () {
grep 'td class="tc[l3]"' | sed -E -e 's#(</a>){0,1}</td>$##' \
-e 's/^.*>//' \
-e 's/([0-9]+),([0-9]{3})/\1\2/' \
}
version2_search_user () {
# this function only returns an exit status but no output
# the user to search for has to be $1
curl --silent "${search}${1:-$default_user}" | \
grep "td class=\"tcl.*${1:-$default_user}" >/dev/null 2>&1
}
version2_reverse_querry () {
# we need $1
local page=$(($1 / 50 + 1))
local count=$(($1 % 50))
if [ $count -eq 0 ]; then
count=50
((page--))
fi
local this="`curl --silent "$sorted$page" | version2_clean_input | sed -n "$((count * 2 - 1)){p;n;p;}"`"
local user=${this%$'\n'*}
local posts=${this##*$'\n'}
version2_output
}
version2_output () {
#echo "$user is #$(((page - 1) * 50 + count)) with $posts posts."
echo "$user is #$(((page - 1) * 50 + count)) with ${this##*$'\n'} posts."
}
version2 () {
version2_load_vars
this=
user=
#declare -i page= count= posts=
count=
posts=
# reverse info (supply a position at $2 and get the username
if [ "$1" = -r ] ; then
version2_reverse_querry "$2"
exit
elif ! version2_search_user "$1"; then
echo "The user ${1:-$default_user} does not seem to exist." 1>&2
exit 1
else
user=${1:-$default_user}
while page=$(($page + 1)) ; do
this=`curl --silent "$sorted$page" | sed "/td class.*$user/{n;n;q;}" | version2_clean_input`
echo "$this" | grep "$user" && break
done
count=$((`echo "$this" | wc -l` / 2))
#posts=${this##*$'\n'}
#posts="${posts/,}"
fi >/dev/null
version2_output
}
version2 "$@"