index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const axios = require('axios');
  2. const apiUrl = 'https://webapi.sporttery.cn/gateway/uniform/football/getMatchCalculatorV1.qry?poolCode=hhad,had&channel=c';
  3. const ratioAccept = (ratio) => {
  4. if (ratio > 0) {
  5. return 'a'
  6. }
  7. return ''
  8. }
  9. const ratioString = (ratio) => {
  10. ratio = Math.abs(ratio);
  11. ratio = ratio.toString();
  12. ratio = ratio.replace(/\./, '');
  13. return ratio;
  14. }
  15. const parseEvents = (oddsInfo) => {
  16. const { h, d, a, goalLineValue, poolId } = oddsInfo;
  17. const gv = +goalLineValue;
  18. const events = {};
  19. const special = {};
  20. const ratio_rh = gv - 0.5;
  21. const ratio_rc = -(gv + 0.5);
  22. if (!gv) {
  23. events['ior_mn'] = +d;
  24. }
  25. else {
  26. special[`ior_wm${gv > 0 ? 'c' : 'h'}_${Math.abs(gv)}`] = +d;
  27. }
  28. events[`ior_r${ratioAccept(ratio_rh)}h_${ratioString(ratio_rh)}`] = +h;
  29. events[`ior_r${ratioAccept(ratio_rc)}c_${ratioString(ratio_rc)}`] = +a;
  30. return { events, special };
  31. }
  32. const getGamesEvents = async () => {
  33. return axios.get(apiUrl, {
  34. headers: {
  35. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
  36. },
  37. proxy: false,
  38. })
  39. .then(res => res.data)
  40. .then(ret => {
  41. const { success, value } = ret;
  42. if (!success || !value) {
  43. return;
  44. }
  45. const { matchInfoList } = value;
  46. const matchList = matchInfoList?.map(item => {
  47. const { subMatchList } = item;
  48. return subMatchList.map(match => {
  49. const { leagueAllName, leagueId,
  50. homeTeamAllName, awayTeamAllName,
  51. matchDate, matchTime,
  52. oddsList, matchId } = match;
  53. const timestamp = new Date(`${matchDate} ${matchTime}`).getTime();
  54. const oddsValues = { events: {}, special: {} };
  55. oddsList.map(parseEvents).forEach(odds => {
  56. oddsValues.events = { ...oddsValues.events, ...odds.events };
  57. oddsValues.special = { ...oddsValues.special, ...odds.special };
  58. });
  59. const evtime = Date.now();
  60. const sptime = Date.now();
  61. return {
  62. leagueId,
  63. eventId: matchId,
  64. leagueName: leagueAllName,
  65. teamHomeName: homeTeamAllName,
  66. teamAwayName: awayTeamAllName,
  67. timestamp,
  68. ...oddsValues,
  69. evtime,
  70. sptime,
  71. }
  72. });
  73. });
  74. return matchList?.flat() ?? [];
  75. })
  76. }
  77. const updateGamesList = ({ platform, games }) => {
  78. axios.post('http://127.0.0.1:9055/api/triangle/update_games_list', { platform, games })
  79. .then(res => res.data)
  80. .then(ret => {
  81. if (ret.code) {
  82. throw new Error(ret.message);
  83. }
  84. })
  85. .catch(error => {
  86. console.log('%cupdate game list failed, %s', 'color:#c00', error.message, platform);
  87. });
  88. }
  89. setInterval(() => {
  90. getGamesEvents()
  91. .then(gamesEvents => {
  92. updateGamesList({ platform: 'jc', games: gamesEvents });
  93. // console.log(JSON.stringify(gamesEvents, null, 2));
  94. // console.log(new Date());
  95. })
  96. .catch(err => {
  97. console.error(err);
  98. });
  99. }, 5000);