-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·186 lines (159 loc) · 4.73 KB
/
main.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
#!/usr/bin/env sh
set -eu
USERNAME=
PASSWORD=
TITLE=
URL=
COOKIE_JAR=
usage() {
echo "Usage: $0 [-t|--title TITLE] [-u|--url URL] [-U|--username USERNAME] [-p|--password PASSWORD]"
echo "Options:"
echo " -t, --title Specify the title of the submission (defaults to \$HACKERNEWS_TITLE)"
echo " -u, --url Specify the URL of the submission (defaults to \$HACKERNEWS_URL)"
echo " -U, --username Specify the HackerNews username (defaults to \$HACKERNEWS_USERNAME)"
echo " -p, --password Specify the HackerNews password (defaults to \$HACKERNEWS_PASSWORD)"
echo ""
echo "All four arguments MUST be provided."
}
set_verbosity() {
set +u
if [ ! -z "$VERBOSE" ]; then
set -x
fi
set -u
}
prepare_cookie() {
COOKIE_JAR=$(mktemp)
}
parse_args() {
while [ $# -gt 0 ]; do
key="$1"
case $key in
-t|--title)
TITLE="$2"
shift
shift
;;
-u|--url)
URL="$2"
shift
shift
;;
-U|--username)
USERNAME="$2"
shift
shift
;;
-p|--password)
PASSWORD="$2"
shift
shift
;;
-h|--help)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
}
parse_envs() {
set +u
if [ -z "$USERNAME" ]; then
USERNAME="$HACKERNEWS_USERNAME"
fi
if [ -z "$PASSWORD" ]; then
PASSWORD="$HACKERNEWS_PASSWORD"
fi
if [ -z "$TITLE" ]; then
TITLE="$HACKERNEWS_TITLE"
fi
if [ -z "$URL" ]; then
URL="$HACKERNEWS_URL"
fi
set -u
}
validate_args() {
if [ -z "$TITLE" ] || [ -z "$URL" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
usage
exit 1
fi
}
login() {
curl -b $COOKIE_JAR -c $COOKIE_JAR 'https://news.ycombinator.com/login' \
-X POST \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
-H 'Accept-Language: en-US' \
-H 'Accept-Encoding: gzip, deflate, br, zstd' \
-H 'Referer: https://news.ycombinator.com/' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Origin: https://news.ycombinator.com' \
-H 'DNT: 1' \
-H 'Sec-GPC: 1' \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-User: ?1' \
-H 'Priority: u=1' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
-H 'TE: trailers' \
--data-raw "goto=newest&acct=${USERNAME}&pw=${PASSWORD}" \
-o /dev/null \
-v
}
submit() {
form_info=$(curl -c $COOKIE_JAR -b $COOKIE_JAR 'https://news.ycombinator.com/submitlink' -sS | pup 'input')
# <input type="hidden" name="fnid" value="RANDOM_STRING">
# <input type="hidden" name="fnop" value="submit-page">
# <input type="text" name="title" size="50" autofocus="t" oninput="tlen(this)" onfocus="tlen(this)">
# <input type="url" name="url" size="50">
# <input type="submit" value="submit">
fnid=$(echo $form_info | grep -oP 'name="fnid" value="\K[^"]+')
fnop=$(echo $form_info | grep -oP 'name="fnop" value="\K[^"]+')
submit="submit"
echo $TITLE
echo $URL
curl -L -c $COOKIE_JAR -b $COOKIE_JAR 'https://news.ycombinator.com/r' -X POST \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
-H 'Accept-Language: en-US' \
-H 'Accept-Encoding: gzip, deflate, br, zstd' \
-H 'Referer: https://news.ycombinator.com/' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Origin: https://news.ycombinator.com' \
-H 'DNT: 1' \
-H 'Sec-GPC: 1' \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-User: ?1' \
-H 'Priority: u=1' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
-H 'TE: trailers' \
--data-urlencode "fnid=$fnid" \
--data-urlencode "fnop=$fnop" \
--data-urlencode "title=$TITLE" \
--data-urlencode "url=$URL" \
-o /dev/null \
-v
}
main() {
set_verbosity
parse_args "$@"
parse_envs
validate_args
prepare_cookie
login
submit
}
main "$@"