aboutsummaryrefslogtreecommitdiff
path: root/server/opensubtitles.lua
blob: a9f21995b80586d86d75f0f3eddfef4cc6192448 (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
#!/usr/bin/env lua

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

-- [[ languages supported by opensubtitles ]] --
local languages = {
	['english'] = 'eng',
	['abkhazian'] = 'abk',
	['afrikaans'] = 'afr',
	['albanian'] = 'alb',
	['arabic'] = 'ara',
	['aragonese'] = 'arg',
	['armenian'] = 'arm',
	['assamese'] = 'asm',
	['asturian'] = 'ast',
	['azerbaijani'] = 'aze',
	['basque'] = 'baq',
	['belarusian'] = 'bel',
	['bengali'] = 'ben',
	['bosnian'] = 'bos',
	['breton'] = 'bre',
	['bulgarian'] = 'bul',
	['burmese'] = 'bur',
	['catalan'] = 'cat',
	['chinese (simplified)'] = 'chi',
	['chinese (traditional)'] = 'zht',
	['chinese bilingual'] = 'zhe',
	['croatian'] = 'hrv',
	['czech'] = 'cze',
	['danish'] = 'dan',
	['dari'] = 'prs',
	['dutch'] = 'dut',
	['esperanto'] = 'epo',
	['estonian'] = 'est',
	['extremaduran'] = 'ext',
	['finnish'] = 'fin',
	['french'] = 'fre',
	['gaelic'] = 'gla',
	['galician'] = 'glg',
	['georgian'] = 'geo',
	['german'] = 'ger',
	['greek'] = 'ell',
	['hebrew'] = 'heb',
	['hindi'] = 'hin',
	['hungarian'] = 'hun',
	['icelandic'] = 'ice',
	['igbo'] = 'ibo',
	['indonesian'] = 'ind',
	['interlingua'] = 'ina',
	['irish'] = 'gle',
	['italian'] = 'ita',
	['japanese'] = 'jpn',
	['kannada'] = 'kan',
	['kazakh'] = 'kaz',
	['khmer'] = 'khm',
	['korean'] = 'kor',
	['kurdish'] = 'kur',
	['latvian'] = 'lav',
	['lithuanian'] = 'lit',
	['luxembourgish'] = 'ltz',
	['macedonian'] = 'mac',
	['malay'] = 'may',
	['malayalam'] = 'mal',
	['manipuri'] = 'mni',
	['marathi'] = 'mar',
	['mongolian'] = 'mon',
	['montenegrin'] = 'mne',
	['navajo'] = 'nav',
	['nepali'] = 'nep',
	['northern sami'] = 'sme',
	['norwegian'] = 'nor',
	['occitan'] = 'oci',
	['odia'] = 'ori',
	['persian'] = 'per',
	['polish'] = 'pol',
	['portuguese'] = 'por',
	['portuguese (br)'] = 'pob',
	['portuguese (mz)'] = 'pom',
	['pushto'] = 'pus',
	['romanian'] = 'rum',
	['russian'] = 'rus',
	['santali'] = 'sat',
	['serbian'] = 'scc',
	['sindhi'] = 'snd',
	['sinhalese'] = 'sin',
	['slovak'] = 'slo',
	['slovenian'] = 'slv',
	['somali'] = 'som',
	['spanish'] = 'spa',
	['spanish (eu)'] = 'spn',
	['spanish (la)'] = 'spl',
	['swahili'] = 'swa',
	['swedish'] = 'swe',
	['syriac'] = 'syr',
	['tagalog'] = 'tgl',
	['tamil'] = 'tam',
	['tatar'] = 'tat',
	['telugu'] = 'tel',
	['thai'] = 'tha',
	['toki pona'] = 'tok',
	['turkish'] = 'tur',
	['turkmen'] = 'tuk',
	['ukrainian'] = 'ukr',
	['urdu'] = 'urd',
	['uzbek'] = 'uzb',
	['vietnamese'] = 'vie',
	['welsh'] = 'wel',
}


local language = 'english'
local domain = 'https://www.opensubtitles.org'
local retries = 10

local search_ohash = function (ohash)
	local fetch, tries, hcode, url, id

	url = domain .. '/en' .. '/search/sublanguageid-' ..
	      languages[language] .. '/moviehash-' .. ohash
	tries = 0
	repeat
		fetch, hcode = curl.get(url, nil, nil)
		tries = tries + 1
	until hcode == 200 or tries > retries

	id = fetch:match('/en/subtitleserve/sub/[^\n]*\n[^\n]*iduser%-0')
	if id then
		id = id:match('/en/subtitleserve/sub/%d*')
	end

	if id then
		return domain .. id
	end
end

local search = function (path, out)
	local ohash, link

	ohash = util.opensubtitles_hash(path)
	link = search_ohash(ohash)
	if link then
		return curl.zip_to_local_file(link, nil, out, retries)
	end
end

return {
	search_ohash = search_ohash,
	search = search
}