-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message downloader (#3), minor fixes, update to v0.0.4
- Loading branch information
1 parent
93a38d3
commit 5c568cd
Showing
5 changed files
with
154 additions
and
27 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { bold, red, green, white, gray } = require('chalk'); | ||
const configstore = require('configstore'); | ||
const fs = require('fs'); | ||
const mkdirp = require('mkdirp'); | ||
const path = require('path'); | ||
const ora = require('ora'); | ||
const request = require('request-promise-native'); | ||
|
||
const utils = require('./utils'); | ||
|
||
module.exports = async directory => { | ||
const spinner = ora('Downloading...').start(); | ||
|
||
try { | ||
const accessToken = (await utils.login())['access_token']; | ||
|
||
mkdirp.sync(directory); | ||
|
||
const messages = JSON.parse( | ||
await request.get( | ||
'https://eugyintezes.e-kreta.hu/integration-kretamobile-api/v1/kommunikacio/postaladaelemek/sajat', | ||
{ | ||
auth: { bearer: accessToken }, | ||
}, | ||
), | ||
); | ||
|
||
const wasMessage = {}; | ||
let messageCounter = 0; | ||
|
||
for (let fMessage of messages) { | ||
const message = JSON.parse( | ||
await request.get( | ||
`https://eugyintezes.e-kreta.hu/integration-kretamobile-api/v1/kommunikacio/postaladaelemek/${ | ||
fMessage['azonosito'] | ||
}`, | ||
{ auth: { bearer: accessToken } }, | ||
), | ||
); | ||
|
||
if (!wasMessage[message['azonosito']]) { | ||
wasMessage[message['azonosito']] = true; | ||
messageCounter++; | ||
} | ||
|
||
spinner.text = `Downloading... (${messageCounter} message${messageCounter == 1 ? '' : 's'})`; | ||
|
||
const date = new Date(message['uzenet']['kuldesDatum']); | ||
|
||
mkdirp.sync( | ||
`${directory}/${utils.replaceIllegalChars( | ||
message['uzenet']['feladoNev'].trim(), | ||
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${message['azonosito']}`, | ||
); | ||
|
||
fs.writeFileSync( | ||
path.resolve( | ||
process.cwd(), | ||
`${directory}/${utils.replaceIllegalChars( | ||
message['uzenet']['feladoNev'].trim(), | ||
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${ | ||
message['azonosito'] | ||
}/message.html`, | ||
), | ||
`<!-- | ||
* Teacher: ${message['uzenet']['feladoNev'].trim()} | ||
* Date: ${utils.toDateString(date)} | ||
* Subject: ${message['uzenet']['targy']} | ||
--> | ||
${message['uzenet']['szoveg']} | ||
`, | ||
); | ||
|
||
for (let attachment of message['uzenet']['csatolmanyok']) { | ||
const data = Buffer.from( | ||
await request.get( | ||
`https://eugyintezes.e-kreta.hu/integration-kretamobile-api/v1/dokumentumok/uzenetek/${ | ||
attachment['azonosito'] | ||
}`, | ||
{ auth: { bearer: accessToken }, encoding: null }, | ||
), | ||
'utf8', | ||
); | ||
|
||
fs.writeFileSync( | ||
`${directory}/${utils.replaceIllegalChars( | ||
message['uzenet']['feladoNev'].trim(), | ||
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${message['azonosito']}/${ | ||
attachment['fajlNev'] | ||
}`, | ||
data, | ||
); | ||
} | ||
} | ||
|
||
spinner.succeed( | ||
`Successfully downloaded ${messageCounter} message${messageCounter == 1 ? '' : 's'}.`, | ||
); | ||
} catch (e) { | ||
spinner.fail('An error occured while fetching data.'); | ||
console.error(e); | ||
} | ||
}; |