soundcloud-key-fetch
Advanced tools
Comparing version 1.0.11 to 1.0.12
133
index.js
@@ -1,59 +0,90 @@ | ||
exports.fetchKey = async function() { | ||
return new Promise(async function(resolve, rej) { | ||
require('https').get('https://soundcloud.com/', (res) => { | ||
let r = '' | ||
res.on('data', async (d) => { | ||
r += d | ||
}) | ||
res.on('end', async () => { | ||
const res = r.split('<script crossorigin src="'); | ||
const urls = []; | ||
res.forEach(urlA => { | ||
const urlreg = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/ | ||
let url = urlA.replace('"></script>', ''); let res = url.split('\n')[0]; | ||
if(urlreg.test(res)) urls.push(res); | ||
exports.fetchKey = async function () { | ||
return new Promise(async function (resolve, rej) { | ||
require("https") | ||
.get("https://soundcloud.com/", (res) => { | ||
let r = ""; | ||
res.on("data", async (d) => { | ||
r += d; | ||
}); | ||
async function fetchKey() { | ||
return new Promise(async function(resolve, rej) { | ||
let key; | ||
require('https').get(urls[urls.length - 1], async a => { | ||
let data = '' | ||
a.on('data', async d => { | ||
data += d | ||
}) | ||
a.on('end', () => { | ||
if(data.includes(',client_id:"')) { | ||
const thingA = data.split(',client_id:"'); | ||
key = thingA[1].split('"')[0]; | ||
return resolve(key) | ||
res.on("end", async () => { | ||
const res = r.split('<script crossorigin src="'); | ||
const urls = []; | ||
res.forEach((urlA) => { | ||
const urlreg = | ||
/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/; | ||
let url = urlA.replace('"></script>', ""); | ||
let res = url.split("\n")[0]; | ||
if (urlreg.test(res)) urls.push(res); | ||
}); | ||
async function fetchKey() { | ||
return new Promise(async function (resolve, rej) { | ||
let key; | ||
require("https").get( | ||
urls[urls.length - 1], | ||
async (a) => { | ||
let data = ""; | ||
a.on("data", async (d) => { | ||
data += d; | ||
}); | ||
a.on("end", () => { | ||
if (data.includes(',client_id:"')) { | ||
const thingA = | ||
data.split(',client_id:"'); | ||
key = thingA[1].split('"')[0]; | ||
return resolve(key); | ||
} | ||
return resolve(""); | ||
}); | ||
a.on("error", (err) => rej(err)); | ||
} | ||
return resolve('') | ||
}) | ||
a.on('error', () => rej(err)) | ||
); | ||
}); | ||
}) | ||
} | ||
const key = await fetchKey() | ||
if(key) {resolve(key)} else {rej(new Error(`Unable to fetch a SoundCloud API key! This most likely has happened due to either making too many requests, or the SoundCloud website has changed!`))} | ||
} | ||
const key = await fetchKey(); | ||
if (key) { | ||
resolve(key); | ||
} else { | ||
rej( | ||
new Error( | ||
`Unable to fetch a SoundCloud API key! This most likely has happened due to either making too many requests, or the SoundCloud website has changed!` | ||
) | ||
); | ||
} | ||
}); | ||
}) | ||
}).on('error', async e => { | ||
rej(e) | ||
}) | ||
}) | ||
.on("error", async (e) => { | ||
rej(e); | ||
}); | ||
}); | ||
}; | ||
exports.testKey = function(key) { | ||
return new Promise(function(res, rej) { | ||
if(!key) {return rej(new Error(`No SoundCloud API key provided`))} else { | ||
require('https').get(`https://api-v2.soundcloud.com/search?client_id=${key}&q=this%20package%20gave%20me%20neck%20pains&limit=0`, async (result) => { | ||
if(result.statusCode === 401 || result.statusCode === 403) return res(false); | ||
if(result.statusCode === 404 || result.statusCode === 200) return res(true) | ||
}).on('error', e => { | ||
return rej(e) | ||
}) | ||
exports.testKey = function (key) { | ||
return new Promise(function (res, rej) { | ||
if (!key) { | ||
return rej(new Error(`No SoundCloud API key provided`)); | ||
} else { | ||
require("https") | ||
.get( | ||
`https://api-v2.soundcloud.com/search?client_id=${key}&q=this%20package%20gave%20me%20neck%20pains&limit=0`, | ||
async (result) => { | ||
if ( | ||
result.statusCode === 401 || | ||
result.statusCode === 403 | ||
) | ||
return res(false); | ||
if ( | ||
result.statusCode === 404 || | ||
result.statusCode === 200 | ||
) | ||
return res(true); | ||
} | ||
) | ||
.on("error", (e) => { | ||
return rej(e); | ||
}); | ||
} | ||
}) | ||
} | ||
}); | ||
}; |
@@ -27,3 +27,3 @@ { | ||
}, | ||
"version": "1.0.11" | ||
"version": "1.0.12" | ||
} |
42
test.js
let sckey; | ||
console.log(`| Testing scraper...`); | ||
try { | ||
sckey = require('soundcloud-key-fetch') | ||
} catch(err) { return console.log(`| Failed! ${err}`) }; | ||
sckey = require("soundcloud-key-fetch"); | ||
} catch (err) { | ||
return console.log(`| Failed! ${err}`); | ||
} | ||
sckey.fetchKey().then(key => { | ||
console.log(`| Fetched key! [${key}]`); | ||
console.log(`| Testing key validity`); | ||
sckey.testKey(key).then(result => { | ||
if(result) { | ||
return console.log(`| Key successfully fetched and is valid!`) | ||
} else { | ||
return console.log(`| Key fetched; unable to validate it.`) | ||
} | ||
}).catch(e => { | ||
return console.log(`| Failed to test key validity!`, e) | ||
sckey | ||
.fetchKey() | ||
.then((key) => { | ||
console.log(`| Fetched key! [${key}]`); | ||
console.log(`| Testing key validity`); | ||
sckey | ||
.testKey(key) | ||
.then((result) => { | ||
if (result) { | ||
return console.log( | ||
`| Key successfully fetched and is valid!` | ||
); | ||
} else { | ||
return console.log(`| Key fetched; unable to validate it.`); | ||
} | ||
}) | ||
.catch((e) => { | ||
return console.log(`| Failed to test key validity!`, e); | ||
}); | ||
}) | ||
}).catch(e => { | ||
return console.log(`| Failed to fetch a key!`, e) | ||
}) | ||
.catch((e) => { | ||
return console.log(`| Failed to fetch a key!`, e); | ||
}); |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
7186
116