|
| 1 | +// Dependencies |
| 2 | +const request = require('request') |
| 3 | +const cheerio = require('cheerio') |
| 4 | +const iconv = require('iconv-lite') |
| 5 | + |
| 6 | +// Greek Encoding |
| 7 | +const encoding = 'iso-8859-7' |
| 8 | + |
| 9 | +// Base URL |
| 10 | +const URL = 'http://www.subs4free.com' |
| 11 | + |
| 12 | +// Search by query |
| 13 | +let getSubs = (query, cb) => { |
| 14 | + let searchURL = `${URL}/search_report.php?search=${query}` |
| 15 | + request(searchURL, (err, res, body) => { |
| 16 | + if (!err && res.statusCode == 200) { |
| 17 | + let $ = cheerio.load(body) |
| 18 | + |
| 19 | + // Movies List [{id, title}] |
| 20 | + let movies = [] |
| 21 | + $('select.style10 option').each(function() { |
| 22 | + let id = $(this).val().split('-').pop().split('.')[0] |
| 23 | + let title = $(this).text() |
| 24 | + let info = {id, title} |
| 25 | + movies.push(info) |
| 26 | + }) |
| 27 | + movies.shift() |
| 28 | + |
| 29 | + // Subtitles List [{lang, name, uploader, downloads, link}] |
| 30 | + let subs = [] |
| 31 | + $('.page_header table tr:nth-child(2) td table').each(function() { |
| 32 | + let lang = $(this).find('tr').first().find('td').eq(1).find('img').attr('src') != undefined ? $(this).find('tr').first().find('td').eq(1).find('img').attr('src').split('/').pop().split('.')[0] : '' |
| 33 | + let name = $(this).find('tr').first().find('td').eq(2).find('b').text().trim() != '' ? $(this).find('tr').first().find('td').eq(2).find('b').text().trim() : '' |
| 34 | + let uploader = $(this).find('tr').eq(1).find('td').first().find('table tr td b').text().trim() != '' ? $(this).find('tr').eq(1).find('td').first().find('table tr td b').text().trim() : '' |
| 35 | + let downloads = $(this).find('tr').eq(1).find('td:nth-child(2) b').text().trim() != '' ? $(this).find('tr').eq(1).find('td:nth-child(2) b').text().trim() : '' |
| 36 | + let link = $(this).find('tr').first().find('td').eq(3).find('a').attr('href') != undefined ? URL + $(this).find('tr').first().find('td').eq(3).find('a').attr('href') : '' |
| 37 | + |
| 38 | + if (lang != '' && name != '' && uploader != '' && downloads != '' && link != '') { |
| 39 | + let sub = {lang, name, uploader, downloads, link} |
| 40 | + subs.push(sub) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + // Languages [lang, lang...] |
| 45 | + let langs = Array.from(new Set(subs.map(x => x.lang))) |
| 46 | + |
| 47 | + if (movies.length > 0 && subs.length > 0) { |
| 48 | + // Results callback |
| 49 | + cb(null, {movies_count: movies.length, subs_count: subs.length, langs, movies, subs}) |
| 50 | + } else { |
| 51 | + // No movies found callback |
| 52 | + cb(null, {movies: 'No movies or subtitles were found'}) |
| 53 | + } |
| 54 | + } else { |
| 55 | + // Error callback |
| 56 | + cb(err) |
| 57 | + } |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// Search by movie id |
| 62 | +let getSubsById = (movie_id, cb) => { |
| 63 | + let searchURL = `${URL}/movie-${movie_id}.html` |
| 64 | + request({url: searchURL, encoding: null}, (err, res, body) => { |
| 65 | + if (!err && res.statusCode == 200) { |
| 66 | + body = iconv.decode(body, encoding) |
| 67 | + let $ = cheerio.load(body) |
| 68 | + |
| 69 | + // Subtitles List [{lang, name, uploader, downloads, link}] |
| 70 | + let subs = [] |
| 71 | + $('#subsTR table tr:nth-child(2) td table').each(function() { |
| 72 | + let lang = $(this).find('tr').first().find('td').eq(1).find('img').attr('src') != undefined ? $(this).find('tr').first().find('td').eq(1).find('img').attr('src').split('/').pop().split('.')[0] : '' |
| 73 | + let name = $(this).find('tr').first().find('td').eq(2).find('b').text().trim() != '' ? $(this).find('tr').first().find('td').eq(2).find('b').text().trim() : '' |
| 74 | + let uploader = $(this).find('tr').eq(1).find('td').first().find('table tr td b').text().trim() != '' ? $(this).find('tr').eq(1).find('td').first().find('table tr td b').text().trim() : '' |
| 75 | + let downloads = $(this).find('tr').eq(1).find('td:nth-child(2) b').text().trim() != '' ? $(this).find('tr').eq(1).find('td:nth-child(2) b').text().trim() : '' |
| 76 | + let link = $(this).find('tr').first().find('td').eq(3).find('a').attr('href') != undefined ? URL + $(this).find('tr').first().find('td').eq(3).find('a').attr('href') : '' |
| 77 | + |
| 78 | + if (lang != '' && name != '' && uploader != '' && downloads != '' && link != '') { |
| 79 | + let sub = {lang, name, uploader, downloads, link} |
| 80 | + subs.push(sub) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + // Languages [lang, lang...] |
| 85 | + let langs = Array.from(new Set(subs.map(x => x.lang))) |
| 86 | + |
| 87 | + if (subs.length > 0) { |
| 88 | + // Results callback |
| 89 | + cb(null, {subs_count: subs.length, langs, subs: subs}) |
| 90 | + } else { |
| 91 | + // No subtitles found callback |
| 92 | + cb(null, {subs: 'No subtitles were found'}) |
| 93 | + } |
| 94 | + } else { |
| 95 | + // Error callback |
| 96 | + cb(err) |
| 97 | + } |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +// Export Functions |
| 102 | +module.exports = {getSubs, getSubsById} |
0 commit comments