2017-04-20 06:23:25 +00:00
|
|
|
/**
|
|
|
|
* Created by Kylart on 20/04/2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const {userInfo} = require('os')
|
|
|
|
const {join} = require('path')
|
|
|
|
|
2017-04-29 21:39:29 +00:00
|
|
|
const wlPath = join(userInfo().homedir, '.KawAnime', 'lists.json')
|
|
|
|
|
2017-04-20 06:23:25 +00:00
|
|
|
exports.getLists = (url, res) => {
|
2017-04-29 21:39:29 +00:00
|
|
|
const wlFile = require(wlPath)
|
2017-04-20 06:23:25 +00:00
|
|
|
|
|
|
|
console.log(`[WatchList] Gathered lists from local.`)
|
|
|
|
|
2017-05-08 13:18:10 +00:00
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'})
|
2017-04-20 06:23:25 +00:00
|
|
|
res.write(JSON.stringify(wlFile))
|
|
|
|
res.end()
|
2017-04-29 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
exports.saveWatchList = (req, res) => {
|
2017-04-29 21:39:29 +00:00
|
|
|
req.on('data', (chunk) => {
|
2017-05-12 21:57:16 +00:00
|
|
|
// Saving list
|
|
|
|
fs.writeFileSync(wlPath, chunk, 'utf-8')
|
2017-04-29 21:39:29 +00:00
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
console.log('[WatchList] Successfully saved lists.')
|
2017-04-29 21:39:29 +00:00
|
|
|
|
|
|
|
res.writeHead(200, {})
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-13 17:29:52 +00:00
|
|
|
// TODO implement this in store, useless here1
|
2017-05-12 21:57:16 +00:00
|
|
|
const actOnList = (type, list, data) => {
|
|
|
|
type === 'append'
|
|
|
|
? list = [...list, ...data] && list.sort()
|
|
|
|
: list = list.filter((x) => { return x !== data })
|
2017-04-29 21:39:29 +00:00
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
return list
|
2017-04-29 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
exports.modifyList = (req, res) => {
|
2017-04-29 21:39:29 +00:00
|
|
|
req.on('data', (chunk) => {
|
2017-05-12 21:57:16 +00:00
|
|
|
chunk = JSON.parse(chunk)
|
|
|
|
|
2017-04-29 21:39:29 +00:00
|
|
|
const wlFile = require(wlPath)
|
2017-05-12 21:57:16 +00:00
|
|
|
const listName = chunk.listName
|
|
|
|
|
|
|
|
// Acting accordingly on list
|
|
|
|
wlFile[listName] = actOnList(chunk.query, wlFile[listName], chunk.data)
|
2017-04-29 21:39:29 +00:00
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
// Saving list
|
|
|
|
fs.writeFileSync(wlPath, JSON.stringify(wlFile), 'utf-8')
|
2017-04-29 21:39:29 +00:00
|
|
|
|
2017-05-12 21:57:16 +00:00
|
|
|
console.log('[WatchList] Successfully saved lists.')
|
2017-04-29 21:39:29 +00:00
|
|
|
|
|
|
|
res.writeHead(200, {})
|
|
|
|
res.end()
|
|
|
|
})
|
2017-05-08 13:18:10 +00:00
|
|
|
}
|