diff --git a/index.js b/index.js index e22ea2c..05eb1cc 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,8 @@ var app = express(); var server = require('http').Server(app); var io = require('socket.io')(server); +const torrentTypeDetect = require('./lib/content'); + const mysqlSettings = { host : 'localhost', user : 'btsearch', @@ -89,6 +91,7 @@ io.on('connection', function(socket) filesList: row.filesList, piecelength: row.piecelength, added: row.added.getTime(), + contentCategory: row.contentCategory, } } @@ -235,6 +238,7 @@ client.on('complete', function (metadata, infohash, rinfo) { const hash = infohash.toString('hex'); let size = metadata.info.length ? metadata.info.length : 0; let filesCount = 1; + let filesArray = []; if(metadata.info.files && metadata.info.files.length > 0) { filesCount = metadata.info.files.length; @@ -253,6 +257,7 @@ client.on('complete', function (metadata, infohash, rinfo) { path: filePath, size: file.length, }; + filesArray.push(fileQ); pushDatabaseBalance(); let query = listenerMysql.query('INSERT INTO files SET ?', fileQ, function(err, result) { popDatabaseBalance(); @@ -272,6 +277,7 @@ client.on('complete', function (metadata, infohash, rinfo) { path: metadata.info.name, size: size, }; + filesArray.push(fileQ); pushDatabaseBalance(); let query = listenerMysql.query('INSERT INTO files SET ?', fileQ, function(err, result) { popDatabaseBalance(); @@ -291,6 +297,9 @@ client.on('complete', function (metadata, infohash, rinfo) { ipv4: rinfo.address, port: rinfo.port }; + + torrentTypeDetect(torrentQ, filesArray); + pushDatabaseBalance(); var query = listenerMysql.query('INSERT INTO torrents SET ? ON DUPLICATE KEY UPDATE hash=hash', torrentQ, function(err, result) { popDatabaseBalance(); diff --git a/lib/content.js b/lib/content.js new file mode 100644 index 0000000..60bfdf3 --- /dev/null +++ b/lib/content.js @@ -0,0 +1,214 @@ +const ContentTypes = { + VIDEO: 'video', + AUDIO: 'audio', + PICTURES: 'pictures', + BOOKS: 'books', + APPLICATION: 'application' +} + +const ExtesionBase = { + webm: ContentTypes.VIDEO, + mkv: ContentTypes.VIDEO, + flv: ContentTypes.VIDEO, + vob: ContentTypes.VIDEO, + ogv: ContentTypes.VIDEO, + drc: ContentTypes.VIDEO, + mng: ContentTypes.VIDEO, + avi: ContentTypes.VIDEO, + mov: ContentTypes.VIDEO, + qt: ContentTypes.VIDEO, + wmv: ContentTypes.VIDEO, + yuv: ContentTypes.VIDEO, + rm: ContentTypes.VIDEO, + rmvb: ContentTypes.VIDEO, + asf: ContentTypes.VIDEO, + amv: ContentTypes.VIDEO, + mp4: ContentTypes.VIDEO, + m4p: ContentTypes.VIDEO, + m4v: ContentTypes.VIDEO, + mpg: ContentTypes.VIDEO, + mpeg: ContentTypes.VIDEO, + mpv: ContentTypes.VIDEO, + svi: ContentTypes.VIDEO, + '3gp': ContentTypes.VIDEO, + '3g2': ContentTypes.VIDEO, + mxf: ContentTypes.VIDEO, + roq: ContentTypes.VIDEO, + nsv: ContentTypes.VIDEO, + f4v: ContentTypes.VIDEO, + + aa: ContentTypes.AUDIO, + aac: ContentTypes.AUDIO, + aax: ContentTypes.AUDIO, + act: ContentTypes.AUDIO, + aiff: ContentTypes.AUDIO, + amr: ContentTypes.AUDIO, + ape: ContentTypes.AUDIO, + au: ContentTypes.AUDIO, + awb: ContentTypes.AUDIO, + dct: ContentTypes.AUDIO, + dss: ContentTypes.AUDIO, + dvf: ContentTypes.AUDIO, + flac: ContentTypes.AUDIO, + gsm: ContentTypes.AUDIO, + iklax: ContentTypes.AUDIO, + ivs: ContentTypes.AUDIO, + m4a: ContentTypes.AUDIO, + mmf: ContentTypes.AUDIO, + mp3: ContentTypes.AUDIO, + mpc: ContentTypes.AUDIO, + msv: ContentTypes.AUDIO, + ogg: ContentTypes.AUDIO, + oga: ContentTypes.AUDIO, + opus: ContentTypes.AUDIO, + rm: ContentTypes.AUDIO, + ra: ContentTypes.AUDIO, + raw: ContentTypes.AUDIO, + sln: ContentTypes.AUDIO, + tta: ContentTypes.AUDIO, + vox: ContentTypes.AUDIO, + wav: ContentTypes.AUDIO, + wma: ContentTypes.AUDIO, + wv: ContentTypes.AUDIO, + + jpg: ContentTypes.PICTURES, + jpeg: ContentTypes.PICTURES, + exif: ContentTypes.PICTURES, + gif: ContentTypes.PICTURES, + tiff: ContentTypes.PICTURES, + bmp: ContentTypes.PICTURES, + png: ContentTypes.PICTURES, + ppm: ContentTypes.PICTURES, + pgm: ContentTypes.PICTURES, + pbm: ContentTypes.PICTURES, + pnm: ContentTypes.PICTURES, + webp: ContentTypes.PICTURES, + heif: ContentTypes.PICTURES, + bpg: ContentTypes.PICTURES, + ico: ContentTypes.PICTURES, + tga: ContentTypes.PICTURES, + cd5: ContentTypes.PICTURES, + deep: ContentTypes.PICTURES, + ecw: ContentTypes.PICTURES, + fits: ContentTypes.PICTURES, + flif: ContentTypes.PICTURES, + ilbm: ContentTypes.PICTURES, + img: ContentTypes.PICTURES, + nrrd: ContentTypes.PICTURES, + pam: ContentTypes.PICTURES, + pcx: ContentTypes.PICTURES, + pgf: ContentTypes.PICTURES, + sgi: ContentTypes.PICTURES, + sid: ContentTypes.PICTURES, + vicar: ContentTypes.PICTURES, + psd: ContentTypes.PICTURES, + cpt: ContentTypes.PICTURES, + psp: ContentTypes.PICTURES, + xcf: ContentTypes.PICTURES, + svg: ContentTypes.PICTURES, + cgm: ContentTypes.PICTURES, + cdr: ContentTypes.PICTURES, + hvif: ContentTypes.PICTURES, + odg: ContentTypes.PICTURES, + vml: ContentTypes.PICTURES, + wmf: ContentTypes.PICTURES, + + cbr: ContentTypes.BOOKS, + cbz: ContentTypes.BOOKS, + cb7: ContentTypes.BOOKS, + cbt: ContentTypes.BOOKS, + cba: ContentTypes.BOOKS, + lrf: ContentTypes.BOOKS, + lrx: ContentTypes.BOOKS, + chm: ContentTypes.BOOKS, + djvu: ContentTypes.BOOKS, + doc: ContentTypes.BOOKS, + docx: ContentTypes.BOOKS, + epub: ContentTypes.BOOKS, + pdf: ContentTypes.BOOKS, + pdb: ContentTypes.BOOKS, + fb2: ContentTypes.BOOKS, + xeb: ContentTypes.BOOKS, + ceb: ContentTypes.BOOKS, + htm: ContentTypes.BOOKS, + html: ContentTypes.BOOKS, + css: ContentTypes.BOOKS, + txt: ContentTypes.BOOKS, + ibooks: ContentTypes.BOOKS, + inf: ContentTypes.BOOKS, + azw3: ContentTypes.BOOKS, + azw: ContentTypes.BOOKS, + kf8: ContentTypes.BOOKS, + lit: ContentTypes.BOOKS, + prc: ContentTypes.BOOKS, + mobi: ContentTypes.BOOKS, + opf: ContentTypes.BOOKS, + txt: ContentTypes.BOOKS, + pdb: ContentTypes.BOOKS, + rtf: ContentTypes.BOOKS, + pdg: ContentTypes.BOOKS, + xml: ContentTypes.BOOKS, + tr2: ContentTypes.BOOKS, + tr3: ContentTypes.BOOKS, + oxps: ContentTypes.BOOKS, + xps: ContentTypes.BOOKS, + + exe: ContentTypes.APPLICATION, + apk: ContentTypes.APPLICATION, + rpm: ContentTypes.APPLICATION, + deb: ContentTypes.APPLICATION, + jar: ContentTypes.APPLICATION, + bundle: ContentTypes.APPLICATION, + com: ContentTypes.APPLICATION, + so: ContentTypes.APPLICATION, + dll: ContentTypes.APPLICATION, + elf: ContentTypes.APPLICATION, + ipa: ContentTypes.APPLICATION, + xbe: ContentTypes.APPLICATION, + xap: ContentTypes.APPLICATION, + a: ContentTypes.APPLICATION, + bin: ContentTypes.APPLICATION, +}; + +const ContentTypeProp = 'contentType'; +const ContentCategoryProp = 'contentCategory'; + +const fileDetect = (file) => { + let name = file.path.split('/').pop(); + let extension = name.split('.').pop(); + if(name.length == 0) + return; + if(extension.length == 0) + return; + + console.log(name + ':' + extension); + return ExtesionBase[extension]; +} + +const torrentTypeDetect = (torrent, files) => { + let name = torrent.name; + + let typesPriority = {}; + + for(let i = 0; i < files.length; i++) { + let file = files[i]; + let type = fileDetect(file) + + if(type) { + if(!typesPriority[type]) + typesPriority[type] = 0.; + + typesPriority[type] += file.size / torrent.size; + } + } + let priority = Object.keys(typesPriority).sort(function(a, b){ + return typesPriority[b] - typesPriority[a] + }); + if(priority.length > 0) + torrent[ContentTypeProp] = priority[0]; + + console.log(typesPriority); + console.log(priority); +} + +module.exports = torrentTypeDetect; \ No newline at end of file diff --git a/patches/patch1.js b/patches/patch1.js new file mode 100644 index 0000000..3558c54 --- /dev/null +++ b/patches/patch1.js @@ -0,0 +1,33 @@ +const mysql = require('mysql'); +const torrentTypeDetect = require('../lib/content'); + +const mysqlSettings = { + host : 'localhost', + user : 'btsearch', + password : 'pirateal100x', + database : 'btsearch' +}; + +socketMysql = mysql.createConnection(mysqlSettings); + +socketMysql.connect(function(mysqlError) { + if (mysqlError) { + console.error('error connecting: ' + mysqlError.stack); + return; + } + + socketMysql.query('SELECT * FROM `torrents` WHERE `contentType` IS NULL', function (error, rows, fields) { + rows.forEach((torrent) => { + socketMysql.query('SELECT * FROM `files` WHERE hash = ?', torrent.hash, function (error, files, fields) { + torrentTypeDetect(torrent, files); + if(torrent.contentType) { + socketMysql.query('UPDATE `torrents` SET `contentType` = ? WHERE `hash` = ?', [torrent.contentType, torrent.hash], function (error, files, fields) { + console.log(error + ':' + torrent.contentType); + }); + } + }); + }); + }); +}); + +console.log('end'); \ No newline at end of file