-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-api-client.js
More file actions
executable file
·114 lines (96 loc) · 3.34 KB
/
Copy pathws-api-client.js
File metadata and controls
executable file
·114 lines (96 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require('fs')
const readline = require('readline')
const axios = require('axios')
const WebSocket = require('ws')
const AUTH_ENDPOINT = 'https://www.theblock.pro/api-public/v1/users/auth'
//const ENDPOINT = 'wss://www.theblock.pro/api-public/v1/news/live?tokens=btc,eth,aave,ada,algo,bnb,bch,eos,luna' //token filter
//const ENDPOINT = 'wss://www.theblock.pro/api-public/v1/news/live?priority=1,2,3' //priority filter
//const ENDPOINT = 'wss://www.theblock.pro/api-public/v1/news/live'
const ENDPOINT = 'wss://priority-api.theblock.pro/api-public/v1/news/live?channel=news_published,news_stream,echo'
const LAST_PING_TIMEOUT = 60000
const read = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let credentials = { email: '', apiKey: '' }
// Function to authenticate and get token
async function authenticateAndGetToken(email, apiKey) {
try {
const response = await axios.post(AUTH_ENDPOINT, { email, apiKey })
if (response.status === 200 && response.data.data.token) {
return response.data.data.token
}
throw new Error('Authentication failed')
} catch (error) {
console.error('Error in authentication:', error.message)
process.exit(1)
}
}
// Function to read the API key from file or get it from the user
async function readApiKey() {
try {
const key = fs.readFileSync('./ws-api-key.txt', 'utf-8').trim()
if (!key) throw new Error('Empty key file')
return key
} catch (error) {
return new Promise((resolve) => {
read.question('Enter your email: ', email => {
read.question('Enter your API key: ', apiKey => {
credentials = { email, apiKey }
authenticateAndGetToken(email, apiKey)
.then(token => {
fs.writeFileSync('./ws-api-key.txt', token)
resolve(token)
})
})
})
})
}
}
function time() {
return `\x1b[32m${new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')}\x1b[0m`
}
async function connect() {
let lastPing = Date.now()
const key = await readApiKey()
const ws = new WebSocket(ENDPOINT, {
headers: {
'x-auth-token': key
}
})
ws.on('open', function open() {
console.log('Connected to', ENDPOINT)
setInterval(() => {
if (ws.readyState !== WebSocket.OPEN || Date.now() - lastPing > LAST_PING_TIMEOUT) {
console.log('Connection lost, reconnecting...')
ws.terminate()
connect()
}
}, 30000)
})
ws.on('ping', function incoming() {
const pingTime = Date.now() - lastPing
lastPing = Date.now()
console.log(`[${time()}] Ping:`, pingTime)
})
ws.on('message', function incoming(data) {
console.log(`[${time()}] PAYLOAD:`, data.toString())
})
ws.on('error', function error(error) {
console.log('ERROR:', arguments)
})
ws.on('close', async function close(code, reason) {
console.log('the code', code, 'the reason', reason.toString())
if (credentials.email && credentials.apiKey) {
console.log('Re-authenticating due to 401 Unauthorized error...')
const newToken = await authenticateAndGetToken(credentials.email, credentials.apiKey)
fs.writeFileSync('./ws-api-key.txt', newToken)
connect()
} else {
fs.writeFileSync('./ws-api-key.txt', '')
console.log(`Disconnected (code: ${code}, reason: ${reason})`)
connect()
}
})
}
connect()