import { BoxrecRequests } from "boxrec-requests" ; import { BoxrecLocationEventParams , BoxrecLocationsPeopleParams , BoxrecRole } from "boxrec-requests/dist/boxrec-requests.constants" ; import * as fs from "fs" ; import { WriteStream } from "fs" ; import { CookieJar , RequestResponse } from "request" ; import { BoxrecPageChampions } from "./boxrec-pages/champions/boxrec.page.champions" ; import { BoxrecPageDate } from "./boxrec-pages/date/boxrec.page.date" ; import { BoxrecPageEventBout } from "./boxrec-pages/event/bout/boxrec.page.event.bout" ; import { BoxrecPageEvent } from "./boxrec-pages/event/boxrec.page.event" ; import { BoxrecPageLocationEvent } from "./boxrec-pages/location/event/boxrec.page.location.event" ; import { BoxrecPageLocationBoxer } from "./boxrec-pages/location/people/boxrec.page.location.boxer" ; import { BoxrecPageLocationPeople } from "./boxrec-pages/location/people/boxrec.page.location.people" ; import { BoxrecPageProfileBoxer } from "./boxrec-pages/profile/boxrec.page.profile.boxer" ; import { BoxrecPageProfileEvents } from "./boxrec-pages/profile/boxrec.page.profile.events" ; import { BoxrecPageProfileManager } from "./boxrec-pages/profile/boxrec.page.profile.manager" ; import { BoxrecPageProfileOtherCommon } from "./boxrec-pages/profile/boxrec.page.profile.other.common" ; import { BoxrecPageProfilePromoter } from "./boxrec-pages/profile/boxrec.page.profile.promoter" ; import { BoxrecPageRatings } from "./boxrec-pages/ratings/boxrec.page.ratings" ; import { BoxrecRatingsParams } from "./boxrec-pages/ratings/boxrec.ratings.constants" ; import { BoxrecResultsParams } from "./boxrec-pages/results/boxrec.results.constants" ; import { BoxrecPageSchedule } from "./boxrec-pages/schedule/boxrec.page.schedule" ; import { BoxrecScheduleParams } from "./boxrec-pages/schedule/boxrec.schedule.constants" ; import { BoxrecPageSearch } from "./boxrec-pages/search/boxrec.page.search" ; import { BoxrecSearch , BoxrecSearchParams , BoxrecStatus } from "./boxrec-pages/search/boxrec.search.constants" ; import { BoxrecPageTitle } from "./boxrec-pages/title/boxrec.page.title" ; import { BoxrecTitlesParams } from "./boxrec-pages/titles/boxrec.page.title.constants" ; import { BoxrecPageTitles } from "./boxrec-pages/titles/boxrec.page.titles" ; import { BoxrecPageVenue } from "./boxrec-pages/venue/boxrec.page.venue" ; import { BoxrecPageWatch } from "./boxrec-pages/watch/boxrec.page.watch" ; import { BoxrecPageWatchRow } from "./boxrec-pages/watch/boxrec.page.watch.row" ; if ( typeof ( Symbol as any ) . asyncIterator === "undefined" ) { ( Symbol as any ) . asyncIterator = Symbol ( "asyncIterator" ) ; } export class Boxrec { param { string } username param { string } password returns { Promise < CookieJar > } static async login ( username , password ) { return BoxrecRequests . login ( username , password ) ; } param { jar } cookieJar param { string } eventBoutId returns { Promise < BoxrecPageEventBout > } static async getBoutById ( cookieJar , eventBoutId ) { const boxrecPageBody = await BoxrecRequests . getBout ( cookieJar , eventBoutId ) ; return new BoxrecPageEventBout ( boxrecPageBody ) ; } param { jar } cookieJar param { number } globalId param { string } pathToSaveTo param { string } fileName returns { Promise < string > } static async getBoxerPDF ( cookieJar , globalId , pathToSaveTo ? , fileName ? ) { return this . getBoxerOther ( cookieJar , globalId , "pdf" , pathToSaveTo , fileName ) ; } param { jar } cookieJar param { number } globalId param { string } pathToSaveTo param { string } fileName returns { Promise < string > } static async getBoxerPrint ( cookieJar , globalId , pathToSaveTo ? , fileName ? ) { return this . getBoxerOther ( cookieJar , globalId , "print" , pathToSaveTo , fileName ) ; } param { jar } cookieJar returns { Promise < BoxrecPageChampions > } static async getChampions ( cookieJar ) { const boxrecPageBody = await BoxrecRequests . getChampions ( cookieJar ) ; return new BoxrecPageChampions ( boxrecPageBody ) ; } param { jar } cookieJar param { string } dateString returns { Promise < void > } static async getDate ( cookieJar , dateString ) { const boxrecPageBody = await BoxrecRequests . getDate ( cookieJar , dateString ) ; return new BoxrecPageDate ( boxrecPageBody ) ; } param { jar } cookieJar param { number } eventId returns { Promise < BoxrecPageEvent > } static async getEventById ( cookieJar , eventId ) { const boxrecPageBody = await BoxrecRequests . getEventById ( cookieJar , eventId ) ; return new BoxrecPageEvent ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecLocationEventParams } params param { BoxrecLocationEventParams . sport } params . sports param { number } offset returns { Promise < BoxrecPageLocationEvent > } static async getEventsByLocation ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getEvents ( cookieJar , params , offset ) ; return new BoxrecPageLocationEvent ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecLocationsPeopleParams } params param { number } offset returns { Promise < BoxrecPageLocationPeople > } static async getPeopleByLocation ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getPeople ( cookieJar , params , offset ) ; if ( params . role === BoxrecRole . proBoxer ) { return new BoxrecPageLocationBoxer ( boxrecPageBody ) ; } return new BoxrecPageLocationPeople ( boxrecPageBody ) ; } param { jar } cookieJar param { number } globalId param { BoxrecRole } role param { number } offset returns { Promise < BoxrecPageProfileBoxer | BoxrecPageProfileOtherCommon | BoxrecPageProfileEvents | BoxrecPageProfileManager > } static async getPersonById ( cookieJar , globalId , role = null , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getPersonById ( cookieJar , globalId , role , offset ) ; switch ( role ) { case BoxrecRole . proBoxer : return new BoxrecPageProfileBoxer ( boxrecPageBody ) ; case BoxrecRole . judge : case BoxrecRole . supervisor : case BoxrecRole . referee : return new BoxrecPageProfileOtherCommon ( boxrecPageBody ) ; case BoxrecRole . promoter : return new BoxrecPageProfilePromoter ( boxrecPageBody ) ; case BoxrecRole . doctor : case BoxrecRole . inspector : case BoxrecRole . matchmaker : return new BoxrecPageProfileEvents ( boxrecPageBody ) ; case BoxrecRole . manager : return new BoxrecPageProfileManager ( boxrecPageBody ) ; } return new BoxrecPageProfileBoxer ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecRatingsParams } params param { number } offset returns { Promise < BoxrecPageRatings > } static async getRatings ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getRatings ( cookieJar , params , offset ) ; return new BoxrecPageRatings ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecResultsParams } params param { number } offset returns { Promise < BoxrecPageSchedule > } static async getResults ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getResults ( cookieJar , params , offset ) ; return new BoxrecPageSchedule ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecScheduleParams } params param { number } offset returns { Promise < BoxrecPageSchedule > } static async getSchedule ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getSchedule ( cookieJar , params , offset ) ; return new BoxrecPageSchedule ( boxrecPageBody ) ; } param { jar } cookieJar param { string } titleString param { number } offset returns { Promise < BoxrecPageTitle > } static async getTitleById ( cookieJar , titleString , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getTitleById ( cookieJar , titleString , offset ) ; return new BoxrecPageTitle ( boxrecPageBody ) ; } param { jar } cookieJar param { BoxrecTitlesParams } params param { number } offset returns { Promise < any > } static async getTitles ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getTitles ( cookieJar , params , offset ) ; return new BoxrecPageTitles ( boxrecPageBody ) ; } param { jar } cookieJar param { number } venueId param { number } offset returns { Promise < BoxrecPageVenue > } static async getVenueById ( cookieJar , venueId , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . getVenueById ( cookieJar , venueId , offset ) ; return new BoxrecPageVenue ( boxrecPageBody ) ; } param { jar } cookieJar returns { Promise < BoxrecPageWatchRow > } static async getWatched ( cookieJar ) { const boxrecPageBody = await BoxrecRequests . getWatched ( cookieJar ) ; return new BoxrecPageWatch ( boxrecPageBody ) . list ; } param { jar } cookieJar param { BoxrecSearchParams } params param { number } offset returns { Promise < BoxrecSearch [ ] > } static async search ( cookieJar , params , offset = 0 ) { const boxrecPageBody = await BoxrecRequests . search ( cookieJar , params , offset ) ; return new BoxrecPageSearch ( boxrecPageBody ) . results ; } param { jar } cookieJar param { number } boxerGlobalId returns { Promise < boolean > } static async unwatch ( cookieJar , boxerGlobalId ) { const boxrecPageBody = await BoxrecRequests . unwatch ( cookieJar , boxerGlobalId ) ; const isBoxerInList = new BoxrecPageWatch ( boxrecPageBody ) . checkForBoxerInList ( boxerGlobalId ) ; if ( isBoxerInList ) { throw new Error ( "Boxer appears in list after being removed" ) ; } return ! isBoxerInList ; } param { jar } cookieJar param { number } boxerGlobalId returns { Promise < boolean > } static async watch ( cookieJar , boxerGlobalId ) { const boxrecPageBody = await BoxrecRequests . watch ( cookieJar , boxerGlobalId ) ; const isBoxerInList = new BoxrecPageWatch ( boxrecPageBody ) . checkForBoxerInList ( boxerGlobalId ) ; if ( ! isBoxerInList ) { throw new Error ( "Boxer did not appear in list after being added" ) ; } return isBoxerInList ; } param { jar } cookieJar param { string } firstName param { string } lastName param { string } role param { BoxrecStatus } status param { number } offset yields static async * getPeopleByName ( cookieJar , firstName , lastName , role = BoxrecRole . proBoxer , status = BoxrecStatus . all , offset = 0 ) { const params = { first_name : firstName , last_name : lastName , role , status , } ; const searchResults = await Boxrec . search ( cookieJar , params , offset ) ; for ( const result of searchResults ) { yield await Boxrec . getPersonById ( cookieJar , result . id ) ; } } param { jar } cookieJar param { number } globalId param { "pdf" | "print" } type param { string } pathToSaveTo param { string } fileName returns { Promise < string > } private static async getBoxerOther ( cookieJar , globalId , type , pathToSaveTo ? , fileName ? ) { let boxrecPageBody ; if ( type === "pdf" ) { boxrecPageBody = await BoxrecRequests . getBoxerPDF ( cookieJar , globalId , pathToSaveTo , fileName ) ; } else { boxrecPageBody = await BoxrecRequests . getBoxerPrint ( cookieJar , globalId , pathToSaveTo , fileName ) ; } if ( pathToSaveTo ) { let fileNameToSaveAs ; if ( fileName ) { fileNameToSaveAs = fileName ; } else { fileNameToSaveAs = `${ globalId }.` + ( type === "pdf" ? "pdf" : "html" ) ; } let updatedPathToSave = pathToSaveTo ; if ( updatedPathToSave . slice ( - 1 ) !== "/" ) { updatedPathToSave += "/" ; } const file = fs . createWriteStream ( updatedPathToSave + fileNameToSaveAs ) ; boxrecPageBody . pipe ( file ) ; } return boxrecPageBody ; } }