From 529f5ba76c3681dc653fc6f1938dc12af9882673 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Tue, 28 Apr 2020 09:29:40 +0200 Subject: [PATCH] [scraper] simplify update seeders query --- scraper/lib/repository.js | 8 ++++++++ scraper/lib/torrentEntries.js | 12 ++---------- scraper/scheduler/seeders.js | 4 ++-- scraper/scrapers/thepiratebay/thepiratebay_api.js | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/scraper/lib/repository.js b/scraper/lib/repository.js index 7ee2062..8e230a4 100644 --- a/scraper/lib/repository.js +++ b/scraper/lib/repository.js @@ -130,6 +130,13 @@ function createTorrent(torrent) { return Torrent.upsert(torrent); } +function setTorrentSeeders(infoHash, seeders) { + return Torrent.update( + { seeders: seeders }, + { where: { infoHash: infoHash } } + ); +} + function createFile(file) { return File.upsert(file); } @@ -164,6 +171,7 @@ module.exports = { connect, getProvider, createTorrent, + setTorrentSeeders, getTorrent, getTorrentsBasedOnTitle, getUpdateSeedersTorrents, diff --git a/scraper/lib/torrentEntries.js b/scraper/lib/torrentEntries.js index bdd31a7..09e799d 100644 --- a/scraper/lib/torrentEntries.js +++ b/scraper/lib/torrentEntries.js @@ -84,19 +84,11 @@ async function getStoredTorrentEntry(torrent) { } async function updateTorrentSeeders(torrent) { - if (torrent.seeders === undefined) { + if (!torrent.infoHash || !Number.isInteger(torrent.seeders)) { return; } - return repository.getTorrent(torrent) - .catch(() => undefined) - .then(stored => { - if (stored) { - stored.seeders = torrent.seeders; - stored.changed('updatedAt', true); - return stored.save() - } - }) + return repository.setTorrentSeeders(torrent.infoHash, torrent.seeders) .catch(error => { console.warn('Failed updating seeders:', error); return undefined; diff --git a/scraper/scheduler/seeders.js b/scraper/scheduler/seeders.js index b6176fe..6ec81fe 100644 --- a/scraper/scheduler/seeders.js +++ b/scraper/scheduler/seeders.js @@ -16,8 +16,8 @@ function scheduleUpdateSeeders() { return repository.getUpdateSeedersTorrents() .then(torrents => Promise.all(torrents.map(torrent => limiter.schedule(() => _updateSeeders(torrent))))) .then(torrents => updateStatistics(torrents)) - .then(() => console.log('Finished seeders update: ', statistics)) - .catch(error => console.warn('Failed seeders update: ', error)) + .then(() => console.log('Finished seeders update:', statistics)) + .catch(error => console.warn('Failed seeders update:', error)) .then(() => delay(DELAY)) .then(() => scheduleUpdateSeeders()); } diff --git a/scraper/scrapers/thepiratebay/thepiratebay_api.js b/scraper/scrapers/thepiratebay/thepiratebay_api.js index fc4b8d2..f5114a5 100644 --- a/scraper/scrapers/thepiratebay/thepiratebay_api.js +++ b/scraper/scrapers/thepiratebay/thepiratebay_api.js @@ -71,7 +71,7 @@ const Categories = { function torrent(torrentId, retries = 2) { if (!torrentId) { - return Promise.reject(new Error('No valid torrentId provider')); + return Promise.reject(new Error('No valid torrentId provided')); } return _request(`t.php?id=${torrentId}`)