Last active
October 26, 2016 10:30
-
-
Save ChrisAlderson/bc10a08af52bb612bebc6b441e15e5a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncq from "async-q"; | |
import parseTorrent from "parse-torrent"; | |
import readTorrent from "read-torrent"; | |
import torrentHealth from "torrent-tracker-health"; | |
import Movie from "./models/Movie"; | |
import Setup from "./config/setup"; | |
import Show from "./models/Show"; | |
Setup.connectMongoDB(); | |
const convertToManget = (torrent, retry = true) => { | |
return new Promise((resolve, reject) => { | |
readTorrent(torrent, { | |
rejectUnauthorized: false | |
}, (err, result) => { | |
if (err && retry) { | |
setTimeout(() => { | |
console.log(`Retrying: ${torrent}`); | |
resolve(convertToManget(torrent, false)); | |
}, 3000); | |
} else if (err) { | |
return reject(err); | |
} else { | |
return resolve(parseTorrent.toMagnetURI(result)); | |
} | |
}); | |
}); | |
}; | |
Movie.find() | |
.then(docs => { | |
return asyncq.eachSeries(docs, doc => { | |
console.log(doc.title); | |
return asyncq.eachSeries(["720p", "1080p"], quality => { | |
if (doc.torrents["en"][quality] && doc.torrents["en"][quality].url && !doc.torrents["en"][quality].url.startsWith("magnet")) { | |
return convertToManget(doc.torrents["en"][quality].url) | |
.then(magnet => doc.torrents["en"][quality].url = magnet) | |
.catch(err => console.error(err)); | |
} | |
}).then(res => { | |
return Movie.findOneAndUpdate({ | |
_id: doc._id | |
}, doc, { | |
new: true, | |
upsert: true | |
}).exec() | |
}); | |
}); | |
}).then(res => { | |
console.log("done"); | |
process.exit(0); | |
}).catch(err => { | |
console.error(err) | |
process.exit(1); | |
}); | |
Show.find() | |
.then(docs => { | |
return asyncq.eachSeries(docs, doc => { | |
if (doc.episodes.length > 0) { | |
console.log(doc.title); | |
return asyncq.eachSeries(doc.episodes, episode => { | |
return asyncq.eachSeries(["0", "480p", "720p", "1080p"], quality => { | |
if (episode.torrents[quality] && episode.torrents[quality].url && !episode.torrents[quality].url.startsWith("magnet")) { | |
return convertToManget(episode.torrents[quality].url) | |
.then(magnet => episode.torrents[quality].url = magnet) | |
.catch(err => console.error(err)); | |
} | |
}); | |
}).then(res => { | |
return Show.findOneAndUpdate({ | |
_id: doc._id | |
}, doc, { | |
new: true, | |
upsert: true | |
}).exec() | |
}); | |
} else { | |
console.warn(`${doc.title} does not have any episodes to convert!`); | |
} | |
}); | |
}).then(res => { | |
console.log("done"); | |
process.exit(0); | |
}).catch(err => { | |
console.error(err) | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment