Update scripts

This commit is contained in:
freearhey 2023-10-16 14:45:12 +03:00
parent e2a5105e69
commit 4d5c6fee64
7 changed files with 140 additions and 104 deletions

View file

@ -3,3 +3,5 @@ export * from './issueParser'
export * from './issueLoader' export * from './issueLoader'
export * from './csvParser' export * from './csvParser'
export * from './idCreator' export * from './idCreator'
export * from './issueData'
export * from './issue'

View file

@ -1,15 +1,15 @@
import { Dictionary } from '@freearhey/core' import { IssueData } from './'
type IssueProps = { type IssueProps = {
number: number number: number
labels: string[] labels: string[]
data: Dictionary data: IssueData
} }
export class Issue { export class Issue {
number: number number: number
labels: string[] labels: string[]
data: Dictionary data: IssueData
constructor({ number, labels, data }: IssueProps) { constructor({ number, labels, data }: IssueProps) {
this.number = number this.number = number

40
scripts/core/issueData.ts Normal file
View file

@ -0,0 +1,40 @@
import { Dictionary } from '@freearhey/core'
export class IssueData {
_data: Dictionary
constructor(data: Dictionary) {
this._data = data
}
has(key: string): boolean {
return this._data.has(key)
}
missing(key: string): boolean {
return this._data.missing(key) || this._data.get(key) === undefined
}
getBoolean(key: string): boolean | undefined {
return this.missing(key) ? undefined : this._data.get(key)
}
getString(key: string): string | undefined {
const deleteSymbol = '~'
return this.missing(key)
? undefined
: this._data.get(key) === deleteSymbol
? ''
: this._data.get(key)
}
getArray(key: string): string[] | undefined {
const deleteSymbol = '~'
return this.missing(key)
? undefined
: this._data.get(key) === deleteSymbol
? []
: this._data.get(key).split(';')
}
}

View file

@ -1,5 +1,5 @@
import { Dictionary } from '@freearhey/core' import { Dictionary } from '@freearhey/core'
import { Issue } from '../models' import { IssueData, Issue } from '../core'
const FIELDS = new Dictionary({ const FIELDS = new Dictionary({
'Channel ID': 'channel_id', 'Channel ID': 'channel_id',
@ -52,7 +52,8 @@ export class IssueParser {
if (!_label || !_value) return data if (!_label || !_value) return data
const id: string = FIELDS.get(_label) const id: string = FIELDS.get(_label)
const value: string = _value === '_No response_' || _value === 'None' ? '' : _value const value: string | undefined =
_value === '_No response_' || _value === 'None' ? undefined : _value
if (!id) return if (!id) return
@ -61,6 +62,6 @@ export class IssueParser {
const labels = issue.labels.map(label => label.name) const labels = issue.labels.map(label => label.name)
return new Issue({ number: issue.number, labels, data }) return new Issue({ number: issue.number, labels, data: new IssueData(data) })
} }
} }

View file

@ -1,5 +1,5 @@
import { CSV, IssueLoader, CSVParser, IDCreator } from '../core' import { CSV, IssueLoader, CSVParser, IDCreator, Issue, IssueData } from '../core'
import { Channel, Blocked, Issue } from '../models' import { Channel, Blocked } from '../models'
import { DATA_DIR } from '../constants' import { DATA_DIR } from '../constants'
import { Storage, Collection } from '@freearhey/core' import { Storage, Collection } from '@freearhey/core'
@ -57,7 +57,9 @@ async function removeChannels({ loader }: { loader: IssueLoader }) {
issues.forEach((issue: Issue) => { issues.forEach((issue: Issue) => {
if (issue.data.missing('channel_id')) return if (issue.data.missing('channel_id')) return
const found = channels.first((channel: Channel) => channel.id === issue.data.get('channel_id')) const found = channels.first(
(channel: Channel) => channel.id === issue.data.getString('channel_id')
)
if (!found) return if (!found) return
channels.remove((channel: Channel) => channel.id === found.id) channels.remove((channel: Channel) => channel.id === found.id)
@ -69,55 +71,41 @@ async function removeChannels({ loader }: { loader: IssueLoader }) {
async function editChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) { async function editChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) {
const issues = await loader.load({ labels: ['channels:edit,approved'] }) const issues = await loader.load({ labels: ['channels:edit,approved'] })
issues.forEach((issue: Issue) => { issues.forEach((issue: Issue) => {
const data = issue.data const data: IssueData = issue.data
if (data.missing('channel_id')) return if (data.missing('channel_id')) return
const found: Channel = channels.first( const found: Channel = channels.first(
(channel: Channel) => channel.id === data.get('channel_id') (channel: Channel) => channel.id === data.getString('channel_id')
) )
if (!found) return if (!found) return
let channelId = found.id let channelId = found.id
if (data.has('name') || data.has('country')) { if (data.has('name') || data.has('country')) {
const name = data.get('name') || found.name const name = data.getString('name') || found.name
const country = data.get('country') || found.country const country = data.getString('country') || found.country
if (name && country) {
channelId = idCreator.create(name, country) channelId = idCreator.create(name, country)
} }
}
const deleteSymbol = '~'
const updated = new Channel({ const updated = new Channel({
id: channelId, id: channelId,
name: data.get('name') !== deleteSymbol ? data.get('name') : '', name: data.getString('name'),
alt_names: alt_names: data.getArray('alt_names'),
data.has('alt_names') && data.get('alt_names') !== deleteSymbol network: data.getString('network'),
? data.get('alt_names').split(';') owners: data.getArray('owners'),
: [], country: data.getString('country'),
network: data.get('network') !== deleteSymbol ? data.get('network') : '', subdivision: data.getString('subdivision'),
owners: city: data.getString('city'),
data.has('owners') && data.get('owners') !== deleteSymbol broadcast_area: data.getArray('broadcast_area'),
? data.get('owners').split(';') languages: data.getArray('languages'),
: [], categories: data.getArray('categories'),
country: data.get('country') !== deleteSymbol ? data.get('country') : '', is_nsfw: data.getBoolean('is_nsfw'),
subdivision: data.get('subdivision') !== deleteSymbol ? data.get('subdivision') : '', launched: data.getString('launched'),
city: data.get('city') !== deleteSymbol ? data.get('city') : '', closed: data.getString('closed'),
broadcast_area: replaced_by: data.getString('replaced_by'),
data.has('broadcast_area') && data.get('broadcast_area') !== deleteSymbol website: data.getString('website'),
? data.get('broadcast_area').split(';') logo: data.getString('logo')
: [],
languages:
data.has('languages') && data.get('languages') !== deleteSymbol
? data.get('languages').split(';')
: [],
categories:
data.has('categories') && data.get('categories') !== deleteSymbol
? data.get('categories').split(';')
: [],
is_nsfw: data.has('is_nsfw') ? data.get('is_nsfw') === 'TRUE' : false,
launched: data.get('launched') !== deleteSymbol ? data.get('launched') : '',
closed: data.get('closed') !== deleteSymbol ? data.get('closed') : '',
replaced_by: data.get('replaced_by') !== deleteSymbol ? data.get('replaced_by') : '',
website: data.get('website') !== deleteSymbol ? data.get('website') : '',
logo: data.get('logo') !== deleteSymbol ? data.get('logo') : ''
}) })
found.merge(updated) found.merge(updated)
@ -129,10 +117,13 @@ async function editChannels({ loader, idCreator }: { loader: IssueLoader; idCrea
async function addChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) { async function addChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) {
const issues = await loader.load({ labels: ['channels:add,approved'] }) const issues = await loader.load({ labels: ['channels:add,approved'] })
issues.forEach((issue: Issue) => { issues.forEach((issue: Issue) => {
const data = issue.data const data: IssueData = issue.data
if (data.missing('name') || data.missing('country')) return const name = data.getString('name')
const country = data.getString('country')
const channelId = idCreator.create(data.get('name'), data.get('country')) if (!name || !country) return
const channelId = idCreator.create(name, country)
const found: Channel = channels.first((channel: Channel) => channel.id === channelId) const found: Channel = channels.first((channel: Channel) => channel.id === channelId)
if (found) return if (found) return
@ -140,22 +131,22 @@ async function addChannels({ loader, idCreator }: { loader: IssueLoader; idCreat
channels.push( channels.push(
new Channel({ new Channel({
id: channelId, id: channelId,
name: data.get('name'), name: data.getString('name'),
alt_names: data.get('alt_names'), alt_names: data.getArray('alt_names'),
network: data.get('network'), network: data.getString('network'),
owners: data.get('owners'), owners: data.getArray('owners'),
country: data.get('country'), country: data.getString('country'),
subdivision: data.get('subdivision'), subdivision: data.getString('subdivision'),
city: data.get('city'), city: data.getString('city'),
broadcast_area: data.get('broadcast_area'), broadcast_area: data.getArray('broadcast_area'),
languages: data.get('languages'), languages: data.getArray('languages'),
categories: data.get('categories'), categories: data.getArray('categories'),
is_nsfw: data.get('is_nsfw'), is_nsfw: data.getBoolean('is_nsfw'),
launched: data.get('launched'), launched: data.getString('launched'),
closed: data.get('closed'), closed: data.getString('closed'),
replaced_by: data.get('replaced_by'), replaced_by: data.getString('replaced_by'),
website: data.get('website'), website: data.getString('website'),
logo: data.get('logo') logo: data.getString('logo')
}) })
) )
@ -170,7 +161,7 @@ async function unblockChannels({ loader }: { loader: IssueLoader }) {
if (data.missing('channel_id')) return if (data.missing('channel_id')) return
const found: Blocked = blocklist.first( const found: Blocked = blocklist.first(
(blocked: Blocked) => blocked.channel === data.get('channel_id') (blocked: Blocked) => blocked.channel === data.getString('channel_id')
) )
if (!found) return if (!found) return
@ -187,14 +178,18 @@ async function blockChannels({ loader }: { loader: IssueLoader }) {
if (data.missing('channel_id')) return if (data.missing('channel_id')) return
const found: Blocked = blocklist.first( const found: Blocked = blocklist.first(
(blocked: Blocked) => blocked.channel === data.get('channel_id') (blocked: Blocked) => blocked.channel === data.getString('channel_id')
) )
if (found) return if (found) return
const channel = data.getString('channel_id')
const ref = data.getString('ref')
if (!channel || !ref) return
blocklist.push( blocklist.push(
new Blocked({ new Blocked({
channel: data.get('channel_id'), channel,
ref: data.get('ref') ref
}) })
) )

View file

@ -1,41 +1,41 @@
type ChannelProps = { type ChannelProps = {
id: string id: string
name: string name?: string
alt_names: string[] alt_names?: string[]
network: string network?: string
owners: string[] owners?: string[]
country: string country?: string
subdivision: string subdivision?: string
city: string city?: string
broadcast_area: string[] broadcast_area?: string[]
languages: string[] languages?: string[]
categories: string[] categories?: string[]
is_nsfw: boolean is_nsfw?: boolean
launched: string launched?: string
closed: string closed?: string
replaced_by: string replaced_by?: string
website: string website?: string
logo: string logo?: string
} }
export class Channel { export class Channel {
id: string id: string
name: string name?: string
alt_names: string[] alt_names?: string[]
network: string network?: string
owners: string[] owners?: string[]
country: string country?: string
subdivision: string subdivision?: string
city: string city?: string
broadcast_area: string[] broadcast_area?: string[]
languages: string[] languages?: string[]
categories: string[] categories?: string[]
is_nsfw: boolean is_nsfw?: boolean
launched: string launched?: string
closed: string closed?: string
replaced_by: string replaced_by?: string
website: string website?: string
logo: string logo?: string
constructor({ constructor({
id, id,
@ -82,10 +82,9 @@ export class Channel {
} }
merge(channel: Channel) { merge(channel: Channel) {
const data: { [key: string]: string | string[] | boolean } = channel.data() const data: { [key: string]: string | string[] | boolean | undefined } = channel.data()
for (const prop in data) { for (const prop in data) {
if (data[prop] === undefined) continue if (data[prop] === undefined) continue
if (Array.isArray(data[prop]) && !(data[prop] as string[]).length) continue
this[prop] = data[prop] this[prop] = data[prop]
} }
} }

View file

@ -1,3 +1,2 @@
export * from './channel' export * from './channel'
export * from './issue'
export * from './blocked' export * from './blocked'