iptv-database/scripts/core/csv.js

106 lines
2 KiB
JavaScript
Raw Normal View History

2022-02-11 21:55:50 -05:00
const csv2json = require('csvtojson')
2022-02-17 09:33:24 -05:00
const chalk = require('chalk')
const logger = require('./logger')
2022-02-11 21:55:50 -05:00
const fs = require('mz/fs')
const {
Parser,
transforms: { flatten },
formatters: { stringQuoteOnlyIfNecessary }
} = require('json2csv')
const csv2jsonOptions = {
checkColumn: true,
trim: true,
2022-02-13 22:05:45 -05:00
delimiter: ',',
2022-02-17 09:33:24 -05:00
eol: '\r\n',
2022-02-11 21:55:50 -05:00
colParser: {
countries: listParser,
languages: listParser,
categories: listParser,
broadcast_area: listParser,
is_nsfw: boolParser,
logo: nullable,
subdivision: nullable,
city: nullable,
2022-02-19 23:05:50 -05:00
network: nullable,
2022-10-17 09:24:36 -04:00
owners: listParser,
2022-04-11 16:30:56 -04:00
launched: nullable,
closed: nullable,
2022-03-01 04:33:29 -05:00
website: nullable,
2022-04-21 14:46:44 -04:00
native_name: nullable,
replaced_by: nullable
2022-02-11 21:55:50 -05:00
}
}
const json2csv = new Parser({
2022-02-19 23:05:50 -05:00
transforms: [flattenArray, formatBool],
2022-02-11 21:55:50 -05:00
formatters: {
string: stringQuoteOnlyIfNecessary()
2022-02-19 23:05:50 -05:00
},
eol: '\r\n'
2022-02-11 21:55:50 -05:00
})
const csv = {}
2022-02-17 09:33:24 -05:00
csv.fromFile = async function (filepath) {
2022-02-11 21:55:50 -05:00
return csv2json(csv2jsonOptions).fromFile(filepath)
}
2022-02-17 09:33:24 -05:00
csv.fromString = async function (filepath) {
return csv2json(csv2jsonOptions).fromString(filepath)
}
2022-02-11 21:55:50 -05:00
csv.save = async function (filepath, data) {
const string = json2csv.parse(data)
return fs.writeFile(filepath, string)
}
csv.saveSync = function (filepath, data) {
const string = json2csv.parse(data)
return fs.writeFileSync(filepath, string)
}
module.exports = csv
function flattenArray(item) {
for (let prop in item) {
const value = item[prop]
item[prop] = Array.isArray(value) ? value.join(';') : value
}
return item
}
2022-02-19 23:05:50 -05:00
function formatBool(item) {
for (let prop in item) {
if (item[prop] === false) {
item[prop] = 'FALSE'
} else if (item[prop] === true) {
item[prop] = 'TRUE'
}
}
return item
}
2022-02-11 21:55:50 -05:00
function listParser(value) {
return value.split(';').filter(i => i)
}
function boolParser(value) {
2022-02-16 14:58:23 -05:00
switch (value) {
case 'TRUE':
return true
case 'FALSE':
return false
default:
return value
}
2022-02-11 21:55:50 -05:00
}
function nullable(value) {
return value === '' ? null : value
}