Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Request = require("request");
- const moment = require('moment');
- const schedule = require('node-schedule');
- const http = require('http');
- const https = require('https');
- const url_parse = require('url-parse');
- const winston = require('winston');
- const SRGameProcessor = require('./SRGameProcessor');
- const logger = winston.createLogger({
- transports: [
- new winston.transports.Console()
- ]
- });
- let gameDate = moment().format('YYYY/MM/DD');
- let baseURL = "https://api.sportradar.us/mlb/trial";
- let subscribeUrl = "/stream/en/events/subscribe?api_key=pau4tyxnw5665qx2phyurjz6&match=sd:match:{game_id}";
- let scheduleUrl = "/{version}/en/games/{game_date}/schedule.json?api_key=pau4tyxnw5665qx2phyurjz6";
- scheduleUrl = baseURL + scheduleUrl.replace("{version}", "v6.5").replace("{game_date}", gameDate);
- logger.info("Schedule URL: " + scheduleUrl);
- Request.get(scheduleUrl, (error, response, body) => {
- if(error) {
- return logger.error(error);
- }
- logger.info(body.toString());
- let scheduleJSON = JSON.parse(body);
- scheduleJSON.games.map( g => {
- let start_url = baseURL + subscribeUrl.replace("{game_id}", g.id);
- if ( g.status === 'inprogress') {
- logger.info("Subscribe to Push Events for game: " + g.home.name + " vs " + g.away.name);
- let gp = new SRGameProcessor();
- Promise.resolve(gp.startListening(start_url)).then(
- logger.info("Finished")
- ).catch(console.log.bind(console));
- } else {
- var j = schedule.scheduleJob(g.scheduled, function(){
- logger.info("Subscribe to Push Events for game: " + g.home.name + " vs " + g.away.name);
- let gp = new SRGameProcessor();
- Promise.resolve(gp.startListening(start_url)).then(
- logger.info("Finished")
- ).catch(console.log.bind(console));
- });
- }
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement