KawAnime/store/index.js

395 lines
11 KiB
JavaScript
Raw Normal View History

2017-05-08 13:18:10 +00:00
// noinspection NpmUsedModulesInstalled
2017-04-15 14:16:14 +00:00
import Vue from 'vue'
2017-05-08 13:18:10 +00:00
// noinspection NpmUsedModulesInstalled
2017-04-15 14:16:14 +00:00
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
2017-05-13 14:38:17 +00:00
let started = false
2017-05-10 14:41:08 +00:00
const log = (message) => {
console.log(`[${(new Date()).toLocaleTimeString()}]: ${message}`)
}
2017-04-15 14:16:14 +00:00
const store = new Vuex.Store({
state: {
2017-04-27 20:48:03 +00:00
releaseFansub: '',
releaseQuality: '',
2017-04-15 14:16:14 +00:00
releases: [],
2017-05-03 18:22:07 +00:00
releasesUpdateTime: (new Date()).toLocaleTimeString(),
errorSnackbar: {
show: false,
text: ''
},
2017-05-03 18:22:07 +00:00
downloaderList: [],
2017-04-15 14:16:14 +00:00
downloaderForm: {
name: '',
fromEp: '',
untilEp: '',
2017-04-27 20:48:03 +00:00
quality: '',
2017-04-15 14:16:14 +00:00
loading: false
},
downloaderModal: {
show: false,
title: '',
text: ''
},
seasons: [],
seasonsStats: {},
year: 2017,
season: 'spring',
news: [],
inside: true,
localFiles: [],
watchLists: {
watchList: [],
watching: [],
seen: []
},
2017-04-27 20:48:03 +00:00
config: {},
configDir: '',
currentDir: '',
2017-04-16 13:18:34 +00:00
searchInputModal: false,
searchInput: '',
searchInfo: {},
2017-04-30 16:13:42 +00:00
history: {},
historyModal: false
2017-04-15 14:16:14 +00:00
},
mutations: {
2017-05-08 13:18:10 +00:00
init (state, data) {
const config = data
2017-04-27 20:48:03 +00:00
config.inside = config.inside.toString()
// config file looks like this
// const config = {
// fansub: 'HorribleSubs',
// quality: '720p',
// sound: 'Nyanpasu',
// localPath: join(userInfo().homedir, 'Downloads'),
// inside: true,
// magnets: false
// }
2017-04-27 20:48:03 +00:00
state.releaseFansub = config.fansub
state.releaseQuality = config.quality
state.downloaderForm.quality = config.quality
state.configDir = config.localPath
state.currentDir = config.localPath
state.config = config
},
2017-05-08 13:18:10 +00:00
setDownloaderList (state, data) {
2017-05-03 18:22:07 +00:00
state.downloaderList = data
2017-05-10 14:41:08 +00:00
log(`${data.length} anime name loaded.`)
2017-05-03 18:22:07 +00:00
},
2017-05-08 13:18:10 +00:00
setErrorSnackbar (state, data) {
state.errorSnackbar.text = data
state.errorSnackbar.show = true
},
2017-05-08 13:18:10 +00:00
setCurrentSeason (state, data) {
2017-04-15 14:16:14 +00:00
state.year = data.year
state.season = data.season
},
setSeasons: function (state, data) {
state.seasons = data.info
state.seasonsStats = data.stats
2017-05-10 14:41:08 +00:00
log(`Seasons set.`)
2017-04-15 14:16:14 +00:00
},
emptySeasons: function (state) {
state.seasons = []
},
emptyReleases: function (state) {
state.releases = []
},
setReleases: function (state, data) {
state.releases = data
2017-05-10 14:41:08 +00:00
log(`Releases updated.`)
2017-04-15 14:16:14 +00:00
},
emptyNews: function (state) {
state.news = []
},
setNews: function (state, data) {
state.news = data
2017-05-10 14:41:08 +00:00
log(`News updated.`)
2017-04-15 14:16:14 +00:00
},
emptyLocals: function (state) {
state.localFiles = []
},
setLocalFiles: function (state, data) {
state.localFiles = data
2017-05-10 14:41:08 +00:00
log(`Local files updated.`)
2017-04-15 14:16:14 +00:00
},
setCurrentDir: function (state, data) {
state.currentDir = data
2017-05-10 14:41:08 +00:00
log(`Current directory now is ${state.currentDir}.`)
2017-04-15 14:16:14 +00:00
},
setWatchLists: function (state, data) {
state.watchLists = data
log('Updated watch lists.')
},
setConfigDir: function (state, data) {
state.configDir = data
2017-05-10 14:41:08 +00:00
log(`Config directory now is ${state.currentDir}`)
},
2017-04-15 14:16:14 +00:00
setDownloaderValues: function (state, data) {
state.downloaderForm = data
},
setQuality: function (state, quality) {
state.downloaderForm.quality = quality
},
setDownloaderModal: function (state, data) {
state.downloaderModal = data
},
showDownloaderModal: function (state, value) {
state.downloaderModal.show = value
},
setConfig: function (state, data) {
state.config = data
},
setHistory: function (state, data) {
state.history = data
2017-05-10 14:41:08 +00:00
log(`History updated.`)
2017-04-30 16:13:42 +00:00
},
setHistoryModal: function (state, data) {
state.historyModal = data
2017-05-03 18:22:07 +00:00
},
2017-05-08 13:18:10 +00:00
setReleasesUpdateTime (state, data) {
2017-05-03 18:22:07 +00:00
state.releasesUpdateTime = data
},
updateList (state, data) {
const listName = data.listName
const entry = data.entry
if (!state.watchLists[listName.includes(entry)]) {
state.watchLists[listName].push(entry)
state.watchLists[listName].sort()
log(`${listName} list updated.`)
}
},
removeFromList (state, data) {
const listName = data.listName
const index = state.watchLists[listName].indexOf(data.entry)
state.watchLists[listName].splice(index, 1)
2017-04-15 14:16:14 +00:00
}
},
actions: {
2017-05-08 13:18:10 +00:00
async init ({commit, dispatch}) {
2017-05-13 14:38:17 +00:00
if (!started) {
started = true
console.log('[SERVER INIT]')
const {data} = await axios.get('getConfig.json')
commit('init', data.config)
2017-05-13 14:38:17 +00:00
dispatch('downloaderInit').catch(err => { void (err) })
dispatch('releasesInit').catch(err => { void (err) })
dispatch('seasonsInit').catch(err => { void (err) })
dispatch('newsInit').catch(err => { void (err) })
dispatch('localInit').catch(err => { void (err) })
dispatch('listInit').catch(err => { void (err) })
dispatch('getHistory').catch(err => { void (err) })
}
},
2017-05-08 13:18:10 +00:00
async downloaderInit ({commit}) {
2017-05-03 18:22:07 +00:00
const {data} = await axios.get('getAllShows.json')
commit('setDownloaderList', data)
},
2017-05-08 13:18:10 +00:00
async releasesInit ({state, commit, dispatch}) {
2017-04-15 14:16:14 +00:00
console.log('[INIT] Releases')
2017-05-03 18:22:07 +00:00
const {data, status} = await axios.get(`getLatest.json?quality=${state.releaseQuality}`)
if (status === 200) commit('setReleases', data)
2017-05-08 13:38:26 +00:00
else if (status === 204) {
2017-05-10 14:41:08 +00:00
log(`An error occurred while getting the latest releases. Retrying in 30 seconds.`)
commit('setErrorSnackbar', 'Could not get the latest releases. Retrying in 30 seconds.')
setTimeout(function () {
2017-05-10 14:41:08 +00:00
log(`Retrying to get latest releases.`)
2017-05-08 13:18:10 +00:00
dispatch('refreshReleases').catch(err => { void (err) })
}, 30 * 1000)
}
2017-05-08 13:18:10 +00:00
},
async seasonsInit ({state, commit}) {
2017-04-15 14:16:14 +00:00
console.log('[INIT] Seasons')
const {data} = await axios.get(`seasons.json?year=${state.year}&season=${state.season}`)
commit('setSeasons', data)
},
2017-05-08 13:18:10 +00:00
async newsInit ({commit}) {
2017-04-15 14:16:14 +00:00
console.log('[INIT] News')
const {data} = await axios.get('news.json')
commit('setNews', data)
},
2017-05-08 13:18:10 +00:00
async localInit ({state, commit}) {
2017-04-15 14:16:14 +00:00
console.log('[INIT] Local Files')
const {data} = await axios.get(`local.json?dir=${state.currentDir}`)
commit('setLocalFiles', data)
},
2017-05-08 13:18:10 +00:00
async listInit ({commit}) {
console.log('[INIT] Watch List')
const {data} = await axios.get(`watchList.json?`)
2017-05-10 14:41:08 +00:00
log(`Received watch lists.`)
commit('setWatchLists', data)
},
2017-05-08 13:18:10 +00:00
async refreshReleases ({state, commit, dispatch}) {
2017-05-10 14:41:08 +00:00
log(`Refreshing Releases...`)
2017-04-15 14:16:14 +00:00
commit('emptyReleases')
2017-05-03 18:22:07 +00:00
const {data, status} = await axios.get(`getLatest.json?quality=${state.releaseQuality}`)
2017-04-15 14:16:14 +00:00
2017-05-03 18:22:07 +00:00
if (status === 200) commit('setReleases', data)
2017-05-08 13:38:26 +00:00
else if (status === 204) {
2017-05-10 14:41:08 +00:00
log(`An error occurred while getting the latest releases. Retrying in 30 seconds.`)
2017-05-03 18:22:07 +00:00
commit('setErrorSnackbar', 'Could not get the latest releases. Retrying in 30 seconds.')
setTimeout(function () {
2017-05-10 14:41:08 +00:00
log(`Retrying to get latest releases.`)
2017-05-08 13:18:10 +00:00
dispatch('refreshReleases').catch(err => { void (err) })
2017-05-03 18:22:07 +00:00
}, 30 * 1000)
}
2017-04-15 14:16:14 +00:00
},
2017-05-08 13:18:10 +00:00
async refreshSeasons ({state, commit}) {
2017-05-10 14:41:08 +00:00
log(`Refreshing Seasons...`)
2017-04-15 14:16:14 +00:00
commit('emptySeasons')
const year = state.year
const season = state.season
const {data} = await axios.get(`seasons.json?year=${year}&season=${season}`)
commit('setSeasons', data)
console.log('Seasons refreshed.')
},
2017-05-08 13:18:10 +00:00
async refreshNews ({commit}) {
2017-05-10 14:41:08 +00:00
log(`Refreshing News...`)
2017-04-15 14:16:14 +00:00
commit('emptyNews')
const {data} = await axios.get('news.json')
commit('setNews', data)
},
2017-05-08 13:18:10 +00:00
async refreshLocal ({commit, state}) {
2017-05-10 14:41:08 +00:00
log(`Refreshing Local files...`)
2017-04-15 14:16:14 +00:00
const {data} = await axios.get(`local.json?dir=${state.currentDir}`)
commit('setLocalFiles', data)
},
async resetLocal ({state}) {
2017-05-10 14:41:08 +00:00
log(`Resetting local information...`)
axios.get(`resetLocal?dir=${state.currentDir}`).then(() => {
log(`Reset completed.`)
}).catch((err) => {
log('An error occurred while resetting.\n' + err)
})
},
2017-05-08 13:18:10 +00:00
async changePath ({commit, dispatch}) {
2017-04-27 19:05:55 +00:00
const {data} = await axios.get('openThis?type=dialog')
commit('emptyLocals')
commit('setCurrentDir', data.path)
dispatch('refreshLocal')
},
2017-05-08 13:18:10 +00:00
async changePathWithConfig ({commit, dispatch}) {
2017-04-27 19:05:55 +00:00
const {data} = await axios.get('openThis?type=dialog')
commit('emptyLocals')
commit('setCurrentDir', data.path)
commit('setConfigDir', data.path)
dispatch('refreshLocal')
2017-04-15 14:16:14 +00:00
},
2017-05-08 13:18:10 +00:00
async openNewsLink ({state}, link) {
2017-05-10 14:41:08 +00:00
log(`Opening a link`)
2017-04-15 14:16:14 +00:00
2017-05-10 14:41:08 +00:00
if ((state.config.inside === 'true') === false) await axios.get(`openThis?type=link&link=${link}`)
else await axios.get(`openThis?type=insideLink&link=${link}`)
2017-04-15 14:16:14 +00:00
},
2017-05-08 13:18:10 +00:00
async download ({state, commit}) {
2017-04-15 14:16:14 +00:00
const name = state.downloaderForm.name.replace(' ', '_')
const fromEp = state.downloaderForm.fromEp !== ''
? state.downloaderForm.fromEp
: 0
const untilEp = state.downloaderForm.untilEp !== ''
? state.downloaderForm.untilEp
: 20000
const quality = state.downloaderForm.quality
2017-04-16 13:18:34 +00:00
const magnets = state.config.magnets
2017-04-15 14:16:14 +00:00
2017-05-10 14:41:08 +00:00
log(`Received a request to download ${name} from ep ${fromEp} to ep ${untilEp}. Transmitting...`)
2017-04-15 14:16:14 +00:00
2017-05-03 18:22:07 +00:00
const {data, status} = await axios.post('download', {
name: name.replace('_', ' '),
quality: quality,
fromEp: fromEp,
untilEp: untilEp
})
2017-04-15 14:16:14 +00:00
state.downloaderForm.loading = false
2017-05-08 13:38:26 +00:00
if (status === 200) {
2017-05-10 14:41:08 +00:00
log(`Request fulfilled!`)
2017-04-15 14:16:14 +00:00
2017-05-08 13:38:26 +00:00
if (magnets === true) {
const lastEp = fromEp !== '1' ? +fromEp + +data.length : data.length
2017-05-10 14:41:08 +00:00
log(`User says he prefers having magnets hashes.`)
2017-05-03 18:22:07 +00:00
commit('setDownloaderModal', {
show: true,
title: `${name.replace('_', ' ')}\t ${fromEp} - ${lastEp}`,
2017-05-03 18:22:07 +00:00
text: data
})
2017-05-08 13:38:26 +00:00
} else {
2017-05-10 14:41:08 +00:00
log(`Opening torrents directly on preferred torrent client.`)
2017-04-15 14:16:14 +00:00
2017-05-03 18:22:07 +00:00
data.forEach((link) => {
window.open(link)
})
}
2017-04-15 14:16:14 +00:00
}
},
2017-05-08 13:18:10 +00:00
saveConfig ({}, data) { // eslint-disable-line
axios.post('saveConfig', JSON.stringify(data)).then((res) => {
2017-05-10 14:41:08 +00:00
if (res.status === 200) { log(`Successfully updated config!`) }
}).catch((err) => {
2017-05-10 14:41:08 +00:00
log(`An error occurred while saving config: ${err}`)
})
},
2017-05-08 13:18:10 +00:00
appendHistory ({}, data) { // eslint-disable-line
axios.post('appendHistory', JSON.stringify(data)).then(() => {
2017-05-10 14:41:08 +00:00
log(`Successfully appended to history.`)
}).catch((err) => {
2017-05-10 14:41:08 +00:00
log(`An error occurred while appending to history... ${err}`)
})
},
2017-05-08 13:18:10 +00:00
async getHistory ({commit}) {
const {data, status} = await axios.get('getHistory?')
2017-05-10 14:41:08 +00:00
if (status !== 200) { log(`An error occurred while gathering the history.`) }
commit('setHistory', data)
},
2017-05-08 13:18:10 +00:00
async openInBrowser () {
const {data} = await axios.get('/_openInBrowser')
2017-05-10 14:41:08 +00:00
log(`Opening KawAnime in browser at ${data.uri}.`)
},
async saveWatchList ({state}) {
axios.post('saveWatchList', JSON.stringify(state.watchLists))
2017-04-15 14:16:14 +00:00
}
}
})
2017-05-08 13:18:10 +00:00
store.dispatch('init').catch(err => { void (err) })
2017-04-15 14:16:14 +00:00
2017-05-08 13:18:10 +00:00
export default store