Skip to content

Commit

Permalink
refactor: újraírtam az egész cuccot
Browse files Browse the repository at this point in the history
BREAKING CHANGE: lib.js szétdarabolva osztályokra, process.env helyett Config modellt használunk már, új logger.js - részletek: ld. JSDoc
  • Loading branch information
juzraai committed Oct 23, 2019
1 parent c295551 commit 6c95ad1
Show file tree
Hide file tree
Showing 20 changed files with 830 additions and 428 deletions.
12 changes: 5 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ OUTPUT_DIR=./szamlak
SLEEP=3


# Naplózás szintje:
# 0 = semmit nem ír a képernyőre
# 1 = hibaüzenetek
# 2 = 1 + sikeres műveletsorok jelzése
# 3 = 2 + műveletek
# 4 = 3 + részletesebb információk
LOG_LEVEL=2
# Naplózás módja:
# default = terminálban könnyen érthető folyamatjelző, fájlba irányítva bővített napló (korábban: LOG_LEVEL=1/2)
# verbose = bővített napló, minden műveletről tájékoztat (korábban: LOG_LEVEL=4)
# quiet = nincs kimenet (korábban: LOG_LEVEL=0)
LOG_MODE=default
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ szamlak/
temp/
.env
error.log
src/_OLD
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env node
const SingleInstance = require('single-instance');
const Logger = require('./src/logger');
const { start } = require('./src/main');
const { handleError } = require('./src/err');

new SingleInstance('dijnet-bot').lock().then(start).catch(error => {
handleError(error.stack ? error : 'A Díjnet bot már fut, és nem futtatható több példányban.');
try {
new Logger().error(error.stack ? error : 'A Díjnet bot már fut, és nem futtatható több példányban.');
} catch (error2) { // in case Logger is also dead
console.log(error2);
}
process.exit(1);
});
73 changes: 36 additions & 37 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
},
"homepage": "https://github.com/juzraai/dijnet-bot#readme",
"dependencies": {
"chalk": "^2.4.2",
"cheerio": "^1.0.0-rc.3",
"commander": "^3.0.2",
"dotenv": "^8.0.0",
"got": "^9.6.0",
"kleur": "^3.0.3",
"lodash.deburr": "^4.1.0",
"mkdirp": "^0.5.1",
"signale": "^1.4.0",
"single-instance": "0.0.1",
"tough-cookie": "^3.0.0"
},
Expand Down
15 changes: 15 additions & 0 deletions src/bill-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Represents a bill's downloadable file.
*/
class BillFile {
/**
* @param {string} name Display name
* @param {string} dijnetPath Díjnet path, relative to `DijnetBrowser`'s `baseUrl`
*/
constructor(name, dijnetPath) {
this.name = name;
this.dijnetPath = dijnetPath;
}
}

module.exports = BillFile;
31 changes: 31 additions & 0 deletions src/bill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Represents bill metadata.
*/
class Bill {
/**
* @param {Bill} data Bill metadata available on Díjnet
* @param {string} data.rowId Row ID in search results
* @param {string} data.serviceProvider Service provider
* @param {string} data.billIssuerId Bill issuer ID
* @param {string} data.billId Bill ID
* @param {string} data.dateOfIssue Date of issue
* @param {string} data.finalAmount Final amount
* @param {string} data.dueDate Due date
* @param {string} data.payable Payable
* @param {string} data.status Status
*/
constructor(data) {
data = data || {};
this.rowId = data.rowId;
this.serviceProvider = data.serviceProvider;
this.billIssuerId = data.billIssuerId;
this.billId = data.billId;
this.dateOfIssue = data.dateOfIssue;
this.finalAmount = data.finalAmount;
this.dueDate = data.dueDate;
this.payable = data.payable;
this.status = data.status;
}
}

module.exports = Bill;
50 changes: 50 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const got = require('got');
const { CookieJar } = require('tough-cookie');

/**
* Wrapper around `got` HTTP client. Adds a cookie jar and stores the last successful response.
*/
class Browser {
constructor() {
this.cookieJar = new CookieJar();
/** @type {got.Response} */
this.lastNavigationResponse = null;
}

/**
* Sends a HTTP request to the given URL.
*
* @param {string} url Requested URL
* @param {got.GotJSONOptions} options Request options (method, headers, body, encoding, etc.)
* @returns {got.GotPromise<got.Response>} Response
*/
async request(url, options) {
return got(url, Object.assign({ cookieJar: this.cookieJar }, options));
}

/**
* Sends a GET request to the given URL, then stores the response in `lastNavigationResponse`.
*
* @param {string} url Requested URL
* @param {got.GotJSONOptions} options Request options (headers, encoding, etc.)
* @returns {got.GotPromise<got.Response>} Response
*/
async navigate(url, options) {
this.lastNavigationResponse = await this.request(url, Object.assign({ method: 'GET' }, options));
return this.lastNavigationResponse;
}

/**
* Sends a POST request to the given URL, then stores the response in `lastNavigationResponse`.
*
* @param {string} url Requested URL
* @param {got.GotJSONOptions} options Request options (body, headers, encoding, etc.)
* @returns {got.GotPromise<got.Response>} Response
*/
async submit(url, options) {
this.lastNavigationResponse = await this.request(url, Object.assign({ method: 'POST' }, options));
return this.lastNavigationResponse;
}
}

module.exports = Browser;
Loading

0 comments on commit 6c95ad1

Please sign in to comment.