Created
March 3, 2021 17:25
-
-
Save Snawoot/7ee07d713381e73ebb66e689cf4f8920 to your computer and use it in GitHub Desktop.
Google App Script for notifying about new releases of The Expanse series
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
{ | |
"timeZone": "Europe/Bucharest", | |
"dependencies": { | |
}, | |
"oauthScopes": [ | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/script.external_request", | |
"https://www.googleapis.com/auth/script.send_mail" | |
], | |
"exceptionLogging": "STACKDRIVER", | |
"runtimeVersion": "V8" | |
} |
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
const urnPrefix = "urn:btih:"; | |
const RELEASE_ENDPOINT = "http://alexfilm.org/release.php"; | |
const MAGNET_ENDPOINT = "http://alexfilm.org/ajax.php"; | |
const ID_RE = /<div\s+class="dropdown-menu"\s+aria-labelledby="1080">\s+<a\s+href="dl.php\?id=(\d+)"\s+class="dropdown-item"/mi; | |
function getAttachID(serverURL, attachID) { | |
const response = UrlFetchApp.fetch(serverURL + "?id=" + attachID); | |
const m = ID_RE.exec(response.getContentText()); | |
if (!m) { | |
throw "pattern not found!"; | |
} | |
return m[1]; | |
} | |
function getMagnetLink(serverURL, attachID) { | |
const formData = { | |
'action': 'magnet', | |
'form_token': '', | |
'attach_id': attachID | |
}; | |
const options = { | |
'method' : 'post', | |
'payload' : formData | |
}; | |
const response = UrlFetchApp.fetch(serverURL, options); | |
return JSON.parse(response.getContentText()).magnet; | |
} | |
function parseQuery(url) { | |
const query = url.split("?")[1]; | |
if (query) { | |
return query.split("&") | |
.reduce(function(o, e) { | |
var temp = e.split("="); | |
var key = temp[0].trim(); | |
var value = temp[1].trim(); | |
value = isNaN(value) ? value : Number(value); | |
if (o[key]) { | |
o[key].push(value); | |
} else { | |
o[key] = [value]; | |
} | |
return o; | |
}, {}); | |
} | |
return null; | |
} | |
function notifyOwner(magnetLink) { | |
const email = Session.getEffectiveUser().getEmail(); | |
const options = { | |
htmlBody: "<html><body>New series found: <a href=\"" + magnetLink + "\">magnet link</a></body></html>" | |
} | |
MailApp.sendEmail(email, | |
"The Expanse", | |
"New series found: " + magnetLink, | |
options); | |
Logger.log("User %s notified!", email); | |
} | |
function main() { | |
const scriptProperties = PropertiesService.getScriptProperties(); | |
Logger.log("The Expanse watcher starting..."); | |
const attachID = getAttachID(RELEASE_ENDPOINT, scriptProperties.getProperty("RELEASE_ID")); | |
Logger.log("attachID=\"%s\"", attachID); | |
const magnetLink = getMagnetLink(MAGNET_ENDPOINT, attachID); | |
Logger.log("magnet=\"%s\"", magnetLink); | |
const urn = parseQuery(magnetLink)["xt"][0]; | |
//Logger.log("urn=%s", urn); | |
let btih = null; | |
if (urn.startsWith(urnPrefix)) { | |
btih = urn.substring(urnPrefix.length) | |
} else throw "Bad URN"; | |
Logger.log("btih=%s", btih); | |
let lastBTIH = scriptProperties.getProperty("LAST_BTIH"); | |
Logger.log("Last btih = %s", lastBTIH); | |
if (btih != lastBTIH) { | |
Logger.log("New BTIH detected! Magnet link: %s", magnetLink); | |
scriptProperties.setProperty("LAST_BTIH", btih); | |
notifyOwner(magnetLink); | |
} | |
Logger.log("The Expanse watcher run finished."); | |
} | |
function trigger(e) { | |
Logger.log("Trigger activated: %s", JSON.stringify(e)); | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment