new(basic.gblib): DOWNLOAD keyword for web automation and more.
This commit is contained in:
parent
90cf855810
commit
100cf31250
3 changed files with 533 additions and 383 deletions
|
@ -48,7 +48,8 @@ import * as fs from 'fs';
|
|||
const DateDiff = require('date-diff');
|
||||
const puppeteer = require('puppeteer');
|
||||
const Path = require('path');
|
||||
import bb, {area, bar, zoom} from "billboard.js";
|
||||
import bb, { area, bar, zoom } from "billboard.js";
|
||||
import * as request from 'request-promise-native';
|
||||
|
||||
/**
|
||||
* Base services of conversation to be called by BASIC which
|
||||
|
@ -201,6 +202,8 @@ export class DialogKeywords {
|
|||
const f = await frameHandle.contentFrame();
|
||||
await f.waitForSelector(selector);
|
||||
const element = await f.$(selector);
|
||||
element['originalSelector'] = selector;
|
||||
element['frame'] = f;
|
||||
return element;
|
||||
}
|
||||
|
||||
|
@ -218,27 +221,9 @@ export class DialogKeywords {
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the screenshot of page or element
|
||||
*
|
||||
* @example file = SCREENSHOT page
|
||||
*/
|
||||
public async getTableData(step, page, selector) {
|
||||
|
||||
const data = await page.evaluate(() => {
|
||||
const rows = document.querySelectorAll(`${selector} tr`);
|
||||
return Array.from(rows, row => {
|
||||
const columns = row.querySelectorAll('td');
|
||||
return Array.from(columns, column => column.innerText);
|
||||
});
|
||||
});
|
||||
|
||||
//You will now have an array of strings
|
||||
//[ 'One', 'Two', 'Three', 'Four' ]
|
||||
console.log(data);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the screenshot of page or element
|
||||
|
@ -250,27 +235,6 @@ export class DialogKeywords {
|
|||
await e.screenshot({ path: localName });
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the download to the .gbdrive Download folder.
|
||||
*
|
||||
* @example file = DOWNLOAD page, "tableName", row
|
||||
*/
|
||||
public async download(step, page, idOrName, localName) {
|
||||
|
||||
const e = await this.getBySelector(page, idOrName);
|
||||
const context = await this.browser.newContext({ acceptDownloads: true });
|
||||
|
||||
var cells = e.rows[0].cells;
|
||||
|
||||
const [download] = await Promise.all([
|
||||
page.waitForEvent('download'),
|
||||
page.click(cells[0])
|
||||
]);
|
||||
|
||||
const path = await download.path();
|
||||
|
||||
console.log(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Types the text into the text field.
|
||||
|
@ -280,7 +244,7 @@ export class DialogKeywords {
|
|||
public async type(step, page, idOrName, text) {
|
||||
const e = await this.getBySelector(page, idOrName);
|
||||
await e.type(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
|
||||
|
@ -297,7 +261,7 @@ export class DialogKeywords {
|
|||
}
|
||||
|
||||
return await tesseract.recognize(localFile, config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
|
||||
|
@ -329,7 +293,7 @@ export class DialogKeywords {
|
|||
default:
|
||||
return [year, month, day].join('/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quits the dialog, currently required to get out of VM context.
|
||||
|
@ -341,7 +305,7 @@ export class DialogKeywords {
|
|||
await this.browser.close();
|
||||
}
|
||||
await step.endDialog();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active tasks.
|
||||
|
@ -351,7 +315,7 @@ export class DialogKeywords {
|
|||
public async getActiveTasks() {
|
||||
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
||||
return await s.getActiveTasks();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new deal.
|
||||
|
@ -362,7 +326,7 @@ export class DialogKeywords {
|
|||
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
||||
let deal = await s.createDeal(dealName, contact, company, amount);
|
||||
return deal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds contacts in XRM.
|
||||
|
@ -372,7 +336,7 @@ export class DialogKeywords {
|
|||
public async fndContact(name) {
|
||||
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
||||
return await s.searchContact(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public getContentLocaleWithCulture(contentLocale) {
|
||||
|
@ -387,7 +351,7 @@ export class DialogKeywords {
|
|||
return 'en-us';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns specified date week day in format 'Mon'.
|
||||
|
@ -416,7 +380,7 @@ export class DialogKeywords {
|
|||
return week.substr(0, 3);
|
||||
}
|
||||
return 'NULL';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object ready to get information about difference in several ways
|
||||
|
@ -443,7 +407,7 @@ export class DialogKeywords {
|
|||
case 'hour': return diff.hours();
|
||||
case 'minute': return diff.minutes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns specified date week day in format 'Mon'.
|
||||
|
@ -471,7 +435,7 @@ export class DialogKeywords {
|
|||
default: ret = undefined; break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -494,7 +458,7 @@ export class DialogKeywords {
|
|||
array = array.join(", ");
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the specified time in format hh:dd.
|
||||
|
@ -525,7 +489,7 @@ export class DialogKeywords {
|
|||
return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
|
||||
}
|
||||
return 'NULL';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current time in format hh:dd.
|
||||
|
@ -549,7 +513,7 @@ export class DialogKeywords {
|
|||
{ timeZone: process.env.DEFAULT_TIMEZONE });
|
||||
|
||||
return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a file to a given mobile.
|
||||
|
@ -560,7 +524,7 @@ export class DialogKeywords {
|
|||
public async sendFileTo(step, mobile, filename, caption) {
|
||||
GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`);
|
||||
return await this.internalSendFile(null, mobile, filename, caption);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a file to the current user.
|
||||
|
@ -572,7 +536,7 @@ export class DialogKeywords {
|
|||
const mobile = await this.userMobile(step);
|
||||
GBLog.info(`BASIC: SEND FILE (current: ${mobile}, filename '${filename}'.`);
|
||||
return await this.internalSendFile(step, mobile, filename, caption);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the current language of the bot conversation.
|
||||
|
@ -588,7 +552,7 @@ export class DialogKeywords {
|
|||
|
||||
await this.min.userProfile.set(step.context, user);
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the maximum lines to scan in spreedsheets.
|
||||
|
@ -601,7 +565,7 @@ export class DialogKeywords {
|
|||
user.basicOptions.maxLines = count;
|
||||
await this.min.userProfile.set(step.context, user);
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the FIND behaviour to consider whole words while searching.
|
||||
|
@ -614,7 +578,7 @@ export class DialogKeywords {
|
|||
user.basicOptions.wholeWord = (on.trim() === "on");
|
||||
await this.min.userProfile.set(step.context, user);
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the theme for assets generation.
|
||||
|
@ -627,7 +591,7 @@ export class DialogKeywords {
|
|||
user.basicOptions.theme = theme.trim();
|
||||
await this.min.userProfile.set(step.context, user);
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines translator behaviour.
|
||||
|
@ -640,7 +604,7 @@ export class DialogKeywords {
|
|||
user.basicOptions.translatorOn = (on.trim() === "on");
|
||||
await this.min.userProfile.set(step.context, user);
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -648,14 +612,14 @@ export class DialogKeywords {
|
|||
*/
|
||||
public async userName(step) {
|
||||
return step ? WhatsappDirectLine.usernames[await this.userMobile(step)] : 'N/A';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* OBSOLETE.
|
||||
*/
|
||||
public async getFrom(step) {
|
||||
return step ? await this.userMobile(step) : 'N/A';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -666,7 +630,7 @@ export class DialogKeywords {
|
|||
*/
|
||||
public async userMobile(step) {
|
||||
return GBMinService.userMobile(step);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the subject menu to the user
|
||||
|
@ -676,7 +640,7 @@ export class DialogKeywords {
|
|||
*/
|
||||
public async showMenu(step) {
|
||||
return await step.beginDialog('/menu');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the transfer of the conversation to a human agent.
|
||||
|
@ -686,7 +650,7 @@ export class DialogKeywords {
|
|||
*/
|
||||
public async transferTo(step, to: string = null) {
|
||||
return await step.beginDialog('/t', { to: to });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hears something from user and put it in a variable
|
||||
|
@ -736,7 +700,7 @@ export class DialogKeywords {
|
|||
await step.beginDialog('/hear', opts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the next dialog to be shown to the specified user.
|
||||
|
@ -758,7 +722,7 @@ export class DialogKeywords {
|
|||
else {
|
||||
await step.beginDialog(fromOrDialogName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -767,7 +731,7 @@ export class DialogKeywords {
|
|||
public async talk(step, text: string) {
|
||||
await this.min.conversationalService['sendTextWithOptions'](this.min, step, text,
|
||||
this.user.basicOptions.translatorOn, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static getChannel(step): string {
|
||||
if (!step) return 'whatsapp';
|
||||
|
@ -779,7 +743,7 @@ export class DialogKeywords {
|
|||
}
|
||||
return 'webchat';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -831,5 +795,5 @@ export class DialogKeywords {
|
|||
|
||||
await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -395,7 +395,7 @@ export class GBVMService extends GBService {
|
|||
});
|
||||
|
||||
code = code.replace(/(\w+)\s*\=\s*download\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
||||
return `${$1} = download (${$2}, ${$3})`;
|
||||
return `${$1} = sys().download (${$2}, ${$3})`;
|
||||
});
|
||||
|
||||
code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
||||
|
@ -696,9 +696,6 @@ export class GBVMService extends GBService {
|
|||
code = code.replace(/("[^"]*"|'[^']*')|\bclick\b/gi, ($0, $1) => {
|
||||
return $1 === undefined ? 'this.click' : $1;
|
||||
});
|
||||
code = code.replace(/("[^"]*"|'[^']*')|\bdownload\b/gi, ($0, $1) => {
|
||||
return $1 === undefined ? 'this.download' : $1;
|
||||
});
|
||||
// await insertion.
|
||||
|
||||
code = code.replace(/this\./gm, 'await this.');
|
||||
|
|
|
@ -39,14 +39,19 @@ import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
|||
import { DialogKeywords } from './DialogKeywords';
|
||||
import { Tabulator } from 'tabulator-tables';
|
||||
import { GBServer } from '../../../src/app';
|
||||
import * as fs from 'fs';
|
||||
const Fs = require('fs');
|
||||
const Excel = require('exceljs');
|
||||
|
||||
const urlJoin = require('url-join');
|
||||
const url = require('url');
|
||||
const puppeteer = require('puppeteer')
|
||||
const Path = require('path');
|
||||
const sgMail = require('@sendgrid/mail');
|
||||
const ComputerVisionClient = require('@azure/cognitiveservices-computervision').ComputerVisionClient;
|
||||
const ApiKeyCredentials = require('@azure/ms-rest-js').ApiKeyCredentials;
|
||||
const alasql = require('alasql');
|
||||
const DateDiff = require('date-diff');
|
||||
|
||||
|
||||
/**
|
||||
|
@ -188,7 +193,7 @@ export class SystemKeywords {
|
|||
* @see puppeteer.
|
||||
*/
|
||||
private async renderTable(data, renderPDF, renderImage) {
|
||||
if (!data[1]){
|
||||
if (!data[1]) {
|
||||
return null;
|
||||
}
|
||||
const gbaiName = `${this.min.botId}.gbai`;
|
||||
|
@ -212,7 +217,7 @@ export class SystemKeywords {
|
|||
break;
|
||||
}
|
||||
|
||||
await page.addScriptTag({path: 'node_modules/tabulator-tables/dist/js/tabulator.min.js'});
|
||||
await page.addScriptTag({ path: 'node_modules/tabulator-tables/dist/js/tabulator.min.js' });
|
||||
|
||||
// Removes internal hidden element used to hold one-based index arrays.
|
||||
|
||||
|
@ -223,7 +228,7 @@ export class SystemKeywords {
|
|||
let fields = [];
|
||||
let keys = Object.keys(data[1]);
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
fields.push({field: keys[i], title: keys[i]});
|
||||
fields.push({ field: keys[i], title: keys[i] });
|
||||
}
|
||||
|
||||
// Adds DIV for Tabulator.
|
||||
|
@ -569,13 +574,12 @@ export class SystemKeywords {
|
|||
*/
|
||||
public async find(file: string, ...args): Promise<any> {
|
||||
GBLog.info(`BASIC: FIND running on ${file} and args: ${JSON.stringify(args)}...`);
|
||||
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
||||
|
||||
const botId = this.min.instance.botId;
|
||||
const path = `/${botId}.gbai/${botId}.gbdata`;
|
||||
|
||||
// TODO: if (typeof(file) ===
|
||||
// MAX LINES property.
|
||||
|
||||
let document = await this.internalGetDocument(client, baseUrl, path, file);
|
||||
let maxLines = 1000;
|
||||
if (this.dk.user && this.dk.user.basicOptions && this.dk.user.basicOptions.maxLines) {
|
||||
if (this.dk.user.basicOptions.maxLines.toString().toLowerCase() !== "default") {
|
||||
|
@ -583,16 +587,94 @@ export class SystemKeywords {
|
|||
}
|
||||
}
|
||||
|
||||
// Choose data sources based on file type (HTML Table, data variable or sheet file)
|
||||
|
||||
let results;
|
||||
let header, rows;
|
||||
|
||||
if (file['$eval']) {
|
||||
const container = file['frame'] ? file['frame'] : file['_page'];
|
||||
const originalSelector = file['originalSelector'];
|
||||
|
||||
// Transforms table
|
||||
|
||||
const resultH = await container.evaluate((originalSelector) => {
|
||||
const rows = document.querySelectorAll(`${originalSelector} tr`);
|
||||
return Array.from(rows, row => {
|
||||
const columns = row.querySelectorAll('th');
|
||||
return Array.from(columns, column => column.innerText);
|
||||
});
|
||||
}, originalSelector);
|
||||
|
||||
const result = await container.evaluate((originalSelector) => {
|
||||
const rows = document.querySelectorAll(`${originalSelector} tr`);
|
||||
return Array.from(rows, row => {
|
||||
const columns = row.querySelectorAll('td');
|
||||
return Array.from(columns, column => column.innerText);
|
||||
});
|
||||
}, originalSelector);
|
||||
|
||||
header = [];
|
||||
for (let i = 0; i < resultH[0].length; i++) {
|
||||
header[i] = resultH[0][i];
|
||||
}
|
||||
|
||||
rows = [];
|
||||
rows[0] = header;
|
||||
for (let i = 1; i < result.length; i++) {
|
||||
rows[i] = result[i];
|
||||
}
|
||||
|
||||
|
||||
} else if (file['cTag']) {
|
||||
|
||||
const gbaiName = `${this.min.botId}.gbai`;
|
||||
const localName = Path.join('work', gbaiName, 'cache', `csv${GBAdminService.getRndReadableIdentifier()}.csv`);
|
||||
const url = file['@microsoft.graph.downloadUrl'];
|
||||
const response = await request({ uri: url, encoding: null });
|
||||
Fs.writeFileSync(localName, response, { encoding: null });
|
||||
|
||||
var workbook = new Excel.Workbook();
|
||||
const worksheet = await workbook.csv.readFile(localName);
|
||||
header = [];
|
||||
rows = [];
|
||||
|
||||
for (let i = 0; i < worksheet._rows.length; i++) {
|
||||
const r = worksheet._rows[i];
|
||||
let outRow = [];
|
||||
for (let j = 0; j < r._cells.length; j++) {
|
||||
outRow.push(r._cells[j].text);
|
||||
}
|
||||
|
||||
if (i == 0) {
|
||||
header = outRow;
|
||||
}
|
||||
else {
|
||||
rows.push(outRow);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
||||
|
||||
let document
|
||||
document = await this.internalGetDocument(client, baseUrl, path, file);
|
||||
|
||||
// Creates workbook session that will be discarded.
|
||||
|
||||
let sheets = await client
|
||||
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets`)
|
||||
.get();
|
||||
|
||||
let results = await client
|
||||
results = await client
|
||||
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A1:Z${maxLines}')`)
|
||||
.get();
|
||||
|
||||
header = results.text[0];
|
||||
rows = results.text;
|
||||
}
|
||||
|
||||
let getFilter = async (text) => {
|
||||
let filter;
|
||||
const operators = [/\<\=/, /\>\=/, /\</, /\>/, /\bnot in\b/, /\bin\b/, /\=/];
|
||||
|
@ -632,7 +714,7 @@ export class SystemKeywords {
|
|||
// Increments columnIndex by looping until find a column match.
|
||||
|
||||
const filters = [];
|
||||
const header = results.text[0];
|
||||
|
||||
await CollectionUtil.asyncForEach(args, async arg => {
|
||||
const filter = await getFilter(arg);
|
||||
if (!filter) {
|
||||
|
@ -670,12 +752,12 @@ export class SystemKeywords {
|
|||
let foundIndex = 1;
|
||||
|
||||
// Fills the row variable.
|
||||
|
||||
for (; foundIndex < results.text.length; foundIndex++) {
|
||||
let rowCount = 0;
|
||||
for (; foundIndex < rows.length; foundIndex++) {
|
||||
let filterAcceptCount = 0;
|
||||
await CollectionUtil.asyncForEach(filters, async filter => {
|
||||
|
||||
let result = results.text[foundIndex][filter.columnIndex];
|
||||
let result = rows[foundIndex][filter.columnIndex];
|
||||
let wholeWord = true;
|
||||
if (this.dk.user && this.dk.user.basicOptions && this.dk.user.basicOptions.wholeWord) {
|
||||
wholeWord = this.dk.user.basicOptions.wholeWord;
|
||||
|
@ -760,9 +842,13 @@ export class SystemKeywords {
|
|||
result = result.substr(1);
|
||||
}
|
||||
const resultDate = SystemKeywords.getDateFromLocaleString(result, contentLocale);
|
||||
if (filter.value['dateOnly']) {
|
||||
resultDate.setHours(0, 0, 0, 0);
|
||||
}
|
||||
if (resultDate) {
|
||||
switch (filter.operator) {
|
||||
case '=':
|
||||
|
||||
if (resultDate.getTime() == filter.value.getTime())
|
||||
filterAcceptCount++;
|
||||
break;
|
||||
|
@ -789,8 +875,9 @@ export class SystemKeywords {
|
|||
});
|
||||
|
||||
if (filterAcceptCount === filters.length) {
|
||||
rowCount++;
|
||||
let row = {};
|
||||
const xlRow = results.text[foundIndex];
|
||||
const xlRow = rows[foundIndex];
|
||||
for (let colIndex = 0; colIndex < xlRow.length; colIndex++) {
|
||||
const propertyName = header[colIndex];
|
||||
let value = xlRow[colIndex];
|
||||
|
@ -801,7 +888,8 @@ export class SystemKeywords {
|
|||
}
|
||||
row[propertyName] = value;
|
||||
}
|
||||
row['line'] = foundIndex + 1;
|
||||
row['line'] = rowCount + 1;
|
||||
row['originalLine'] = foundIndex + 1;
|
||||
table.push(row);
|
||||
}
|
||||
|
||||
|
@ -814,36 +902,137 @@ export class SystemKeywords {
|
|||
GBLog.info(`BASIC: FIND returned single result: ${table[0]}.`);
|
||||
return table[1];
|
||||
} else {
|
||||
GBLog.info(`BASIC: FIND returned multiple results (Count): ${table.length}.`);
|
||||
GBLog.info(`BASIC: FIND returned multiple results (Count): ${table.length - 1}.`);
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
public static getDateFromLocaleString(date: any, contentLocale: any) {
|
||||
let ret = null;
|
||||
let parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})\s*(10|11|12|0?[1-9]):([0-5][0-9])/gi.exec(date);
|
||||
if (parts && parts[5]) {
|
||||
|
||||
switch (contentLocale) {
|
||||
case 'pt':
|
||||
return new Date(Number.parseInt(parts[3]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[1]),
|
||||
ret = new Date(Number.parseInt(parts[3]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[1]),
|
||||
Number.parseInt(parts[4]), Number.parseInt(parts[5]), 0, 0);
|
||||
break;
|
||||
case 'en':
|
||||
return new Date(Number.parseInt(parts[3]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[2]),
|
||||
ret = new Date(Number.parseInt(parts[3]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[2]),
|
||||
Number.parseInt(parts[4]), Number.parseInt(parts[5]), 0, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
ret['dateOnly'] = false;
|
||||
}
|
||||
|
||||
parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})$/gi.exec(date);
|
||||
if (parts && parts[3]) {
|
||||
|
||||
switch (contentLocale) {
|
||||
case 'pt':
|
||||
return new Date(Number.parseInt(parts[2]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[3]), 0, 0, 0, 0);
|
||||
ret = new Date(Number.parseInt(parts[3]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[1]), 0, 0, 0, 0);
|
||||
break;
|
||||
case 'en':
|
||||
return new Date(Number.parseInt(parts[1]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[3]), 0, 0, 0, 0);
|
||||
ret = new Date(Number.parseInt(parts[3]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[2]), 0, 0, 0, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
ret['dateOnly'] = true;
|
||||
}
|
||||
return null;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the download to the .gbdrive Download folder.
|
||||
*
|
||||
* @example file = DOWNLOAD element, folder
|
||||
*/
|
||||
public async download(element, folder) {
|
||||
|
||||
const page = element['_page'];
|
||||
const container = element['_frame'] ? element['_frame'] : element['_page'];
|
||||
|
||||
await page.setRequestInterception(true);
|
||||
await container.click(element.originalSelector);
|
||||
|
||||
const xRequest = await new Promise(resolve => {
|
||||
page.on('request', interceptedRequest => {
|
||||
interceptedRequest.abort(); //stop intercepting requests
|
||||
resolve(interceptedRequest);
|
||||
});
|
||||
});
|
||||
|
||||
const options = {
|
||||
encoding: null,
|
||||
method: xRequest['._method'],
|
||||
uri: xRequest['_url'],
|
||||
body: xRequest['_postData'],
|
||||
headers: xRequest['_headers']
|
||||
}
|
||||
|
||||
const cookies = await page.cookies();
|
||||
options.headers.Cookie = cookies.map(ck => ck.name + '=' + ck.value).join(';');
|
||||
GBLog.info(`BASIC: DOWNLOADING '${options.uri}...'`);
|
||||
|
||||
let local;
|
||||
let filename;
|
||||
if (options.uri.indexOf('file://') != -1) {
|
||||
local = url.fileURLToPath(options.uri);
|
||||
filename = Path.basename(local);
|
||||
}
|
||||
else {
|
||||
const getBasenameFormUrl = (urlStr) => {
|
||||
const url = new URL(urlStr)
|
||||
return Path.basename(url.pathname)
|
||||
};
|
||||
filename = getBasenameFormUrl(options.uri);
|
||||
}
|
||||
|
||||
let result: Buffer;
|
||||
if (local) {
|
||||
result = fs.readFileSync(local);
|
||||
} else {
|
||||
result = await request.get(options);
|
||||
}
|
||||
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
||||
const botId = this.min.instance.botId;
|
||||
|
||||
// Normalizes all slashes.
|
||||
|
||||
folder = folder.replace(/\\/gi, '/');
|
||||
|
||||
// Determines full path at source and destination.
|
||||
|
||||
const root = urlJoin(`/${botId}.gbai/${botId}.gbdrive`);
|
||||
const dstPath = urlJoin(root, folder, filename);
|
||||
|
||||
// Checks if the destination contains subfolders that
|
||||
// need to be created.
|
||||
|
||||
folder = await this.createFolder(folder);
|
||||
|
||||
// Performs the conversion operation getting a reference
|
||||
// to the source and calling /content on drive API.
|
||||
let file;
|
||||
try {
|
||||
|
||||
file = await client
|
||||
.api(`${baseUrl}/drive/root:/${dstPath}:/content`)
|
||||
.put(result);
|
||||
|
||||
} catch (error) {
|
||||
|
||||
if (error.code === "nameAlreadyExists") {
|
||||
GBLog.info(`BASIC: DOWNLOAD destination file already exists: ${dstPath}.`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a folder in the bot instance drive.
|
||||
*
|
||||
|
@ -854,7 +1043,7 @@ export class SystemKeywords {
|
|||
|
||||
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
|
||||
const botId = this.min.instance.botId;
|
||||
let path = `/${botId}.gbai/${botId}.gbdata`;
|
||||
let path = `/${botId}.gbai/${botId}.gbdrive`;
|
||||
|
||||
// Extracts each part of path to call create folder to each
|
||||
// one of them.
|
||||
|
|
Loading…
Add table
Reference in a new issue