summarylogtreecommitdiffstats
path: root/ysubs.js
blob: a1c0db04d84f5449c3e2467cfd6e744a2a8703ad (plain)
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
(function (context) {
    'use strict';

    var App = context.App ? context.App : context;
    var _ = require('underscore');
    var request = require('request');
    var Q = require('q');

    var baseUrl = 'http://api.yifysubtitles.com/subs/';
    var mirrorUrl = 'http://api.ysubs.com/subs/';
    var prefix = 'http://www.yifysubtitles.com';

    var inherits = require('util').inherits;

    var TTL = 1000 * 60 * 60 * 4; // 4 hours
    var YSubs = function () {
        if (!(this instanceof YSubs)) {
            return new YSubs();
        }

        App.Providers.CacheProvider.call(this, 'subtitle', TTL);
    };
    
        // Language mapping to match PT langcodes
    var languageMapping = {
        'albanian': 'sq',
        'arabic': 'ar',
        'bengali': 'bn',
        'brazilian-portuguese': 'pt-br',
        'bulgarian': 'bg',
        'bosnian': 'bs',
        'chinese': 'zh',
        'croatian': 'hr',
        'czech': 'cs',
        'danish': 'da',
        'dutch': 'nl',
        'english': 'en',
        'estonian': 'et',
        'farsi-persian': 'fa',
        'finnish': 'fi',
        'french': 'fr',
        'german': 'de',
        'greek': 'el',
        'hebrew': 'he',
        'hungarian': 'hu',
        'indonesian': 'id',
        'italian': 'it',
        'japanese': 'ja',
        'korean': 'ko',
        'lithuanian': 'lt',
        'macedonian': 'mk',
        'malay': 'ms',
        'norwegian': 'no',
        'polish': 'pl',
        'portuguese': 'pt',
        'romanian': 'ro',
        'russian': 'ru',
        'serbian': 'sr',
        'slovenian': 'sl',
        'spanish': 'es',
        'swedish': 'sv',
        'thai': 'th',
        'turkish': 'tr',
        'urdu': 'ur',
        'ukrainian': 'uk',
        'vietnamese': 'vi'
    };



    YSubs.prototype = Object.create(App.Providers.CacheProvider.prototype);
    YSubs.prototype.constructor = YSubs;
    YSubs.prototype.config = {
        name: 'ysubs',
        type: 'subtitle'
    };

    var querySubtitles = function (imdbIds) {
        if (_.isEmpty(imdbIds)) {
            return {};
        }

        var url = baseUrl + _.map(imdbIds.sort(), function (id) {
            return id;
        }).join('-');
        var mirrorurl = mirrorUrl + _.map(imdbIds.sort(), function (id) {
            return id;
        }).join('-');

        var deferred = Q.defer();

        request({
            url: url,
            json: true
        }, function (error, response, data) {
            if (error || response.statusCode >= 400 || !data || !data.success) {
                request({
                    url: mirrorurl,
                    json: true
                }, function (error, response, data) {
                    if (error || response.statusCode >= 400 || !data || !data.success) {
                        deferred.reject(error);
                    } else {
                        deferred.resolve(data);
                    }
                });
            } else {
                deferred.resolve(data);
            }
        });

        return deferred.promise;
    };

    var formatForPopcorn = function (data) {
        var allSubs = {};
        // Iterate each movie
        _.each(data.subs, function (langs, imdbId) {
            var movieSubs = {};
            // Iterate each language
            _.each(langs, function (subs, lang) {
                // Pick highest rated
                var langCode = languageMapping[lang];
                movieSubs[langCode] = prefix + _.max(subs, function (s) {
                    return s.rating;
                }).url;
            });

            // Remove unsupported subtitles
            var filteredSubtitle = App.Localization.filterSubtitle(movieSubs);

            allSubs[imdbId] = filteredSubtitle;
        });

        return Common.sanitize(allSubs);
    };

    YSubs.prototype.query = function (ids) {
        return Q.when(querySubtitles(ids))
            .then(formatForPopcorn);
    };



    App.Providers.install(YSubs);
})(window);