Skip to content

Commit

Permalink
Add message downloader (#3), minor fixes, update to v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
szekelymilan committed Mar 24, 2020
1 parent 93a38d3 commit 5c568cd
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 27 deletions.
49 changes: 33 additions & 16 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ const updateNotifier = require('update-notifier');
const utils = require('./src/utils');
const assignments = require('./src/assignments');
const averages = require('./src/averages');
const messages = require('./src/messages');
const reconfigure = require('./src/reconfigure');

const cli = meow(
`
${bold('Usage')}
$ kreta <assignments|averages|reconfigure> [options]
$ kreta <assignments|averages|messages|reconfigure> [options]
Use 'kreta' for interactive mode.
${bold('Options')}
assignments:
-o, --output Output folder (defaults to assignments)
messages:
-o, --output Output folder (defaults to messages)
${bold('Examples')}
See: https://github.com/szekelymilan/e-kreta-cli#examples
`,
Expand All @@ -38,25 +42,37 @@ const { output: OUTPUT } = cli.flags;

async function doTask(task) {
switch (task) {
case 'reconfigure':
await reconfigure();
break;
case 'assignments':
let output =
await assignments(
OUTPUT ||
(await inquirer.prompt([
{
type: 'input',
name: 'output',
message: 'Output folder:',
default: 'assignments',
},
]))['output'];

await assignments(output);
(await inquirer.prompt([
{
type: 'input',
name: 'output',
message: 'Output folder:',
default: 'assignments',
},
]))['output'],
);
break;
case 'averages':
await averages();
break;
case 'reconfigure':
await reconfigure();
case 'messages':
await messages(
OUTPUT ||
(await inquirer.prompt([
{
type: 'input',
name: 'output',
message: 'Output folder:',
default: 'messages',
},
]))['output'],
);
break;
default:
await interactive();
Expand All @@ -71,9 +87,10 @@ async function interactive() {
name: 'tasks',
message: 'Select tasks:',
choices: [
{ name: 'Download assignments', value: 'assignments', checked: false },
{ name: 'Show subject averages', value: 'averages', checked: false },
{ name: 'Update configuration', value: 'reconfigure', checked: false },
{ name: 'Show subject averages', value: 'averages', checked: false },
{ name: 'Download assignments', value: 'assignments', checked: false },
{ name: 'Download messages', value: 'messages', checked: false },
],
},
]);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "e-kreta-cli",
"version": "0.0.3",
"version": "0.0.4",
"description": "Check your electronic diary - from right inside your terminal.",
"main": "cli.js",
"scripts": {},
Expand Down
24 changes: 15 additions & 9 deletions src/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ module.exports = async directory => {
assignmentCounter++;
}

mkdirp.sync(`${directory}/${utils.replaceIllegalChars(assignment['Tantargy'])}`);

spinner.text = `Downloading... (${assignmentCounter} assignments)`;
spinner.text = `Downloading... (${assignmentCounter} assignment${
assignmentCounter == 1 ? '' : 's'
})`;

const date = new Date(assignment['FeladasDatuma']);
const deadline = new Date(assignment['Hatarido']);

mkdirp.sync(`${directory}/${utils.replaceIllegalChars(assignment['Tantargy'].trim())}`);

fs.writeFileSync(
path.resolve(
process.cwd(),
`${directory}/${utils.replaceIllegalChars(assignment['Tantargy'])}/${utils.toDateFileName(
date,
)} - ${assignment['Id']}.html`,
`${directory}/${utils.replaceIllegalChars(
assignment['Tantargy'].trim(),
)}/${utils.toDateFileName(date)} - ${assignment['Id']}.html`,
),
`<!--
* Lesson: ${assignment['Tantargy']}
* Teacher: ${assignment['Rogzito']}
* Lesson: ${assignment['Tantargy'].trim()}
* Teacher: ${assignment['Rogzito'].trim()}
* Date: ${utils.toDateString(date)}
* Deadline: ${utils.toDateString(deadline)}
-->
Expand All @@ -68,7 +70,11 @@ ${assignment['Szoveg']}
);
}

spinner.succeed(`Successfully downloaded ${assignmentCounter} assignments.`);
spinner.succeed(
`Successfully downloaded ${assignmentCounter} assignment${
assignmentCounter == 1 ? '' : 's'
}.`,
);
} catch (e) {
spinner.fail('An error occured while fetching data.');
console.error(e);
Expand Down
104 changes: 104 additions & 0 deletions src/messages.js
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);
}
};

0 comments on commit 5c568cd

Please sign in to comment.