first commit
This commit is contained in:
commit
2ca923889e
|
@ -0,0 +1,4 @@
|
|||
node_modules/
|
||||
src/data/
|
||||
prod/
|
||||
*.log
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "micro365",
|
||||
"version": "1.0.0",
|
||||
"description": "a server that delivers daily writing prompts via REST API",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Andrzej Stepien",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"devDependencies": {
|
||||
"json-index": "^1.1.0",
|
||||
"spellchecker": "^3.7.1",
|
||||
"sqlite3": "^5.1.6"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
const Spellchecker = require("spellchecker")
|
||||
const sqlite3 = require("sqlite3").verbose()
|
||||
const db = new sqlite3.Database("data/database")
|
||||
|
||||
db.serialize(() => {
|
||||
db.each("SELECT * FROM prompts", [],
|
||||
function (err, row) {
|
||||
if (err) {
|
||||
return console.error(err.message)
|
||||
}
|
||||
if (Spellchecker.isMisspelled(row.word)) {
|
||||
db.run("DELETE from prompts WHERE word=?", [row.word], function (err) { if (err) { return console.error(err.message) } })
|
||||
console.log(`deleted non-word ${row.word}`)
|
||||
}
|
||||
},
|
||||
function (err, rows) {
|
||||
if (err) { return console.error(err.message) }
|
||||
console.log(`${rows} rows`)
|
||||
db.close()
|
||||
console.log("db closed")
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
|
||||
const StreamArray = require('stream-json/streamers/StreamArray');
|
||||
const fs = require('fs');
|
||||
const pipeline = fs.createReadStream('data/dp/wiktionary-grouped-objects-array.json').pipe(StreamArray.withParser());
|
||||
|
||||
const sqlite3 = require("sqlite3").verbose()
|
||||
const db = new sqlite3.Database("data/database")
|
||||
|
||||
const queries = []
|
||||
|
||||
pipeline.on('data', data => {
|
||||
const term = data.value.word
|
||||
// if(term==="unpalatable"){console.log("test word found!!")}
|
||||
const object = JSON.stringify(makeObject(data.value))
|
||||
// db.run('UPDATE prompts SET object=? WHERE word=?',[object,term],
|
||||
// function(err){
|
||||
// if(err){return console.error(err.message)}
|
||||
// console.log(`word: ${term} -- ${this.changes} changes`)
|
||||
// console.log(`object: ${object}`)
|
||||
// })
|
||||
|
||||
queries.push(`${queries}UPDATE prompts
|
||||
SET object='${object}'
|
||||
WHERE word='${term}';
|
||||
`)
|
||||
console.log(queries)
|
||||
});
|
||||
|
||||
db.exec(queries.join()).close(err=>{
|
||||
if(err){return console.error(err.message)}
|
||||
console.log("Database closed successfully")
|
||||
})
|
||||
|
||||
|
||||
const makeObject = (obj) => {
|
||||
const meaningsArray = []
|
||||
obj.data.forEach(element => {
|
||||
meaningsArray.push({
|
||||
type:element.type,
|
||||
definition:element.definition[0]
|
||||
})
|
||||
});
|
||||
return {
|
||||
word: obj.word,
|
||||
pronunciation: obj.data[0].pronunciation,
|
||||
meanings: meaningsArray
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import makeToot from "./makeToot.js"
|
||||
const word = "and"
|
||||
const output = fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
|
||||
.then(res=>res.json()).then(json=>{
|
||||
|
||||
makeToot(json[0])
|
||||
})
|
||||
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
export default function makeToot(data){
|
||||
console.dir(data)
|
||||
let newString = ""
|
||||
const maxIterations = 3
|
||||
|
||||
for(let i=0;i<=data.meanings.length-1;i++){
|
||||
const e = data.meanings[i]
|
||||
newString=newString+`
|
||||
**${e.partOfSpeech}**
|
||||
|
||||
`
|
||||
for(let j=0;j<=e.definitions.length-1;j++){
|
||||
const definition = e.definitions[j]
|
||||
newString =
|
||||
`${newString} *${definition.definition}*
|
||||
${definition.example ? `> ${definition.example}
|
||||
|
||||
` : ``}`
|
||||
if(j+1>=maxIterations){break}
|
||||
}
|
||||
if(i+1>=maxIterations){break}
|
||||
}
|
||||
|
||||
const definitionsArray = data.meanings[0].definition
|
||||
const string =
|
||||
`Today's prompt is:
|
||||
# **${data.word}**
|
||||
*${data.phonetic}*
|
||||
${newString}
|
||||
${data.sourceUrls}`
|
||||
|
||||
console.log(string)
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
export default function sendToot(){
|
||||
// Example POST method implementation:
|
||||
async function postData(url = "", data = {}) {
|
||||
// Default options are marked with *
|
||||
const response = await fetch(url, {
|
||||
method: "POST", // *GET, POST, PUT, DELETE, etc.
|
||||
mode: "cors", // no-cors, *cors, same-origin
|
||||
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
|
||||
credentials: "same-origin", // include, *same-origin, omit
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
redirect: "follow", // manual, *follow, error
|
||||
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
||||
body: JSON.stringify(data), // body data type must match "Content-Type" header
|
||||
});
|
||||
return response.json(); // parses JSON response into native JavaScript objects
|
||||
}
|
||||
|
||||
postData("https://example.com/answer", { answer: 42 }).then((data) => {
|
||||
console.log(data); // JSON data parsed by `data.json()` call
|
||||
});
|
||||
|
||||
}
|
Loading…
Reference in New Issue