Jaidyn Lev authored on 2019-01-20 20:51:47
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,47 @@
1
+===============================================================================
2
+OSUBDL                                                 Fetch subs, senprobleme!
3
+===============================================================================
4
+
5
+It's kind of annoying to visit OpenSubtitle's website.
6
+First off, their site's shit.
7
+Second off, it uses Javascript.
8
+
9
+Let's get around that.
10
+With `osubdl`, you can download subtitles for a given file in the
11
+given language "automagically"~
12
+
13
+
14
+
15
+----------------------------------------
16
+PRE-REQUISITES
17
+----------------------------------------
18
+You'll need:
19
+	* To compile "oshash.h" to "oshash" and put it in your $PATH
20
+	* a POSIX-compatible shell (tested with `pdksh` and `bash`)
21
+	* "unzip" installed
22
+	* "curl" installed"
23
+	* Some videos <3
24
+
25
+
26
+
27
+----------------------------------------
28
+USAGE
29
+----------------------------------------
30
+Just run "osubdl" like so:
31
+
32
+	osubdl file [language] [destination]
33
+
34
+The file should be, obviously, some TV episode or movie's video file.
35
+The language defaults to "eng" (english), but you can choose any
36
+three-char language code.
37
+The destination defaults to the file's name, but with "srt" as the
38
+file-extension.
39
+
40
+
41
+
42
+----------------------------------------
43
+BORING STUFF
44
+----------------------------------------
45
+License is CC-0
46
+Author is Jenga Phoenix <jadedctrl@teknik.io>
47
+Sauce is at https://git.eunichx.us/osubdl
0 48
new file mode 100644
... ...
@@ -0,0 +1,34 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+
4
+/* This file is from https://github.com/tmadeira/downloadsubtitle */
5
+/* License? I don't know. ;___: */
6
+
7
+void usage(char *name) {
8
+    printf("Usage: %s <file>\n", name);
9
+    exit(1);
10
+}
11
+
12
+int main(int argc, char *argv[]) {
13
+    unsigned long long buf[16384], c = 0;
14
+    FILE *in;
15
+    int i;
16
+    if (argc != 2) {
17
+        usage(argv[0]);
18
+    }
19
+    in = fopen(argv[1], "rb");
20
+    if (in == NULL) {
21
+        usage(argv[0]);
22
+    }
23
+    fread(buf, 8192, 8, in);
24
+    fseek(in, -65536, SEEK_END);
25
+    fread(&buf[8192], 8192, 8, in);
26
+    for (i = 0; i < 16384; i++) {
27
+        c+= buf[i];
28
+    }
29
+    c+= ftell(in);
30
+    fclose(in);
31
+    printf("%016llx\n", c);
32
+    return 0;
33
+}
34
+
0 35
new file mode 100644
... ...
@@ -0,0 +1,189 @@
1
+#!/bin/sh
2
+
3
+# --------------------------------------
4
+# generic
5
+
6
+# NIL --> STRING
7
+# Read from stdin until eof hit; return all input--
8
+# good for writing functions that take piped info.
9
+function reade {
10
+	local stack=""
11
+
12
+	while read input; do
13
+		stack="$(printf '%s\n%s' "$stack" "$input")"
14
+	done
15
+
16
+	echo "$stack"
17
+}
18
+
19
+# STRING STRING --> STRING
20
+# Replace the file-extension of a filename with another.
21
+function replace_file_ext {
22
+	local filename="$1"
23
+	local ext="$2"
24
+	
25
+	local new_filename="$(echo "$filename" | sed 's%\....$%\.'"$ext"'%')"
26
+
27
+	if test "$new_filename" = "$filename"; then
28
+		echo "${filename}.${ext}"
29
+	else
30
+		echo "${new_filename}"
31
+	fi	
32
+}	
33
+
34
+
35
+
36
+#---------------------------------------
37
+
38
+# STRING --> STRING
39
+# Hash a video file.
40
+function vhash {
41
+	local file="$1"
42
+	oshash "$file"
43
+}
44
+
45
+
46
+
47
+#---------------------------------------
48
+
49
+# STRING STRING --> STRING
50
+# Get search results/subtitle-page of a file of certain hash and language.
51
+# Make sure the second argument, $lang, is set to the 3-char language code,
52
+# *not* the 2-char. I.E., "esp" over "es"
53
+function search {
54
+	local hash="$1"
55
+	local lang="$2"
56
+
57
+	local url="https://www.opensubtitles.org/en/search"
58
+	url="${url}/sublanguageid-${lang}"
59
+	url="${url}/moviehash-${hash}"
60
+
61
+	curl --silent "$url"
62
+}
63
+
64
+# |STRING --> [BOOLEAN]
65
+# Return 0 if page is a search-results; 1 if it's a subtitle page.
66
+# Sometimes, when searching for a file, it'll redirect you to the only result;
67
+# other times, it lists results. So we need this. :p
68
+function search_resultsp {
69
+	local html="$(reade)"
70
+
71
+	local result="$(echo "$html" | grep "Subtitles -" | grep -v "img wid")"
72
+
73
+	if test -z "$result"; then
74
+		return 1
75
+	else
76
+		return 0
77
+	fi
78
+}
79
+
80
+# --------------------------------------
81
+
82
+# |STRING --> STRING
83
+# Return the dl.opensubtitles URL, from the given subtitle page HTML.
84
+function subtitle_dl_url {
85
+	local html="$(reade)"
86
+
87
+	echo "$html" \
88
+	| grep "subtitleserve" \
89
+	| grep "directUrl" \
90
+	| head -1 \
91
+	| sed 's/.*=.//' \
92
+	| sed 's/.;//' \
93
+	| sed 's%.*/%%' \
94
+	| sed 's%^%https://dl.opensubtitles.org/en/download/sub/%'
95
+}
96
+
97
+# STRING --> STRING
98
+# Return the regular subtitle-page URL from the dl.opensubtitles URL.
99
+function subtitle_url {
100
+	local dl_url="$1"
101
+	local temp="$(mktemp)"
102
+
103
+	curl --verbose "$dl_url" \
104
+	2> $temp
105
+
106
+	grep "location:" $temp \
107
+	| sed 's%.*location: %%'
108
+}
109
+
110
+# --------------------------------------
111
+
112
+# STRING STRING --> NIL
113
+# Download the subtitle zip of a video, from subpage URL and dl URL.
114
+function subtitle_get {
115
+	local url="$1"
116
+	local dl_url="$2"
117
+	local dest="$3"
118
+
119
+	local temp="/tmp/$(echo "$RANDOM")"
120
+
121
+	mkdir "$temp"
122
+	local useragent='Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101'
123
+	useragent="$useragent Firefox/52.0"
124
+
125
+	# curl line shamelessly borrowed from jwilk/opensubtitles-dl <3
126
+	curl --show-error --user-agent "$useragent" --referer "$url" "$dl_url" \
127
+	> "${temp}sub.zip"
128
+	
129
+	unzip -qq -d "$temp" "${temp}/sub.zip"
130
+	mv ${temp}/*.srt "$dest"
131
+	rm -rf "${temp}"
132
+}
133
+
134
+# --------------------------------------
135
+
136
+# STRING STRING PATH --> NIL
137
+# Download a file's subtitles in given language to given path.
138
+function get_file_subs {
139
+	local file="$1"
140
+	local language="$2"
141
+	local dest="$3"
142
+
143
+	local hash="$(vhash "$file")"
144
+	local html="$(search "$hash" "$language")"
145
+
146
+	if echo "$html" | search_resultsp; then
147
+#		echo "$html"
148
+		echo "No results found."
149
+		return 3
150
+	fi
151
+
152
+	local dl_url="$(echo "$html" | subtitle_dl_url | tr -d '\r')"
153
+	local url="$(subtitle_url "$dl_url" | tr -d '\r')"
154
+
155
+	subtitle_get "$url" "$dl_url" "$dest"
156
+}
157
+
158
+
159
+
160
+# --------------------------------------
161
+# invocation
162
+
163
+function usage {
164
+	echo "usage: osubdl file [language] [path]"
165
+	echo "	'file' should be a video file."
166
+	echo "	'language' should be a three-char language code."
167
+	echo "	'path' should be the destination of the srt file."
168
+
169
+	exit 2
170
+}
171
+
172
+# --------------------------------------
173
+
174
+FILE="$1"
175
+LANGUAGE="$2"
176
+DESTINATION="$3"
177
+if test -z "$FILE"; then
178
+	usage
179
+elif test -z "$LANG"; then
180
+	LANGUAGE="eng"
181
+elif test -z "$DESTINATION"; then
182
+	DESTINATION="$(replace_file_ext "$FILE" "srt")"
183
+fi
184
+	
185
+# --------------------------------------
186
+
187
+echo "$FILE --> $DESTINATION"
188
+
189
+get_file_subs "$FILE" "$LANGUAGE" "$DESTINATION"