import * as cheerio from "cheerio" ; import { BoxrecCommonTablesColumnsClass } from "../../boxrec-common-tables/boxrec-common-tables-columns.class" ; import { OutputGetter , OutputInterface } from "../../decorators/output.decorator" ; import { changeToCamelCase , trimRemoveLineBreaks } from "../../helpers" ; import { BoxrecBelts , BoxrecChampion , BoxrecChampionsByWeightDivision , BoxrecChampionsOutput , BoxrecUnformattedChampions } from "./boxrec.champions.constants" ; const beltOrganizations = { BoxRec : null , IBF : null , IBO : null , WBA : null , WBC : null , WBO : null , } ; const weightDivisions = { bantamweight : beltOrganizations , cruiserweight : beltOrganizations , featherWeight : beltOrganizations , flyweight : beltOrganizations , heavyweight : beltOrganizations , lightFlyweight : beltOrganizations , lightHeavyweight : beltOrganizations , lightweight : beltOrganizations , middleweight : beltOrganizations , minimumweight : beltOrganizations , superBantamweight : beltOrganizations , superFeatherweight : beltOrganizations , superFlyweight : beltOrganizations , superLightweight : beltOrganizations , superMiddleweight : beltOrganizations , superWelterweight : beltOrganizations , welterweight : beltOrganizations , } ; @ OutputGetter ( [ "boxingOrganizations" , "byWeightDivision" , "champions" , ] ) export class BoxrecPageChampions implements OutputInterface { output : BoxrecChampionsOutput ; private readonly $ : CheerioStatic ; constructor ( boxrecBodyString ) { this . $ = cheerio . load ( boxrecBodyString ) ; } get boxingOrganizations ( ) : string [ ] { return this . parseBoxingOrganizations ( ) ; } get byWeightDivision ( ) : BoxrecChampionsByWeightDivision { const championsFormatted = weightDivisions ; const champions = this . parseChampions ( ) ; for ( const weightDivision of champions ) { if ( weightDivision . weightDivision ) { const weightDivisionFormatted = changeToCamelCase ( weightDivision . weightDivision ) ; ( championsFormatted as any ) [ weightDivisionFormatted ] = weightDivision . beltHolders ; } } return championsFormatted ; } get champions ( ) : BoxrecUnformattedChampions [ ] { return this . parseChampions ( ) ; } private parseBoxingOrganizations ( ) { const listOfBoxingOrganizations = [ ] ; this . $ ( ".dataTable th:contains(BoxRec)" ) . first ( ) . siblings ( ) . each ( ( index , elem ) => { let boxingOrganization = this . $ ( elem ) . text ( ) ; boxingOrganization = boxingOrganization . trim ( ) ; if ( boxingOrganization . length > 0 ) { listOfBoxingOrganizations . push ( boxingOrganization ) ; } } ) ; if ( listOfBoxingOrganizations . length ) { listOfBoxingOrganizations . unshift ( "BoxRec" ) ; } return listOfBoxingOrganizations ; } private parseChampions ( ) { const champions = [ ] ; const listOfBoxingOrganizations = this . parseBoxingOrganizations ( ) ; this . $ ( ".dataTable tr:nth-child(4n+3)" ) . each ( ( index , elem ) => { const weightDivision = this . $ ( elem ) . prev ( ) . prev ( ) . find ( "td:nth-child(1)" ) . text ( ) ; champions . push ( { beltHolders : Object . assign ( { } , beltOrganizations ) , weightDivision : BoxrecCommonTablesColumnsClass . parseDivision ( weightDivision ) , } ) ; const last = champions . length - 1 ; this . $ ( elem ) . find ( "td" ) . each ( ( tdIndex , tdElem ) => { if ( tdIndex !== 0 ) { const boxer = { id : null , name : null , picture : null , } ; const firstLink = this . $ ( tdElem ) . find ( "a:nth-child(1)" ) ; if ( firstLink . length ) { let name = this . $ ( tdElem ) . html ( ) ; if ( name ) { name = name . replace ( "<br>" , " " ) ; name = this . $ ( name ) . text ( ) ; name = trimRemoveLineBreaks ( name ) ; if ( firstLink [ 0 ] && firstLink [ 0 ] . attribs ) { const href = firstLink [ 0 ] . attribs . href . match ( /(\d+)$/ ) ; const picture = this . $ ( tdElem ) . find ( "img" ) . attr ( "src" ) ; if ( href && href [ 1 ] ) { boxer . id = parseInt ( href [ 1 ] , 10 ) ; boxer . name = name ; boxer . picture = picture ; ( champions as any ) [ last ] . beltHolders [ listOfBoxingOrganizations [ tdIndex - 1 ] ] = boxer ; } } } } } } ) ; } ) ; return champions ; } }