aboutsummaryrefslogtreecommitdiff
path: root/server/subscene.lua
blob: 304ffde44a43ce48e8eed1df96913d75bb6c43ec (plain) (blame)
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
#!/usr/bin/env lua

local curl = require 'lib/curl'
local util = require 'lib/util'

-- [[ languages supported by subscene ]] --
local languages = {
	['arabic'] = 2,
	['bengali'] = 54,
	['brazillian-portuguese'] = 4,
	['chinese-bg-code'] = 7,
	['czech'] = 9,
	['danish'] = 10,
	['dutch'] = 11,
	['english'] = 13,
	['farsipersian'] = 46,
	['finnish'] = 17,
	['french'] = 18,
	['german'] = 19,
	['greek'] = 21,
	['hebrew'] = 22,
	['indonesian'] = 44,
	['italian'] = 26,
	['japanese'] = 27,
	['korean'] = 28,
	['malay'] = 50,
	['norwegian'] = 30,
	['polish'] = 31,
	['portuguese'] = 32,
	['romanian'] = 33,
	['spanish'] = 38,
	['swedish'] = 39,
	['thai'] = 40,
	['turkish'] = 41,
	['vietnamese'] = 45,
	['albanian'] = 1,
	['armenian'] = 73,
	['azerbaijani'] = 55,
	['basque'] = 74,
	['belarusian'] = 68,
	['big-5-code'] = 3,
	['bosnian'] = 60,
	['bulgarian'] = 5,
	['bulgarian_english'] = 6,
	['burmese'] = 61,
	['cambodian_khmer'] = 79,
	['catalan'] = 49,
	['croatian'] = 8,
	['dutch_english'] = 12,
	['english_german'] = 15,
	['esperanto'] = 47,
	['estonian'] = 16,
	['georgian'] = 62,
	['greenlandic'] = 57,
	['hindi'] = 51,
	['hungarian'] = 23,
	['hungarian_english'] = 24,
	['icelandic'] = 25,
	['kannada'] = 78,
	['kinyarwanda'] = 81,
	['kurdish'] = 52,
	['latvian'] = 29,
	['lithuanian'] = 43,
	['macedonian'] = 48,
	['malayalam'] = 64,
	['manipuri'] = 65,
	['mongolian'] = 72,
	['nepali'] = 80,
	['pashto'] = 67,
	['punjabi'] = 66,
	['russian'] = 34,
	['serbian'] = 35,
	['sinhala'] = 58,
	['slovak'] = 36,
	['slovenian'] = 37,
	['somali'] = 70,
	['sundanese'] = 76,
	['swahili'] = 75,
	['tagalog'] = 53,
	['tamil'] = 59,
	['telugu'] = 63,
	['ukrainian'] = 56,
	['urdu'] = 42,
	['yoruba'] = 71,
}

local language = 'english'
local domain = 'https://subscene.com'
local headr = {['Cookie'] = 'LanguageFilter=' ..languages[language]}
local retries = 10

local title_search = function (key)
	local url, args, fetch, hcode, tries, title

	url = domain .. '/subtitles/searchbytitle'
	args = { '--data-raw',  'query=' .. key }

	tries = 0
	repeat
		fetch, hcode = curl.get(url, headr, args)
		tries = tries + 1
	until hcode == 200 or tries > retries

	title = fetch:match('href="/subtitles/[^"]*')
	if title then
		title = title:gsub('href="',  domain)
	end

	return title, (hcode == 200 and title ~= nil)
end

local id_fetch = function (title)
	local tab, id, name, fetch, hcode, tries, iter, line

	tries = 0
	repeat
		fetch, hcode = curl.get(title, headr, nil)
		tries = tries + 1
	until hcode == 200 or tries > retries

	tab = {}
	iter = fetch:gmatch('[^\n\r]+')
	while true do
		line = iter()
		if not line then
			break
		end

		id = line:match('/subtitles/[^/]*/[^/]*/%d*')
		if id then
			iter()
			iter()
			iter()
			iter()

			name = iter():match('%S[^\n\r]*%S')
			tab[name] = domain .. id
			id = nil
		end
	end

	return tab
end

local link_fetch = function (id)
	local fetch, tries, hcode, link

	tries = 0
	repeat
		fetch, hcode = curl.get(id, headr, nil)
		tries = tries + 1
	until hcode == 200 or tries > retries

	if hcode == 200 then
		link = domain .. fetch:match('/subtitles/[%l_-]*%-text/[^"]*')
	end

	return link, (hcode == 200)
end

local search = function (path, out)
	local title, id, link, rc, key

	key = util.string_vid_path_to_name(path)
	title, rc = title_search(key)
	if not rc then
		util.error('err: subscene: title_search')
		return false
	end

	id = id_fetch(title)
	id = util.table_match_or_any(id, key)
	if not id then
		util.error('subscene: table_match_or_any')
		return false
	end

	link, rc = link_fetch(id)
	if not rc then
		util.error('subscene: link_fetch')
		return false
	end

	rc = curl.zip_to_local_file(link, headr, out, retries)
	if not rc then
		util.error('subscene: sub_fetch')
		return false
	end

	return true
end

return {
	title_search = title_search,
	id_fetch = id_fetch,
	link_fetch = link_fetch,
	search = search,
}