Last active
December 2, 2021 15:29
-
-
Save mxvin/fdbe8d92bb0e5a6982d149f4a8c116c5 to your computer and use it in GitHub Desktop.
Import subreddit plaintext list to infinity - for - reddit client (https://github.com/Docile-Alligator/Infinity-For-Reddit) anonymous subscriptions database
This file contains 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
var fs = require('fs') | |
var path = require('path') | |
var https = require('https'); | |
var subNameFile = "test.txt" | |
var result = []; | |
var errorListArr = []; | |
var errorReasonListArr = []; | |
async function run (){ | |
let rawSubName = fs.readFileSync(path.join(__dirname,subNameFile)).toString() | |
// todo: unix/dos newline detection (maybe not consider this is just hack script :|) | |
// parse file to array | |
let subNameArr = rawSubName.split("\r\n") | |
subNameArr = subNameArr.filter(a=>a!=="") | |
subNameArr = [... new Set(subNameArr)] | |
console.log("Entity total: %s", subNameArr.length) | |
for(entity of subNameArr){ | |
// space removal | |
entity = entity.replace(/\s+/g, ''); | |
let entityJson = await fetchEntity(entity) | |
if(! entityJson.error){ | |
createEntity(entityJson) | |
console.log("Done entity %s",entity) | |
}else{ | |
errorListArr.push(entity) | |
errorReasonListArr.push(entityJson.reason) | |
console.log("Error entity %s [%s]",entity, entityJson?.reason) | |
} | |
} | |
// write finished result to file | |
fs.writeFileSync(path.join(__dirname,subNameFile+"_result.json"), | |
JSON.stringify(result) | |
) | |
if( errorListArr.length > 0 ){ | |
fs.writeFileSync(path.join(__dirname,subNameFile+"_error.json"), | |
JSON.stringify(errorListArr)+"\n\r"+JSON.stringify(errorReasonListArr) | |
) | |
} | |
} | |
run(); | |
function fetchEntity (name){ | |
var json | |
return new Promise((resolve,reject)=>{ | |
https.get(`https://www.reddit.com/r/${name}/about.json`, | |
{ | |
headers: | |
{ | |
// PROVIDE YOUR OWN HEADERS HERE | |
} | |
}, res => { | |
res.setEncoding("utf8"); | |
let body = ""; | |
res.on("data", data => { | |
body += data; | |
}); | |
res.on("end", () => { | |
try{ | |
json = JSON.parse(body); | |
if (json?.error){ | |
resolve(json) | |
}else{ | |
json.error = false | |
} | |
} | |
catch(e){ | |
if (res.statusCode !== 200) { | |
resolve({error:true, reason:"E: HTTP NON 200 - NO JSON RETURNED"}) | |
return | |
} | |
resolve({error:true, reason:"E: JSON ERR"}) | |
} | |
// hacky delay. | |
setTimeout(() => { | |
resolve(json) | |
}, 1000); | |
}); | |
res.on("error", () => { | |
resolve({error:true, reason: "E: HTTP ERROR"}) | |
}) | |
}) | |
}) | |
} | |
function createEntity (json){ | |
json = json.data | |
result.push({ | |
"favorite": false, | |
"iconUrl": json?.icon_img, | |
"id": json?.name, | |
"name": json?.display_name, | |
"username": "-" | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment