-
Notifications
You must be signed in to change notification settings - Fork 6
/
url-normalise
executable file
·168 lines (160 loc) · 5.14 KB
/
url-normalise
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
#!/bin/bash
# Taking a list of URLs from stdin (optionally in new-viewer style wiki format), every URL is normalised as follows:
# - For social media URLs, the correct capitalisation is extracted and extraneous parameters are removed.
# - For YouTube user or channel URLs, the canonical base URL is extracted.
# - For anything else, retrieval is attempted and the final, post-redirect URL is used. (To not follow redirects, use --other-no-redirects.)
otherCurlRedirectOpt='-L'
verbose=
while [[ $# -gt 0 ]]
do
if [[ "$1" == '--other-no-redirects' ]]
then
otherCurlRedirectOpt=
elif [[ "$1" == '--verbose' || "$1" == '-v' ]]
then
verbose=1
else
echo "Unknown option: $1" >&2
exit 1
fi
shift
done
function verbose_echo {
if [[ "${verbose}" ]]
then
echo "$@"
fi
}
userAgent='Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0'
while read -r line
do
if [[ "${line}" != 'http://'* && "${line}" != 'https://'* && "${line}" != '* http://'* && "${line}" != '* https://'* ]]
then
echo "${line}"
continue
fi
if [[ "${line}" == '* '* ]]
then
prefix="${line::2}"
url="${line:2}"
else
prefix=""
url="${line}"
fi
if [[ "${url}" == *' | '* ]]
then
suffix=" | ${url#* | }"
url="${url%% | *}"
else
suffix=""
fi
# Normalise domain
if [[ "${url}" =~ ^https?://.*/ ]]
then
domain="${url#*://}"
domain="${domain%%/*}"
url="${url%%://*}://${domain,,}/${url#*://*/}"
fi
if [[ "${url}" =~ ^https?://((www|m|[a-z][a-z]-[a-z][a-z]).)?facebook.com/login/.*[?\&]next=https?%3A%2F%2F((www|m|[a-z][a-z]-[a-z][a-z]).)?facebook.com%2F && "${url}" != *'%0A'* && "${url}" != *'%00'* ]]
then
url="${url##*\?next=}"
url="${url##*&next=}"
url="${url%%&*}"
url="$(printf '%b' "${url//%/\\x}")"
fi
if [[ "${url}" =~ ^https?://((www|m|[a-z][a-z]-[a-z][a-z]).)?facebook.com/([^/]+/?(\?|$)|pages/[^/]+/[0-9]+/?(\?|$)|pages/category/[^/]+/[^/]+/?(\?|$)|pg/[^/]+([/?]|$)|profile\.php\?id=[0-9]+(&|$)) ]]
then
verbose_echo "Normalising Facebook URL: ${url}" >&2
if [[ "${url}" == *profile.php* ]]
then
url="${url%%&*}"
else
url="${url%%\?*}"
fi
page="$(curl -sL --max-time 10 -A "${userAgent}" -H 'Accept-Language: en-US,en;q=0.5' "https://www.facebook.com/${url#*facebook.com/}")"
user="$(grep -Po '<div\s[^>]*(?<=\s)data-key\s*=\s*"tab_home".*?</div>' <<< "${page}" | grep -Po '<a\s[^>]*(?<=\s)href="/\K[^/]+')"
if [[ "${user}" ]]
then
echo "${prefix}https://www.facebook.com/${user}/${suffix}"
continue
elif grep -q 'id="pagelet_loggedout_sign_up"' <<< "${page}"
then
# Profile page which is only visible when logged in
# Extract canonical URL
user="$(grep -Po '<link rel="canonical" href="\K[^"]+' <<< "${page}")"
if [[ "${user}" ]]
then
echo "${prefix}${user}${suffix}"
continue
fi
fi
echo "Failed to normalise Facebook URL: ${url}" >&2
echo "${line}"
elif [[ "${url}" =~ ^https?://(www\.)?twitter\.com/[^/]+/?(\?.*)?$ ]]
then
if [[ "${url}" =~ ^https?://(www\.)?twitter\.com/(i|web|search|hashtag)[/?] ]]
then
verbose_echo "Leaving Twitter URL alone: ${url}" >&2
echo "${line}"
continue
fi
verbose_echo "Normalising Twitter URL: ${url}" >&2
url="${url%%\?*}"
url="${url%/}"
unnormalisedUser="${url##*/}"
user="$(curl -sL --max-time 10 -A "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.18" "https://twitter.com/${unnormalisedUser}" | grep -Po '<a class="([^"]*\s)?ProfileHeaderCard-screennameLink(\s[^"]*)?" href="/\K[^/"]+(?=")')"
if [[ "${user}" ]]
then
echo "${prefix}https://twitter.com/${user}${suffix}"
else
echo "Failed to normalise Twitter URL: ${url}" >&2
echo "${line}"
fi
elif [[ "${url}" =~ ^https?://(www\.)?instagram\.com/[^/]+/?$ ]]
then
if [[ "${url}" =~ ^https?://(www\.)?instagram\.com/(p|explore)/ ]]
then
verbose_echo "Leaving Instagram URL alone: ${url}" >&2
echo "${line}"
continue
fi
verbose_echo "Normalising Instagram URL: ${url}" >&2
user="${url%/}"
user="${user##*/}"
echo "${prefix}https://www.instagram.com/${user,,}/${suffix}"
elif [[ "${url}" =~ ^https?://(www\.)?youtube\.com/ ]]
then
verbose_echo "Normalising YouTube URL: ${url}" >&2
if [[ "${url}" == *'?'* ]]
then
rurl="${url}&disable_polymer=1"
else
rurl="${url}?disable_polymer=1"
fi
page="$(curl -4sL --max-time 10 -A "${userAgent}" -H 'Accept-Language: en-US,en;q=0.5' "${rurl}")"
canonical="$(grep -Po '<link itemprop="url" href="http://www\.youtube\.com/\Kuser/[^"]+' <<< "${page}")"
if [[ "${canonical}" ]]
then
echo "${prefix}https://www.youtube.com/${canonical}${suffix}"
else
canonical="$(grep -Po '<link itemprop="url" href="http://www\.youtube\.com/\Kchannel/[^"]+' <<< "${page}")"
if [[ "${canonical}" ]]
then
echo "${prefix}https://www.youtube.com/${canonical}${suffix}"
else
echo "Failed to normalise YouTube URL: ${url}" >&2
echo "${line}"
fi
fi
else
verbose_echo "Normalising other URL: ${url}" >&2
canonical="$(curl -sS ${otherCurlRedirectOpt} --max-time 10 -A "${userAgent}" -o /dev/null -w '%{url_effective}' "${url}")"
if [[ "${canonical}" ]]
then
echo "${prefix}${canonical}${suffix}"
else
echo "Failed to normalise other URL: ${url}" >&2
echo "${line}"
fi
fi
done