mirror of
https://github.com/WebTools-NG/WebTools-NG
synced 2024-11-26 13:00:18 +00:00
just more on a bumby way
This commit is contained in:
parent
1a13b717e2
commit
09f24b368b
4 changed files with 289 additions and 23 deletions
Binary file not shown.
|
@ -8,7 +8,16 @@
|
|||
"Level 5": "level5",
|
||||
"Level 6": "level6",
|
||||
"Test Tommy": "testtommy"
|
||||
},
|
||||
},
|
||||
"LevelCount": {
|
||||
"Level 1": 1,
|
||||
"Level 2": 1,
|
||||
"Level 3": 2,
|
||||
"Level 4": 2,
|
||||
"Level 5": 2,
|
||||
"Level 6": 2,
|
||||
"Test Tommy": 2
|
||||
},
|
||||
"level": {
|
||||
"testtommy": [
|
||||
"Title",
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var def = JSON.parse(JSON.stringify(require('./definitions.json')));
|
||||
const log = require('electron-log');
|
||||
import {wtconfig, wtutils} from '../../../wtutils'
|
||||
|
||||
const fetch = require('node-fetch');
|
||||
const jp = require('jsonpath')
|
||||
|
||||
|
@ -27,11 +28,14 @@ const et = new class ET {
|
|||
|
||||
async getItemData(baseURL, accessToken, element)
|
||||
{
|
||||
console.log('ged22')
|
||||
const url = baseURL + element
|
||||
var headers = {
|
||||
"Accept": "application/json",
|
||||
"X-Plex-Token": accessToken
|
||||
}
|
||||
}
|
||||
|
||||
console.log('ged33: ' + url)
|
||||
let response = await fetch(url, { method: 'GET', headers: headers});
|
||||
let resp = await response.json();
|
||||
const respJSON = await Promise.resolve(resp)
|
||||
|
@ -48,7 +52,7 @@ const et = new class ET {
|
|||
|
||||
getLevelFields(level, libType) {
|
||||
// return fields in a level
|
||||
const out = []
|
||||
const out = []
|
||||
const levels = def[libType]['level'][et.getRealLevelName(level, libType)]
|
||||
Object.keys(levels).forEach(function(key) {
|
||||
out.push(levels[key])
|
||||
|
@ -56,19 +60,10 @@ const et = new class ET {
|
|||
return out
|
||||
}
|
||||
|
||||
getLevelCall (libType, level) {
|
||||
async getLevelCall (libType, level) {
|
||||
// this will return number of calls needed
|
||||
let count = 0
|
||||
// So walk every field
|
||||
const fields = et.getLevelFields(level, libType)
|
||||
for (var i=0; i<fields.length; i++) {
|
||||
const call = def[libType]['fields'][fields[i]]['call']
|
||||
if (call > count)
|
||||
{
|
||||
count = call
|
||||
}
|
||||
}
|
||||
log.debug('Count needed is: ' + count)
|
||||
const count = await def[libType]['LevelCount'][level]
|
||||
log.debug('Count needed is: ' + count)
|
||||
return count
|
||||
}
|
||||
|
||||
|
@ -144,7 +139,8 @@ const et = new class ET {
|
|||
|
||||
const excel = new class Excel {
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
|
||||
NewSheet(Workbook, Library, Level) {
|
||||
const sheet = Workbook.addWorksheet(Library + '-' + Level, {
|
||||
views: [
|
||||
|
@ -210,6 +206,55 @@ const excel = new class Excel {
|
|||
}
|
||||
|
||||
async addToSheet(sheet, libType, level, data, baseURL, accessToken, workBook, libName, outType) {
|
||||
sheet
|
||||
libType
|
||||
level
|
||||
data
|
||||
baseURL
|
||||
accessToken
|
||||
workBook, libName, outType
|
||||
|
||||
console.log('Ged start addToSheet')
|
||||
const call = await et.getLevelCall(libType, level)
|
||||
console.log('Ged Count needed is: ' + call)
|
||||
if ( call == 1 )
|
||||
{
|
||||
console.log('GED Result returned1: ' + JSON.stringify(data));
|
||||
// Single call needed, so simply pass along the individual items
|
||||
const items = jp.nodes(data, '$.MediaContainer.Metadata[*]')
|
||||
for (var x=0; x<items.length; x++) {
|
||||
console.log('GED Item returned: ' + JSON.stringify(items[x]['value']));
|
||||
excel.addRowToSheet(sheet, libType, level, items[x]['value'])
|
||||
}
|
||||
excel.SaveWorkbook(workBook, libName, level, outType)
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Ged multiple calls needed')
|
||||
// Get rating key for each item
|
||||
const urls = jp.query(data, '$.MediaContainer.Metadata[*].key');
|
||||
log.verbose('Items to lookup are: ' + urls)
|
||||
// TODO: Wait until all is processed before we save
|
||||
urls.forEach(element => {
|
||||
// console.log('Ged item: ' + element)
|
||||
et.getItemData(baseURL, accessToken, element)
|
||||
.then(function(data) {
|
||||
const items = jp.nodes(data, '$.MediaContainer.Metadata[*]')
|
||||
for (var x=0; x<items.length; x++) {
|
||||
console.log('GED Item returned Multi: ' + JSON.stringify(items[x]['value']));
|
||||
excel.addRowToSheet(sheet, libType, level, items[x]['value'])
|
||||
}
|
||||
}).catch(function(error) {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
excel.SaveWorkbook(workBook, libName, level, outType)
|
||||
console.log('GED Tommy Done')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async addToSheet1(sheet, libType, level, data, baseURL, accessToken, workBook, libName, outType) {
|
||||
log.info('Start AddToSheet')
|
||||
// Check if single or multi call is needed
|
||||
const call = et.getLevelCall(libType, level)
|
||||
|
@ -354,4 +399,208 @@ const excel = new class Excel {
|
|||
}
|
||||
}
|
||||
|
||||
export {et, excel};
|
||||
const excel2 = new class Excel {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
async NewExcelWorkBook() {
|
||||
const Excel = require('exceljs');
|
||||
// A new Excel Work Book
|
||||
const workbook = new Excel.Workbook();
|
||||
// Some information about the Excel Work Book.
|
||||
workbook.creator = 'WebTools-NG';
|
||||
workbook.lastModifiedBy = '';
|
||||
workbook.created = new Date();
|
||||
workbook.modified = new Date();
|
||||
return workbook
|
||||
}
|
||||
|
||||
async NewSheet(Workbook, Library, Level) {
|
||||
const sheet = Workbook.addWorksheet(Library + '-' + Level, {
|
||||
views: [
|
||||
{state: 'frozen', ySplit: 1}
|
||||
]
|
||||
});
|
||||
return sheet
|
||||
}
|
||||
|
||||
async AddHeader(Sheet, Level, libType) {
|
||||
const columns = []
|
||||
log.verbose(`AddHeader sheet: ${Sheet} - level: ${Level} - libType: ${libType}`)
|
||||
|
||||
// Get level fields
|
||||
const fields = et.getLevelFields(Level, libType)
|
||||
|
||||
|
||||
console.log('ged Field:s ' + JSON.stringify(fields))
|
||||
for (var i=0; i<fields.length; i++) {
|
||||
log.debug('Column: ' + fields[i] + ' - ' + fields[i])
|
||||
//let column = { header: Level[i], key: 'id', width: 10 }
|
||||
let column = { header: fields[i], key: fields[i] }
|
||||
columns.push(column)
|
||||
}
|
||||
Sheet.columns = columns
|
||||
// Add background to header column
|
||||
Sheet.getRow(1).fill = {
|
||||
type: 'pattern',
|
||||
pattern:'solid',
|
||||
fgColor:{ argb:'729fcf' }
|
||||
}
|
||||
|
||||
/* Sheet.autoFilter = {
|
||||
from: 'A1',
|
||||
to: 'D1',
|
||||
} */
|
||||
return true;
|
||||
}
|
||||
|
||||
async SaveWorkbook(Workbook, Library, Level, Type) {
|
||||
const fs = require('fs')
|
||||
const dateFormat = require('dateformat');
|
||||
const OutDir = wtconfig.get('ET.OutPath', wtutils.UserHomeDir)
|
||||
const timeStamp=dateFormat(new Date(), "yyyy.mm.dd_h.MM.ss");
|
||||
const name = OutDir + '/' + Library + '_' + Level + '_' + timeStamp + '.' + Type;
|
||||
log.debug('Saving output file as: ' + name)
|
||||
// Save Excel on Hard Disk
|
||||
Workbook.xlsx.writeBuffer()
|
||||
.then(buffer => fs.writeFileSync(name, buffer))
|
||||
return true
|
||||
}
|
||||
|
||||
async addRowToSheet(sheet, libType, level, data) {
|
||||
console.log('Start AddToSheet')
|
||||
// Placeholder for row
|
||||
let row = []
|
||||
let date, year, month, day, hours, minutes, seconds
|
||||
//year, month, day,
|
||||
// Need to find the fields and keys we'll
|
||||
// query the data for
|
||||
const keyVal = et.getFieldsKeyValType( libType, level)
|
||||
// Now get the medias
|
||||
//const nodes = jp.nodes(data, '$.MediaContainer.Metadata[*]')
|
||||
|
||||
const rowentry = {}
|
||||
for (var i=0; i<keyVal.length; i++) {
|
||||
// Get type
|
||||
let val
|
||||
console.log('Ged Field Type: ' + Object.values(keyVal[i])[0][1]);
|
||||
switch(Object.values(keyVal[i])[0][1]) {
|
||||
case "string":
|
||||
console.log('Ged Item: ' + JSON.stringify(data))
|
||||
val = jp.value(data, Object.values(keyVal[i])[0][0]);
|
||||
console.log('Ged1 Result: ' + val)
|
||||
break;
|
||||
case "array":
|
||||
// Get Items
|
||||
val = jp.query(data, Object.values(keyVal[i])[0][0]);
|
||||
// Seperate as wanted
|
||||
val = val.join(wtconfig.get('ET.ArraySep', ' - '))
|
||||
break;
|
||||
case "int":
|
||||
val = '';
|
||||
break;
|
||||
case "time":
|
||||
val = jp.value(data, Object.values(keyVal[i])[0][0]);
|
||||
if ( typeof val !== 'undefined' && val )
|
||||
{
|
||||
seconds = '0' + (Math.round(val/1000)%60).toString();
|
||||
minutes = '0' + (Math.round((val/(1000 * 60))) % 60).toString();
|
||||
hours = (Math.trunc(val / (1000 * 60 * 60)) % 24).toString();
|
||||
// Will display time in 10:30:23 format
|
||||
val = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
|
||||
}
|
||||
else
|
||||
{
|
||||
val = null
|
||||
}
|
||||
break;
|
||||
|
||||
case "datetime":
|
||||
val = jp.value(data, Object.values(keyVal[i])[0][0]);
|
||||
if ( typeof val !== 'undefined' && val )
|
||||
{
|
||||
// Create a new JavaScript Date object based on the timestamp
|
||||
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
|
||||
date = new Date(val * 1000);
|
||||
year = date.getFullYear().toString();
|
||||
month = ('0' + date.getMonth().toString()).substr(-2);
|
||||
day = ('0' + date.getDate().toString()).substr(-2);
|
||||
hours = date.getHours();
|
||||
minutes = "0" + date.getMinutes();
|
||||
seconds = "0" + date.getSeconds();
|
||||
// Will display time in 10:30:23 format
|
||||
val = year+'-'+month+'-'+day+' '+hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
|
||||
}
|
||||
else
|
||||
{
|
||||
val = null
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (val == null)
|
||||
{
|
||||
val = wtconfig.get('ET.NotAvail', 'N/A')
|
||||
}
|
||||
rowentry[Object.keys(keyVal[i])] = val
|
||||
}
|
||||
row.push(rowentry)
|
||||
|
||||
//console.log('Entire rows: ' + JSON.stringify(row))
|
||||
row.forEach(element => {
|
||||
excel.AddRow(sheet, element)
|
||||
});
|
||||
}
|
||||
|
||||
async createOutFile( libName, level, libType, outType, data, baseURL, accessToken )
|
||||
{
|
||||
console.log(`Ged CreateOutFile libName: ${libName} - level: ${level} - libType: ${libType}`)
|
||||
// First create a WorkBook
|
||||
const workBook = await excel2.NewExcelWorkBook()
|
||||
console.log(`Ged workBook: ${workBook}`);
|
||||
// Create Sheet
|
||||
let sheet = await excel2.NewSheet(workBook, libName, level)
|
||||
console.log(`Ged sheet: ${sheet}`);
|
||||
// Add the header to the sheet
|
||||
const header = await excel2.AddHeader(sheet, level, libType)
|
||||
console.log(`Ged header: ${header}`);
|
||||
// Now we need to find out how many calls to make
|
||||
const call = await et.getLevelCall(libType, level)
|
||||
console.log(`GED Count needed is: ${call}`)
|
||||
if ( call == 1 )
|
||||
{
|
||||
// Single call needed, so simply pass along the individual items
|
||||
const items = jp.nodes(data, '$.MediaContainer.Metadata[*]')
|
||||
for (var x=0; x<items.length; x++) {
|
||||
await excel2.addRowToSheet(sheet, libType, level, items[x]['value'])
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Ged multiple calls needed')
|
||||
// Get rating key for each item
|
||||
const urls = jp.query(data, '$.MediaContainer.Metadata[*].key');
|
||||
log.verbose('Items to lookup are: ' + urls)
|
||||
|
||||
for (const url of urls) {
|
||||
const contents = await et.getItemData(baseURL, accessToken, url);
|
||||
console.log('Ged gummi 123' + JSON.stringify(contents));
|
||||
const items = jp.nodes(data, '$.MediaContainer.Metadata[*]')
|
||||
for (var y=0; y<items.length; y++) {
|
||||
console.log('GED Item returned Multi: ' + JSON.stringify(items[y]['value']));
|
||||
excel2.addRowToSheet(sheet, libType, level, items[y]['value'])
|
||||
}
|
||||
}
|
||||
|
||||
console.log('GED Tommy Done')
|
||||
}
|
||||
|
||||
|
||||
data
|
||||
|
||||
// Save Excel file
|
||||
const result = await excel2.SaveWorkbook(workBook, libName, level, 'xlsx')
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export {et, excel, excel2};
|
20
src/main.js
20
src/main.js
|
@ -72,6 +72,8 @@ const libName = 'Ged'
|
|||
const level = 'Test Tommy'
|
||||
//const level = 'Level 2'
|
||||
const libType = 'movie'
|
||||
|
||||
libType
|
||||
/*
|
||||
Some hidden stuff during dev only
|
||||
I need baseurl of the server, as
|
||||
|
@ -101,12 +103,15 @@ like:
|
|||
const baseURI = wtconfig.get('Developer.baseURI', 'NO SERVER URI');
|
||||
const accessToken = wtconfig.get('Developer.accessToken', 'NO SERVER TOKEN');
|
||||
|
||||
|
||||
baseURI
|
||||
accessToken
|
||||
|
||||
// Real stuff to use
|
||||
|
||||
// ET Stuff
|
||||
import {excel} from './components/modules/ExportTools/et'
|
||||
//import {excel} from './components/modules/ExportTools/et'
|
||||
|
||||
import {excel2} from './components/modules/ExportTools/et'
|
||||
|
||||
const testimp3 = require('./components/modules/ExportTools/testimp3.json')
|
||||
const testimp = require('./components/modules/ExportTools/testimp.json')
|
||||
|
@ -120,16 +125,19 @@ testimp1
|
|||
|
||||
// EXCEL Stuff
|
||||
// Create WorkBook
|
||||
const WorkBook = excel.NewExcelFile()
|
||||
//const WorkBook = excel.NewExcelFile()
|
||||
// Create Sheet
|
||||
const Sheet = excel.NewSheet(WorkBook, libName, level)
|
||||
//const Sheet = excel.NewSheet(WorkBook, libName, level)
|
||||
// Now add the header column
|
||||
excel.AddHeader(Sheet, level, libType)
|
||||
//excel.AddHeader(Sheet, level, libType)
|
||||
// Add a couple of Rows
|
||||
//excel.addToSheet(Sheet, libType, level, testimp3, baseURI, accessToken)
|
||||
//excel.addToSheet(Sheet, libType, level, testimp, baseURI, accessToken)
|
||||
//excel.addToSheet(Sheet, libType, level, testimp1, baseURI, accessToken)
|
||||
excel.addToSheet(Sheet, libType, level, testimp1, baseURI, accessToken, WorkBook, libName, 'xlsx')
|
||||
console.log('Ged main call')
|
||||
//excel.addToSheet(Sheet, libType, level, testimp1, baseURI, accessToken, WorkBook, libName, 'xlsx')
|
||||
excel2.createOutFile( libName, level, libType, 'xlsx', testimp1, baseURI, accessToken );
|
||||
console.log('Ged main ended')
|
||||
// Save Excel file
|
||||
//excel.SaveWorkbook(WorkBook, libName, level, 'xlsx')
|
||||
|
||||
|
|
Loading…
Reference in a new issue