Skip to content

Commit

Permalink
Add date interval selection to assignment downloading (#4), minor fix…
Browse files Browse the repository at this point in the history
…es, upgrade to v0.0.6
  • Loading branch information
szekelymilan committed Apr 10, 2020
1 parent accc546 commit 3a22379
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 20 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ $ kreta --help
Options
assignments:
-o, --output Output folder (defaults to assignments)
-o, --output Output folder
--sD, --startDate Start date of interval (yyyy-mm-dd)
--eD, --endDate End date of interval (yyyy-mm-dd)
messages:
-o, --output Output folder (defaults to messages)
-o, --output Output folder
Examples
See: https://github.com/szekelymilan/e-kreta-cli#examples
Expand All @@ -83,10 +85,10 @@ $ kreta reconfigure
$ kreta averages
```

#### Download assignments to My Assignments folder
#### Download assignments to My Assignments folder (from 2020-03-16 to 2020-03-20)

```
$ kreta assignments -o "My Assignments"
$ kreta assignments -o "My Assignments" --sD="2020-03-16" --eD="2020-03-20"
```

#### Download messages to My Messages folder
Expand All @@ -98,6 +100,7 @@ $ kreta messages -o "My Messages"
## Contributors

- Many thanks to [boapps](https://github.com/boapps) for making [e-kreta-api-docs](https://github.com/boapps/e-kreta-api-docs).
- Thanks to [zoltansx](https://github.com/zoltansx) for lots of great ideas.

## License

Expand Down
70 changes: 67 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const inquirer = require('inquirer');
const meow = require('meow');
const updateNotifier = require('update-notifier');

inquirer.registerPrompt('datepicker', require('inquirer-datepicker'));

const utils = require('./src/utils');
const assignments = require('./src/assignments');
const averages = require('./src/averages');
Expand All @@ -20,10 +22,12 @@ const cli = meow(
${bold('Options')}
assignments:
-o, --output Output folder (defaults to assignments)
-o, --output Output folder
--sD, --startDate Start date of interval (yyyy-mm-dd)
--eD, --endDate End date of interval (yyyy-mm-dd)
messages:
-o, --output Output folder (defaults to messages)
-o, --output Output folder
${bold('Examples')}
See: https://github.com/szekelymilan/e-kreta-cli#examples
Expand All @@ -34,18 +38,76 @@ const cli = meow(
type: 'string',
alias: 'o',
},
startDate: {
type: 'string',
alias: 'sD',
},
endDate: {
type: 'string',
alias: 'eD',
},
},
},
);

const { output: OUTPUT } = cli.flags;
const { output: OUTPUT, startDate: START_DATE, endDate: END_DATE } = cli.flags;

async function doTask(task) {
switch (task) {
case 'reconfigure':
await reconfigure();
break;
case 'assignments':
const startDate = utils.isValidDate(START_DATE)
? START_DATE
: utils.toDateString(
(await inquirer.prompt([
{
type: 'datepicker',
name: 'date',
message: 'Select start date:',
format: ['Y', '-', 'MM', '-', 'DD'],
min: {
year: 1970,
month: 1,
day: 1,
},
max: {
year: utils.isValidDate(END_DATE)
? END_DATE.split(/\D/)[0]
: new Date().getFullYear(),
month: utils.isValidDate(END_DATE)
? END_DATE.split(/\D/)[1]
: new Date().getMonth() + 1,
day: utils.isValidDate(END_DATE) ? END_DATE.split(/\D/)[2] : new Date().getDate(),
},
},
]))['date'],
);

const endDate = utils.isValidDate(END_DATE)
? END_DATE
: utils.toDateString(
(await inquirer.prompt([
{
type: 'datepicker',
name: 'date',
message: 'Select end date:',
format: ['Y', '-', 'MM', '-', 'DD'],
min: {
year: startDate.split(/\D/)[0],
month: startDate.split(/\D/)[1],
day: startDate.split(/\D/)[2],
},
max: {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1,
day: new Date().getDate(),
},
},
]))['date'],
);

await assignments(
OUTPUT ||
(await inquirer.prompt([
Expand All @@ -56,6 +118,8 @@ async function doTask(task) {
default: 'assignments',
},
]))['output'],
startDate,
endDate,
);
break;
case 'averages':
Expand Down
29 changes: 28 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 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.5",
"version": "0.0.6",
"description": "Check your electronic diary - from right inside your terminal.",
"main": "cli.js",
"scripts": {},
Expand All @@ -17,6 +17,7 @@
"fuse.js": "^5.1.0",
"inquirer": "^7.1.0",
"inquirer-autocomplete-prompt": "^1.0.2",
"inquirer-datepicker": "^1.1.0",
"js-base64": "^2.5.2",
"meow": "^6.1.0",
"mkdirp": "^1.0.4",
Expand Down
15 changes: 10 additions & 5 deletions src/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const ora = require('ora');
const utils = require('./utils');
const request = utils.request;

module.exports = async directory => {
module.exports = async (directory, startDate, endDate) => {
if (new Date(Date.parse(startDate)) > new Date(Date.parse(endDate)))
return console.error(red('Invalid date interval.'));

const spinner = ora('Downloading...').start();

try {
Expand All @@ -18,7 +21,9 @@ module.exports = async directory => {

const lessons = JSON.parse(
await request.get(
`https://${utils.conf.get('institute')}.e-kreta.hu/mapi/api/v1/Lesson?fromDate=1970-01-01`,
`https://${utils.conf.get(
'institute',
)}.e-kreta.hu/mapi/api/v1/Lesson?fromDate=${startDate}&toDate=${endDate}`,
{
auth: { bearer: accessToken },
},
Expand Down Expand Up @@ -59,13 +64,13 @@ module.exports = async directory => {
process.cwd(),
`${directory}/${utils.replaceIllegalChars(
assignment['Tantargy'].trim(),
)}/${utils.toDateFileName(date)} - ${assignment['Id']}.html`,
)}/${utils.toDateString(date)} - ${assignment['Id']}.html`,
),
`<!--
* Lesson: ${assignment['Tantargy'].trim()}
* Teacher: ${assignment['Rogzito'].trim()}
* Date: ${utils.toDateString(date)}
* Deadline: ${utils.toDateString(deadline)}
* Date: ${utils.toDateTimeString(date)}
* Deadline: ${utils.toDateTimeString(deadline)}
-->
${assignment['Szoveg']}
Expand Down
8 changes: 4 additions & 4 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ module.exports = async directory => {
mkdirp.sync(
`${directory}/${utils.replaceIllegalChars(
message['uzenet']['feladoNev'].trim(),
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${message['azonosito']}`,
)}/${utils.replaceIllegalChars(utils.toDateString(date))} - ${message['azonosito']}`,
);

fs.writeFileSync(
path.resolve(
process.cwd(),
`${directory}/${utils.replaceIllegalChars(
message['uzenet']['feladoNev'].trim(),
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${
)}/${utils.replaceIllegalChars(utils.toDateString(date))} - ${
message['azonosito']
}/message.html`,
),
`<!--
* Teacher: ${message['uzenet']['feladoNev'].trim()}
* Date: ${utils.toDateString(date)}
* Date: ${utils.toDateTimeString(date)}
* Subject: ${message['uzenet']['targy']}
-->
Expand All @@ -86,7 +86,7 @@ ${message['uzenet']['szoveg']}
fs.writeFileSync(
`${directory}/${utils.replaceIllegalChars(
message['uzenet']['feladoNev'].trim(),
)}/${utils.replaceIllegalChars(utils.toDateFileName(date))} - ${message['azonosito']}/${
)}/${utils.replaceIllegalChars(utils.toDateString(date))} - ${message['azonosito']}/${
attachment['fajlNev']
}`,
data,
Expand Down
10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ exports.replaceIllegalChars = path => {
return path.replace(/[/\\?%*:|"<>]/g, '-');
};

exports.toDateString = date => {
exports.toDateTimeString = date => {
return (
date.getFullYear() +
'. ' +
Expand All @@ -82,7 +82,7 @@ exports.toDateString = date => {
);
};

exports.toDateFileName = date => {
exports.toDateString = date => {
return (
date.getFullYear() +
'-' +
Expand All @@ -91,3 +91,9 @@ exports.toDateFileName = date => {
('0' + date.getDate()).slice(-2)
);
};

exports.isValidDate = dateString => {
if (!dateString || !dateString.match(/^\d{4}-\d{2}-\d{2}$/)) return false;
const date = new Date(dateString);
return (date.getTime() || date.getTime() === 0) && date.toISOString().slice(0, 10) === dateString;
};

0 comments on commit 3a22379

Please sign in to comment.