import { getRandomUserAgent } from '@stock-bot/utils'; import type { CeoHandler } from '../ceo.handler'; export async function updateCeoChannels( this: CeoHandler, payload: number | undefined ): Promise { const proxy = this.proxy?.getProxy(); if (!proxy) { this.logger.warn('No proxy available for CEO channels update'); return; } let page; if (payload === undefined) { page = 1; } else { page = payload; } this.logger.info(`Fetching CEO channels for page ${page} with proxy ${proxy}`); const response = await this.http.get( 'https://api.ceo.ca/api/home?exchange=all&sort_by=symbol§or=All&tab=companies&page=' + page, { proxy: proxy, headers: { 'User-Agent': getRandomUserAgent(), }, } ); const results = await response.json(); const channels = results.channel_categories[0].channels; const totalChannels = results.channel_categories[0].total_channels; const totalPages = Math.ceil(totalChannels / channels.length); const exchanges: { exchange: string; countryCode: string }[] = []; interface Channel { symbol: string; exchange: string; title: string; type: string; channel: string; spiel_count: number; unread_count: number; is_following: boolean; last_viewed_timestamp: string; last_timestamp: string; company_details?: Record; } const symbols = channels.map((channel: Channel) => { // check if exchange is in the exchanges array object if (!exchanges.find((e) => e.exchange === channel.exchange)) { exchanges.push({ exchange: channel.exchange, countryCode: 'CA', }); } const details = channel.company_details || {}; return { symbol: channel.symbol, exchange: channel.exchange, name: channel.title, type: channel.type, ceoId: channel.channel, marketCap: details.market_cap, volumeRatio: details.volume_ratio, avgVolume: details.avg_volume, stockType: details.stock_type, issueType: details.issue_type, sharesOutstanding: details.shares_outstanding, float: details.float, }; }); await this.mongodb.batchUpsert('ceoSymbols', symbols, ['symbol', 'exchange']); await this.mongodb.batchUpsert('ceoExchanges', exchanges, ['exchange']); if (page === 1) { for (let i = 2; i <= totalPages; i++) { this.logger.info(`Scheduling page ${i} of ${totalPages} for CEO channels`); await this.scheduleOperation('update-ceo-channels', i); } } this.logger.info(`Fetched CEO channels for page ${page}/${totalPages}`); return { page, totalPages }; }