Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const {
limitText
} = require('./libraries/helpers');
const generateHTML = require('./libraries/html-generator');
const {
uploadFile
} = require('./libraries/bucketManager');
const { uploadFile } = require('./libraries/bucketManager');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which clean code standart are we using? why are you modifiying some into one line but leaving out the others?

const generateImage = require('./libraries/image-generator');

const membersJSON = JSON.parse(fs.readFileSync('./members.json', 'utf-8'));
Expand Down Expand Up @@ -40,7 +38,11 @@ app.post('/', async (req, res, next) => {
} = req.body;

await login.login(email, password);
const pages = await Promise.all([login.getTicketList(), login.getEventlist(), login.getHandshakeList()]);
const pages = await Promise.all([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with this, should've spanned multiple lines

login.getTicketList(),
login.getEventlist(),
login.getHandshakeList()
]);
const attendance = login.combineShows(login.parseShowTickets(pages[0]), login.parseEvents(pages[1]));
const handshakes = login.parseHandshake(pages[2]);
const username = login.username;
Expand All @@ -58,24 +60,20 @@ app.post('/', async (req, res, next) => {
memberImagebuffers.push(membersJSON[memberName]);
handshakeRanks.push(`${limitText(memberName)} - ${handshakes[i].sum} kali` || null);
}
}

if (handshakes.length === 0) {
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else will prone to error if we have unhandled case within the ifs, in this case, we know every possible case, so it's okay.

handshakeRanks.push('Tidak tersedia');
memberImagebuffers.push(membersJSON.Empty);
}

if (attendance.length === 0) {
setlistRanks.push('Tidak tersedia');
setlistImageBuffers.push(setlistJSON.Empty);
}

if (attendance.length > 0) {
const length = attendance.length > 3 ? 3 : attendance.length;
for (let i = 0; i < length; i++) {
setlistImageBuffers.push(setlistJSON[attendance[i].showName] || null);
setlistRanks.push(`${attendance[i].showName} - ${attendance[i].sum} kali` || null);
}
} else {
setlistRanks.push('Tidak tersedia');
setlistImageBuffers.push(setlistJSON.Empty);
}

const slug = createSlug(username);
Expand Down Expand Up @@ -103,7 +101,10 @@ app.post('/', async (req, res, next) => {
userNameText: username
});

const results = await Promise.all([uploadFile(`share/${slug}.png`, 'image/png', Buffer.from(image)), uploadFile(`share/${slug}.html`, 'text/html', Buffer.from(html))]);
const results = await Promise.all([
uploadFile(`share/${slug}.png`, 'image/png', Buffer.from(image)),
uploadFile(`share/${slug}.html`, 'text/html', Buffer.from(html))
]);
if (results) {
res.send({
resultUrl: `https://2020.ngidol.club/share/${slug}.html`
Expand Down
8 changes: 3 additions & 5 deletions libraries/bucketManager.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const {
Storage
} = require('@google-cloud/storage');
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('2020.ngidol.club');

module.exports.uploadFile = async (filename, contentType, buffer) => {
module.exports.uploadFile = (filename, contentType, buffer) => {
const file = bucket.file(filename);
return await file.save(buffer, {
return file.save(buffer, {
metadata: {
contentType: contentType
},
Expand Down
33 changes: 9 additions & 24 deletions libraries/loginPage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require('dotenv').config();
const got = require('got');
const {
CookieJar
} = require('tough-cookie');
const {
JSDOM
} = require('jsdom');
const { CookieJar } = require('tough-cookie');
const { JSDOM } = require('jsdom');

const ticketListUrl = 'mypage/ticket-list?lang=id';
const eventListUrl = 'mypage/event-list?lang=id';
Expand All @@ -16,14 +11,6 @@ const setlist = ['Saka Agari', 'Matahari Milikku', 'Pajama Drive', 'Fajar Sang I
const isSetlistName = text => setlist.some(setlistTitle => text ? text.includes(setlistTitle) : '');
const getSetlistName = text => setlist.find(setlistTitle => text.includes(setlistTitle));


// const {
// bootstrap
// } = require('global-agent');
const e = require('express');
// bootstrap();


class Login {
constructor() {
const cookieJar = new CookieJar();
Expand All @@ -47,9 +34,7 @@ class Login {
throw new Error("Alamat email atau Kata kunci salah");
}

const {
document
} = (new JSDOM(resp.body)).window;
const { document } = (new JSDOM(resp.body)).window;
this.username = document.querySelector('.pinx').innerHTML;

} catch (e) {
Expand All @@ -61,9 +46,9 @@ class Login {
}
}

async getTicketList() {
getTicketList() {
try {
return await this.req.get(ticketListUrl, {
return this.req.get(ticketListUrl, {
resolveBodyOnly: true
});
} catch (error) {
Expand All @@ -75,9 +60,9 @@ class Login {
}
}

async getEventlist() {
getEventlist() {
try {
return await this.req.get(eventListUrl, {
return this.req.get(eventListUrl, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the catch inside this function will be useless then?

I'm putting out catch in this function to avoid polluting the caller with handling got http error

resolveBodyOnly: true
});
} catch (error) {
Expand All @@ -89,9 +74,9 @@ class Login {
}
}

async getHandshakeList() {
getHandshakeList() {
try {
return await this.req(handshakeUrl, {
return this.req(handshakeUrl, {
resolveBodyOnly: true
});
} catch (error) {
Expand Down