import * as cheerio from "cheerio" ; import * as querystring from "querystring" ; import { BoxrecBasic , BoxrecJudge , BoxrecLocation , Record , WinLossDraw } from "../boxrec-pages/boxrec.constants" ; import { WeightDivision } from "../boxrec-pages/champions/boxrec.champions.constants" ; import { BoxingBoutOutcome , BoxingBoutOutcomeKeys } from "../boxrec-pages/event/boxrec.event.constants" ; import { convertFractionsToNumber , replaceWithWeight , trimRemoveLineBreaks , whatTypeOfLink } from "../helpers" ; import { BoxrecTitles } from "./boxrec-common.constants" ; const $ = cheerio . load ( "<div></div>" ) ; /**
 * Used to parse common columns
 */ export abstract class BoxrecCommonTablesColumnsClass { hidden static parseAlias ( htmlString ) { if ( htmlString ) { return trimRemoveLineBreaks ( htmlString ) ; } return null ; } hidden static parseCareer ( htmlString ) { const careerMatches = htmlString . match ( /(\d{4})-(\d{4})/ ) ; if ( careerMatches && careerMatches . length === 3 ) { return [ parseInt ( careerMatches [ 1 ] , 10 ) , parseInt ( careerMatches [ 2 ] , 10 ) ] ; } return [ null , null ] ; } param htmlString return static parseDivision ( htmlString ) { const cleanedDivision = trimRemoveLineBreaks ( htmlString . toLowerCase ( ) ) ; const withWeight = ! /weight$/ . test ( cleanedDivision ) ? `${ cleanedDivision }weight` : cleanedDivision ; const fullWeightDivision = Object . values ( WeightDivision ) . find ( item => item === withWeight ) ; if ( fullWeightDivision ) { return fullWeightDivision ; } return null ; } static parseId ( htmlString ) { const html = $ ( `<div>${ htmlString }</div>` ) ; const href = html . find ( "a" ) . attr ( "href" ) ; if ( href ) { const matches = href . match ( /(\d+)$/ ) ; if ( matches && matches [ 1 ] ) { return parseInt ( matches [ 1 ] , 10 ) ; } } return null ; } hidden static parseJudges ( htmlString ) { const html = $ ( `<div>${ htmlString }</div>` ) ; const judges = [ ] ; html . find ( "a" ) . each ( ( index , elem ) => { const href = $ ( elem ) . get ( 0 ) . attribs . href ; const matches = href . match ( /judge\/(\d+)$/ ) ; if ( matches && matches [ 1 ] ) { const { id , name } = BoxrecCommonTablesColumnsClass . parseIdAndName ( elem , matches ) ; const scoreCardRegex = /<\/a>\s(\d{1,3})-(\d{1,3})/g ; const scorecard = [ ] ; let m ; let i = 0 ; do { m = scoreCardRegex . exec ( htmlString ) ; if ( m && m [ 1 ] && m [ 2 ] ) { if ( i === judges . length ) { scorecard . push ( parseInt ( m [ 1 ] , 10 ) , parseInt ( m [ 2 ] , 10 ) ) ; } } i ++ ; } while ( m ) ; judges . push ( { id , name , scorecard , } ) ; } } ) ; return judges ; } static parseLast6Column ( htmlString ) { const last6 = [ ] ; $ ( htmlString ) . find ( "div.last6" ) . each ( ( i , elem ) => { const className = elem . attribs . class ; if ( className . includes ( "bgW" ) ) { last6 . push ( WinLossDraw . win ) ; } else if ( className . includes ( "bgD" ) ) { last6 . push ( WinLossDraw . draw ) ; } else if ( className . includes ( "bgL" ) ) { last6 . push ( WinLossDraw . loss ) ; } else { last6 . push ( WinLossDraw . unknown ) ; } } ) ; return last6 ; } param { string } htmlString param { number } linkToLookAt hidden returns { BoxrecLocation } static parseLocationLink ( htmlString , linkToLookAt = 0 ) { const location = { country : { id : null , name : null , } , region : { id : null , name : null , } , town : { id : null , name : null , } , } ; const html = $ ( `<div>${ htmlString }</div>` ) ; const links = html . find ( "a" ) ; links . each ( ( index , elem ) => { const href = $ ( elem ) . attr ( "href" ) ; const text = $ ( elem ) . text ( ) ; const whatTypeOfLinkResult = whatTypeOfLink ( href ) ; const matchQSKeyVals = href . match ( /(\w+)\=(\w+)/g ) ; if ( matchQSKeyVals ) { const idParsed = matchQSKeyVals . map ( item => querystring . parse ( item ) ) . filter ( item => Object . keys ( item ) [ 0 ] === whatTypeOfLinkResult ) . map ( item => { const val = Object . values ( item ) [ 0 ] as string ; const testValIfNumber = parseInt ( val , 10 ) ; if ( isNaN ( testValIfNumber ) ) { return val ; } return testValIfNumber ; } ) [ 0 ] ; location [ whatTypeOfLinkResult ] = { id : idParsed , name : text , } ; } } ) ; return location ; } static parseName ( htmlString ) { const html = $ ( `<div>${ htmlString }</div>` ) ; let name = html . text ( ) ; name = trimRemoveLineBreaks ( name ) ; if ( name . slice ( - 1 ) === "*" ) { name = name . slice ( 0 , - 1 ) ; } return name ; } hidden static parseNameAndId ( htmlString ) { if ( trimRemoveLineBreaks ( htmlString ) !== "TBA" ) { const html = $ ( `<div>${ htmlString }</div>` ) ; const href = html . find ( "a" ) . attr ( "href" ) ; const regex = /(\d+)$/ ; if ( href ) { const matches = href . match ( regex ) ; if ( matches && matches [ 1 ] ) { return { id : parseInt ( matches [ 1 ] , 10 ) , name : html . find ( "a" ) . text ( ) , } ; } } } return { id : null , name : null , } ; } hidden static parseNumberOfRounds ( htmlString ) { let numberOfRounds = [ null , null ] ; const splitRounds = htmlString . trim ( ) . split ( "/" ) ; const formattedSplitRounds = splitRounds . map ( item => parseInt ( item , 10 ) ) ; if ( formattedSplitRounds . length === 2 ) { numberOfRounds = formattedSplitRounds ; } else if ( formattedSplitRounds . length === 1 && ! isNaN ( formattedSplitRounds [ 0 ] ) ) { numberOfRounds = [ null , formattedSplitRounds [ 0 ] ] ; } return numberOfRounds ; } hidden static parseOutcome ( htmlString ) { let outcome = htmlString ; outcome = outcome . trim ( ) ; switch ( outcome ) { case "W" : return WinLossDraw . win ; case "D" : return WinLossDraw . draw ; case "L" : return WinLossDraw . loss ; case "S" : return WinLossDraw . scheduled ; default : return WinLossDraw . unknown ; } } hidden static parseOutcomeByWayOf ( htmlString , parseText = false ) { if ( trimRemoveLineBreaks ( htmlString ) ) { const html = $ ( `<div>${ htmlString }</div>` ) ; html . find ( "div" ) . remove ( ) ; let outcomeByWayOf = html . text ( ) ; outcomeByWayOf = outcomeByWayOf . trim ( ) ; if ( parseText ) { return BoxingBoutOutcome [ outcomeByWayOf as BoxingBoutOutcomeKeys ] ; } return outcomeByWayOf ; } return null ; } static parseRating ( htmlString ) { const html = $ ( htmlString ) ; let rating = null ; const starRating = html . find ( ".starRating" ) ; if ( starRating && starRating . get ( 0 ) ) { const widthString = starRating . get ( 0 ) . attribs . style ; if ( widthString ) { const regex = /width:\s(\d+)%;/ ; const widthMatch = widthString . match ( regex ) ; if ( widthMatch && widthMatch [ 1 ] ) { rating = parseInt ( widthMatch [ 1 ] , 10 ) ; } } } return rating ; } static parseRecord ( htmlString ) { const record = { draw : null , loss : null , win : null , } ; const html = $ ( `<div>${ htmlString }</div>` ) ; if ( html . find ( ".textWon" ) . length && ! htmlString . includes ( "debut" ) ) { record . win = parseInt ( html . find ( ".textWon" ) . text ( ) , 10 ) ; record . loss = parseInt ( html . find ( ".textLost" ) . text ( ) , 10 ) ; record . draw = parseInt ( html . find ( ".textDraw" ) . text ( ) , 10 ) ; } else if ( htmlString . includes ( "debut" ) ) { record . win = 0 ; record . loss = 0 ; record . draw = 0 ; } return record ; } hidden static parseReferee ( htmlString ) { const html = $ ( `<div>${ htmlString }</div>` ) ; let referee = { id : null , name : null , } ; html . find ( "a" ) . each ( ( index , elem ) => { const href = $ ( elem ) . get ( 0 ) . attribs . href ; const matches = href . match ( /referee\/(\d+)$/ ) ; if ( matches && matches [ 1 ] ) { referee = BoxrecCommonTablesColumnsClass . parseIdAndName ( elem , matches ) ; } } ) ; return referee ; } hidden static parseTitles ( htmlString ) { const titles = [ ] ; const html = $ ( `<div>${ htmlString }</div>` ) ; let titleIndexAt = 0 ; html . find ( "a" ) . each ( ( index , elem ) => { const { class : className , href } = $ ( elem ) . get ( 0 ) . attribs ; if ( className && className . includes ( "titleLink" ) ) { const matches = href . match ( /(\d+\/[\w%]+)$/ ) ; if ( matches && matches [ 1 ] ) { const id = matches [ 1 ] ; let name = $ ( elem ) . text ( ) ; name = replaceWithWeight ( name ) ; titles . push ( { id , name , } ) ; titleIndexAt = titles . length - 1 ; } } else if ( href . includes ( "supervisor" ) ) { const matches = href . match ( /\d+$/ ) ; if ( matches ) { titles [ titleIndexAt ] . supervisor = { id : parseInt ( matches [ 0 ] , 10 ) , name : replaceWithWeight ( $ ( elem ) . text ( ) ) , } ; } } } ) ; return titles ; } hidden static parseWeight ( htmlString ) { const weight = htmlString . trim ( ) ; let formattedWeight = null ; if ( weight . length ) { formattedWeight = parseInt ( weight , 10 ) ; if ( isNaN ( parseInt ( weight . slice ( - 1 ) , 10 ) ) ) { formattedWeight += convertFractionsToNumber ( weight . slice ( - 1 ) ) ; } } return formattedWeight ; } private static parseIdAndName ( elem , matches ) { const id = parseInt ( matches [ 1 ] , 10 ) ; let name = $ ( elem ) . text ( ) ; name = trimRemoveLineBreaks ( name ) ; return { id , name , } ; } }