import * as cheerio from "cheerio" ; import { ListingsGetter , ListingsInterface } from "../../decorators/listings.decorator" ; import { OutputGetter , OutputInterface } from "../../decorators/output.decorator" ; import { trimRemoveLineBreaks } from "../../helpers" ; import { BoxrecLocation } from "../boxrec.constants" ; import { BoxrecPageVenueEventsRowOutput , BoxrecVenueOutput } from "./boxrec.page.venue.constants" ; import { BoxrecPageVenueEventsRow } from "./boxrec.page.venue.events.row" ; /**
 * parse a BoxRec Venue page
 * <pre>ex. http://boxrec.com/en/venue/38555</pre>
 */ @ ListingsGetter ( "events" , BoxrecPageVenueEventsRow , "#eventsTable" ) @ OutputGetter ( [ { function : ( events ) => events . map ( ( event ) => event . output ) , method : "events" , } , "localBoxers" , "localManagers" , "location" , "name" ] ) export class BoxrecPageVenue implements ListingsInterface , OutputInterface { output : BoxrecVenueOutput ; returns { BoxrecPageVenueEventsRow [ ] } events : BoxrecPageVenueEventsRow [ ] ; private readonly $ : CheerioStatic ; constructor ( boxrecBodyString ) { this . $ = cheerio . load ( boxrecBodyString ) ; } returns { Object } get localBoxers ( ) : Array < { id : number ; name : string ; } > { return this . parseBasicInfo ( "boxer" ) ; } returns { Object } get localManagers ( ) : Array < { id : number ; name : string ; } > { return this . parseBasicInfo ( "manager" ) ; } get location ( ) : BoxrecLocation { return this . events [ 0 ] . location ; } get name ( ) : string { return trimRemoveLineBreaks ( this . $ ( ".pageOuter h1" ) . text ( ) ) ; } private parseBasicInfo ( type ) { const links = this . $ ( ".filterBarFloat div:nth-child(2) a" ) ; const results = [ ] ; links . each ( ( i , elem ) => { const href = this . $ ( elem ) . attr ( "href" ) ; const matches = href . match ( /(\d+)$/ ) ; let person = null ; if ( matches && matches [ 1 ] ) { person = { id : parseInt ( matches [ 1 ] , 10 ) , name : trimRemoveLineBreaks ( this . $ ( elem ) . text ( ) ) , } ; if ( href . includes ( type ) ) { results . push ( person ) ; } } } ) ; return results ; } }