diff --git a/.gitignore b/.gitignore index 123ae94..7a1537b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,27 +1,2 @@ -# Logs -logs -*.log - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directory -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +.idea node_modules diff --git a/lib/main.js b/lib/main.js new file mode 100644 index 0000000..34fb7f7 --- /dev/null +++ b/lib/main.js @@ -0,0 +1,72 @@ +var request = require('request'); + +function formatShowNumber(number) { + return parseInt(number) < 10 ? '0' + number : number; +} + +function searchSubtitles(show, season, episode, languages, callback) { + request({ + uri: 'http://www.addic7ed.com/search.php', + qs: { + search: show.trim() + ' ' + formatShowNumber(season) + 'x' + formatShowNumber(episode) + } + }, function (error, response, body) { + if (error || !body.trim()) { + return console.log('Error', error, body); + } + + var subs = {}, + versionRegExp = /Version (.+?),([^]+?)<\/table/g, + versionMatch, + version, + subInfoRegExp = /class="language">([^]+?)(?:most updated|Download)/g, + subInfoMatch, + lang, + notCompleted, + link, + distributionMatch, + distribution, + team; + + // Find subtitles HTML block parts + // =============================== + while ((versionMatch = versionRegExp.exec(body)) !== null) { + version = versionMatch[1].toUpperCase(); + + while ((subInfoMatch = subInfoRegExp.exec(versionMatch[2])) !== null) { + notCompleted = subInfoMatch[2]; + if (notCompleted) { + continue; + } + lang = subInfoMatch[1].substr(0, 3).toLowerCase(); + link = subInfoMatch[3]; + + if (languages && !~languages.indexOf(lang)) { + continue; + } + + subs[lang] || (subs[lang] = {}); + + distributionMatch = version.match(/WEB.DL|WEB.?RIP|BRRIP|BDRIP|BLURAY/i); + + distribution = distributionMatch + ? distributionMatch[0].toUpperCase() + .replace(/WEB.DL|WEB.?RIP/, 'WEB-DL') + .replace(/BRRIP|BDRIP|BLURAY/, 'BLURAY') + : 'HDTV'; + + subs[lang][distribution] || (subs[lang][distribution] = []); + + team = distribution === 'HDTV' ? version.replace(/.?(REPACK|PROPER|X264|HDTV|480P|720P|1080P|2160P)+.?/g, '').trim().toUpperCase() : ''; + + subs[lang][distribution].push({ + link: 'http://www.addic7ed.com' + link, + team: team, + version: version + }); + } + } + + typeof callback === 'function' && callback(subs); + }); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..8fef1e4 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "addic7ed-api", + "version": "0.0.1", + "description": "API to search and download subtitles files from addic7ed.com", + "main": "lib/main.js", + "dependencies": { + "request": "^2.67.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/same31/addic7ed-api.git" + }, + "keywords": [ + "addic7ed.com", + "subtitles", + "api" + ], + "author": "Matthieu Mitrani", + "license": "MIT", + "bugs": { + "url": "https://github.com/same31/addic7ed-api/issues" + }, + "homepage": "https://github.com/same31/addic7ed-api" +}