pinnacleClient.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import dotenv from 'dotenv';
  2. import axios from "axios";
  3. import { HttpsProxyAgent } from "https-proxy-agent";
  4. import { randomUUID } from 'crypto';
  5. import Logs from "./logs.js";
  6. import getDateInTimezone from "./getDateInTimezone.js";
  7. dotenv.config();
  8. const axiosDefaultOptions = {
  9. baseURL: "",
  10. url: "",
  11. method: "GET",
  12. headers: {},
  13. params: {},
  14. data: {},
  15. timeout: 10000,
  16. };
  17. const pinnacleWebOptions = {
  18. ...axiosDefaultOptions,
  19. baseURL: "https://www.part987.com",
  20. headers: {
  21. "accept": "application/json, text/plain, */*",
  22. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36",
  23. "x-app-data": "directusToken=TwEdnphtyxsfMpXoJkCkWaPsL2KJJ3lo;lang=zh_CN;dpVXz=ZDfaFZUP9"
  24. },
  25. }
  26. export const pinnacleRequest = async (options) => {
  27. const { url, ...optionsRest } = options;
  28. const username = process.env.PINNACLE_USERNAME;
  29. const password = process.env.PINNACLE_PASSWORD;
  30. if (!url || !username || !password) {
  31. throw new Error("url、username、password is required");
  32. }
  33. const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
  34. const axiosConfig = { ...axiosDefaultOptions, ...optionsRest, url, baseURL: "https://api.pinnacle888.com" };
  35. Object.assign(axiosConfig.headers, {
  36. "Authorization": authHeader,
  37. "Accept": "application/json",
  38. });
  39. const proxy = process.env.NODE_HTTP_PROXY;
  40. if (proxy) {
  41. axiosConfig.proxy = false;
  42. axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
  43. }
  44. return axios(axiosConfig).then(res => {
  45. // Logs.out('pinnacle request', url, axiosConfig, res.data);
  46. return res.data;
  47. });
  48. }
  49. /**
  50. * Pinnacle API Get请求
  51. * @param {*} url
  52. * @param {*} params
  53. * @returns
  54. */
  55. export const pinnacleGet = async (url, params) => {
  56. return pinnacleRequest({
  57. url,
  58. params
  59. })
  60. .catch(err => {
  61. const source = { url, params };
  62. if (err?.response?.data) {
  63. const data = err.response.data;
  64. Object.assign(source, { data });
  65. }
  66. err.source = source;
  67. return Promise.reject(err);
  68. });
  69. }
  70. /**
  71. * Pinnacle API Post请求
  72. * @param {*} url
  73. * @param {*} data
  74. * @returns
  75. */
  76. export const pinnaclePost = async (url, data) => {
  77. return pinnacleRequest({
  78. url,
  79. method: 'POST',
  80. headers: {
  81. 'Content-Type': 'application/json',
  82. },
  83. data
  84. });
  85. }
  86. /**
  87. * 清理对象中的undefined值
  88. * @param {*} obj
  89. * @returns {Object}
  90. */
  91. const cleanUndefined = (obj) => {
  92. return Object.fromEntries(
  93. Object.entries(obj).filter(([, v]) => v !== undefined)
  94. );
  95. }
  96. /**
  97. * 获取直盘线
  98. */
  99. export const getLineInfo = async (info = {}) => {
  100. const {
  101. leagueId, eventId, betType, handicap, team, side,
  102. specialId, contestantId,
  103. periodNumber=0, oddsFormat='Decimal', sportId=29
  104. } = info;
  105. let url = '/v2/line/';
  106. let data = { sportId, leagueId, eventId, betType, handicap, periodNumber, team, side, oddsFormat };
  107. if (specialId) {
  108. url = '/v2/line/special';
  109. data = { specialId, contestantId, oddsFormat };
  110. }
  111. data = cleanUndefined(data);
  112. return pinnacleGet(url, data)
  113. .then(ret => ({ info: ret, line: data }))
  114. .catch(err => {
  115. Logs.errDev('get line info error', err);
  116. return Promise.reject(err);
  117. });
  118. }
  119. /**
  120. * 获取账户余额
  121. */
  122. export const getAccountBalance = async () => {
  123. return pinnacleGet('/v1/client/balance');
  124. }
  125. /**
  126. * 下注
  127. */
  128. export const placeOrder = async ({ info, line, stakeSize }) => {
  129. // return Promise.resolve({info, line, stakeSize});
  130. const uuid = randomUUID()
  131. if (line.specialId) {
  132. const data = cleanUndefined({
  133. oddsFormat: line.oddsFormat,
  134. uniqueRequestId: uuid,
  135. acceptBetterLine: true,
  136. stake: stakeSize,
  137. winRiskStake: 'RISK',
  138. lineId: info.lineId,
  139. specialId: info.specialId,
  140. contestantId: info.contestantId,
  141. });
  142. Logs.outDev('pinnacle place order data', data);
  143. return pinnaclePost('/v4/bets/special', { bets: [data] })
  144. .then(ret => ret.bets?.[0] ?? ret)
  145. .then(ret => {
  146. Logs.outDev('pinnacle place order', ret, uuid);
  147. return ret;
  148. })
  149. .catch(err => {
  150. Logs.outDev('pinnacle place order error', err.response.data, uuid);
  151. Logs.errDev(err);
  152. return Promise.reject(err);
  153. });
  154. }
  155. else {
  156. const data = cleanUndefined({
  157. oddsFormat: line.oddsFormat,
  158. uniqueRequestId: uuid,
  159. acceptBetterLine: true,
  160. stake: stakeSize,
  161. winRiskStake: 'RISK',
  162. lineId: info.lineId,
  163. altLineId: info.altLineId,
  164. fillType: 'NORMAL',
  165. sportId: line.sportId,
  166. eventId: line.eventId,
  167. periodNumber: line.periodNumber,
  168. betType: line.betType,
  169. team: line.team,
  170. side: line.side,
  171. handicap: line.handicap,
  172. });
  173. Logs.outDev('pinnacle place order data', data);
  174. return pinnaclePost('/v4/bets/place', data)
  175. .then(ret => {
  176. Logs.outDev('pinnacle place order', ret, uuid);
  177. return ret;
  178. })
  179. .catch(err => {
  180. Logs.outDev('pinnacle place order error', err.response.data, uuid);
  181. Logs.errDev(err);
  182. return Promise.reject(err);
  183. });
  184. }
  185. }
  186. /**
  187. * 获取Web端联赛数据
  188. * @param {*} marketType 0: 早盘赛事, 1: 今日赛事
  189. * @param {*} locale en_US or zh_CN
  190. * @returns
  191. */
  192. export const pinnacleWebLeagues = async (marketType = 1, locale = "zh_CN") => {
  193. const dateString = marketType == 1 ? getDateInTimezone(-4) : getDateInTimezone(-4, Date.now()+24*60*60*1000);
  194. const nowTime = Date.now();
  195. const axiosConfig = {
  196. ...pinnacleWebOptions,
  197. url: "/sports-service/sv/compact/leagues",
  198. params: {
  199. btg: 1, c: "", d: dateString,
  200. l: true, mk: marketType,
  201. pa: 0, pn: -1, sp: 29, tm: 0,
  202. locale, _: nowTime, withCredentials: true
  203. },
  204. };
  205. const proxy = process.env.NODE_HTTP_PROXY;
  206. if (proxy) {
  207. axiosConfig.proxy = false;
  208. axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
  209. }
  210. return axios(axiosConfig).then(res => res.data?.[0]?.[2]?.map(item => {
  211. const [ id, , name ] = item;
  212. return [id, { id, name }];
  213. }) ?? []);
  214. }
  215. /**
  216. * 获取Web比赛列表
  217. */
  218. export const pinnacleWebGames = async (leagues=[], marketType=1, locale="zh_CN") => {
  219. const dateString = marketType == 1 ? getDateInTimezone(-4) : getDateInTimezone(-4, Date.now()+24*60*60*1000);
  220. const nowTime = Date.now();
  221. const axiosConfig = {
  222. ...pinnacleWebOptions,
  223. url: "/sports-service/sv/odds/events",
  224. params: {
  225. sp: 29, lg: leagues.join(','), ev: "",
  226. mk: marketType, btg: 1, ot: 1,
  227. d: dateString, o: 0, l: 100, v: 0,
  228. me: 0, more: false, tm: 0, pa: 0, c: "",
  229. g: "QQ==", cl: 100, pimo: "0,1,8,39,2,3,6,7,4,5",
  230. inl: false, _: nowTime, locale
  231. },
  232. };
  233. const proxy = process.env.NODE_HTTP_PROXY;
  234. if (proxy) {
  235. axiosConfig.proxy = false;
  236. axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
  237. }
  238. return axios(axiosConfig).then(res => res.data.n?.[0]?.[2]?.map(league => {
  239. const [leagueId, leagueName, games] = league;
  240. return games.map(game => {
  241. const [id, teamHomeName, teamAwayName, , timestamp] = game;
  242. const startTime = getDateInTimezone('+8', timestamp, true);
  243. return { id, leagueId, leagueName, teamHomeName, teamAwayName, timestamp, startTime }
  244. })
  245. }).flat().filter(game => {
  246. const { teamHomeName, teamAwayName } = game;
  247. if (teamHomeName.startsWith('主队')) {
  248. return false;
  249. }
  250. else if (teamAwayName.startsWith('客队')) {
  251. return false;
  252. }
  253. else if (teamHomeName.startsWith('Home Teams')) {
  254. return false;
  255. }
  256. else if (teamAwayName.startsWith('Away Teams')) {
  257. return false;
  258. }
  259. return true;
  260. }) ?? []);
  261. }
  262. export const platformRequest = async (options) => {
  263. const { url } = options;
  264. if (!url) {
  265. throw new Error("url is required");
  266. }
  267. const mergedOptions = {
  268. ...axiosDefaultOptions,
  269. ...options,
  270. baseURL: "http://127.0.0.1:9020",
  271. };
  272. return axios(mergedOptions).then(res => res.data);
  273. }
  274. export const platformPost = async (url, data) => {
  275. return platformRequest({
  276. url,
  277. method: 'POST',
  278. headers: {
  279. 'Content-Type': 'application/json',
  280. },
  281. data,
  282. });
  283. }
  284. export const platformGet = async (url, params) => {
  285. return platformRequest({ url, method: 'GET', params });
  286. }
  287. /**
  288. * 通知异常
  289. * @param {*} message
  290. */
  291. export const notifyException = async (message) => {
  292. Logs.out('notify exception', message);
  293. }