Update scripts

This commit is contained in:
freearhey 2023-10-08 02:13:58 +03:00
parent 0feb6b1e89
commit f0cbc45e14
4 changed files with 29 additions and 34 deletions

17
scripts/core/idCreator.ts Normal file
View file

@ -0,0 +1,17 @@
export class IDCreator {
create(name: string, country: string): string {
const slug = normalize(name)
const code = country.toLowerCase()
return `${slug}.${code}`
}
}
function normalize(name: string) {
return name
.replace(/^@/gi, 'At')
.replace(/^&/i, 'And')
.replace(/\+/gi, 'Plus')
.replace(/\s-(\d)/gi, ' Minus$1')
.replace(/[^a-z\d]+/gi, '')
}

View file

@ -2,3 +2,4 @@ export * from './csv'
export * from './issueParser'
export * from './issueLoader'
export * from './csvParser'
export * from './idCreator'

View file

@ -1,4 +1,4 @@
import { CSV, IssueLoader, CSVParser } from '../core'
import { CSV, IssueLoader, CSVParser, IDCreator } from '../core'
import { Channel, Blocked, Issue } from '../models'
import { DATA_DIR } from '../constants'
import { Storage, Collection } from '@freearhey/core'
@ -8,6 +8,7 @@ let channels = new Collection()
const processedIssues = new Collection()
async function main() {
const idCreator = new IDCreator()
const dataStorage = new Storage(DATA_DIR)
const parser = new CSVParser()
@ -20,8 +21,8 @@ async function main() {
const loader = new IssueLoader()
await removeChannels({ loader })
await editChannels({ loader })
await addChannels({ loader })
await editChannels({ loader, idCreator })
await addChannels({ loader, idCreator })
await blockChannels({ loader })
await unblockChannels({ loader })
@ -65,7 +66,7 @@ async function removeChannels({ loader }: { loader: IssueLoader }) {
})
}
async function editChannels({ loader }: { loader: IssueLoader }) {
async function editChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) {
const issues = await loader.load({ labels: ['channels:edit,approved'] })
issues.forEach((issue: Issue) => {
const data = issue.data
@ -80,7 +81,7 @@ async function editChannels({ loader }: { loader: IssueLoader }) {
if (data.has('name') || data.has('country')) {
const name = data.get('name') || found.name
const country = data.get('country') || found.country
channelId = generateChannelId(name, country)
channelId = idCreator.create(name, country)
}
found.update({
@ -107,13 +108,13 @@ async function editChannels({ loader }: { loader: IssueLoader }) {
})
}
async function addChannels({ loader }: { loader: IssueLoader }) {
async function addChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) {
const issues = await loader.load({ labels: ['channels:add,approved'] })
issues.forEach((issue: Issue) => {
const data = issue.data
if (data.missing('name') || data.missing('country')) return
const channelId = generateChannelId(data.get('name'), data.get('country'))
const channelId = idCreator.create(data.get('name'), data.get('country'))
const found: Channel = channels.first((channel: Channel) => channel.id === channelId)
if (found) return
@ -182,13 +183,3 @@ async function blockChannels({ loader }: { loader: IssueLoader }) {
processedIssues.push(issue)
})
}
function generateChannelId(name: string, country: string): string {
const slug = name
.replace(/\+/gi, 'Plus')
.replace(/^@/gi, 'At')
.replace(/[^a-z\d]+/gi, '')
country = country.toLowerCase()
return `${slug}.${country}`
}

View file

@ -1,9 +1,8 @@
import { Collection, Storage, File, Dictionary, Logger } from '@freearhey/core'
import { DATA_DIR } from '../constants'
import { transliterate } from 'transliteration'
import { program } from 'commander'
import Joi from 'joi'
import { CSVParser } from '../core'
import { CSVParser, IDCreator } from '../core'
import chalk from 'chalk'
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
@ -217,28 +216,15 @@ function findDuplicatesBy(rows: { [key: string]: string }[], key: string) {
function validateChannelId(row: { [key: string]: string }, i: number) {
const errors = new Collection()
const name = normalize(row.name)
const code = row.country.toLowerCase()
const expected = `${name}.${code}`
const expectedId = new IDCreator().create(row.name, row.country)
if (expected !== row.id) {
if (expectedId !== row.id) {
errors.push({
line: i + 2,
message: `"${row.id}" must be derived from the channel name "${row.name}" and the country code "${row.country}"`
})
}
function normalize(name: string) {
const translit = transliterate(name)
return translit
.replace(/^@/i, 'At')
.replace(/^&/i, 'And')
.replace(/\+/gi, 'Plus')
.replace(/\s-(\d)/gi, ' Minus$1')
.replace(/[^a-z\d]+/gi, '')
}
return errors
}