-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-exchanges.js
284 lines (238 loc) · 10.9 KB
/
export-exchanges.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"use strict";
const fs = require ('fs')
const countries = require ('./countries')
const asTable = require ('as-table')
const execSync = require ('child_process').execSync
const log = require ('ololog').unlimited
const ansi = require ('ansicolor').nice
const { keys, values } = Object
// ---------------------------------------------------------------------------
let exchanges
let verbose = false
// ---------------------------------------------------------------------------
let wikiPath = 'wiki'
let gitWikiPath = 'ccxt.wiki'
let ccxtCertifiedBadge = '[![CCXT Certified](https://img.shields.io/badge/CCXT-certified-green.svg)](https://github.com/ccxt/ccxt/wiki/Certification)'
let spacing = ' '.repeat (7)
let logoHeading = spacing + 'logo' + spacing
let tableHeadings = [ logoHeading, 'id', 'name', 'ver', 'doc', 'certified', ]
let exchangesByCountryHeading = [ 'country / region', ... tableHeadings ]
if (!fs.existsSync (gitWikiPath)) {
log.bright.cyan ('Checking out ccxt.wiki...')
execSync ('git clone https://github.com/ccxt/ccxt.wiki.git')
}
// ---------------------------------------------------------------------------
function replaceInFile (filename, regex, replacement) {
log.bright.cyan ('Exporting exchanges →', filename.yellow)
let contents = fs.readFileSync (filename, 'utf8')
const newContents = contents.replace (regex, replacement)
fs.truncateSync (filename)
fs.writeFileSync (filename, newContents)
}
// ---------------------------------------------------------------------------
const includedIds = fs.readFileSync ('exchanges.cfg')
.toString () // Buffer → String
.split ('\n') // String → Array
.map (line => line.split ('#')[0].trim ()) // trim comments
.filter (exchange => exchange); // filter empty lines
const isIncluded = (id) => ((includedIds.length === 0) || includedIds.includes (id))
try {
exchanges = require ('./config').ids.filter (isIncluded)
} catch (e) {
log.bright.cyan ('Exporting exchanges...'.yellow)
const ids = fs.readdirSync ('./js/')
.filter (file => file.includes ('.js'))
.map (file => file.slice (0, -3))
.filter (isIncluded);
const pad = function (string, n) {
return (string + ' '.repeat (n)).slice (0, n)
};
[
{
file: './ccxt.js',
regex: /(?:const|var)\s+exchanges\s+\=\s+\{[^\}]+\}/,
replacement: "const exchanges = {\n" + ids.map (id => pad (" '" + id + "':", 30) + " require ('./js/" + id + ".js'),").join ("\n") + " \n}",
},
{
file: './python/ccxt/__init__.py',
regex: /exchanges \= \[[^\]]+\]/,
replacement: "exchanges = [\n" + " '" + ids.join ("',\n '") + "'," + "\n]",
},
{
file: './python/ccxt/__init__.py',
regex: /(?:from ccxt\.[^\.]+ import [^\s]+\s+\# noqa\: F401[\r]?[\n])+[\r]?[\n]exchanges/,
replacement: ids.map (id => pad ('from ccxt.' + id + ' import ' + id, 60) + '# noqa: F401').join ("\n") + "\n\nexchanges",
},
{
file: './python/ccxt/async_support/__init__.py',
regex: /(?:from ccxt\.async_support\.[^\.]+ import [^\s]+\s+\# noqa\: F401[\r]?[\n])+[\r]?[\n]exchanges/,
replacement: ids.map (id => pad ('from ccxt.async_support.' + id + ' import ' + id, 74) + '# noqa: F401').join ("\n") + "\n\nexchanges",
},
{
file: './python/ccxt/async_support/__init__.py',
regex: /exchanges \= \[[^\]]+\]/,
replacement: "exchanges = [\n" + " '" + ids.join ("',\n '") + "'," + "\n]",
},
{
file: './php/Exchange.php',
regex: /public static \$exchanges \= array \([^\)]+\)/,
replacement: "public static $exchanges = array (\n '" + ids.join ("',\n '") + "',\n )",
},
].forEach (({ file, regex, replacement }) => {
replaceInFile (file, regex, replacement)
})
exchanges = {}
ids.forEach (id => {
exchanges[id] = { 'verbose': verbose, 'apiKey': '', 'secret': '' }
})
log.bright.green ('Base sources updated successfully.')
}
// ----------------------------------------------------------------------------
// strategically placed exactly here
const ccxt = require ('./ccxt.js')
// ----------------------------------------------------------------------------
for (let id in exchanges) {
ccxt[id].prototype.checkRequiredDependencies = () => {}
exchanges[id] = new (ccxt)[id] (exchanges[id])
exchanges[id].verbose = verbose
}
var countryName = function (code) {
return ((countries[code] !== undefined) ? countries[code] : code)
}
// ---------------------------------------------------------------------------
// list all supported exchanges
let tableData = values (exchanges).map (exchange => {
let logo = exchange.urls['logo']
let website = Array.isArray (exchange.urls.www) ? exchange.urls.www[0] : exchange.urls.www
let url = exchange.urls.referral || website
let countries = Array.isArray (exchange.countries) ? exchange.countries.map (countryName).join (', ') : countryName (exchange.countries)
let doc = Array.isArray (exchange.urls.doc) ? exchange.urls.doc[0] : exchange.urls.doc
let version = exchange.version ? exchange.version : '\*'
let matches = version.match (/[^0-9]*([0-9].*)/)
if (matches)
version = matches[1];
return [
'[![' + exchange.id + '](' + logo + ')](' + url + ')',
exchange.id,
'[' + exchange.name + '](' + url + ')',
version,
'[API](' + doc + ')',
exchange.certified ? ccxtCertifiedBadge : '',
countries,
]
})
tableData.splice (0, 0, tableHeadings)
function makeTable (jsonArray) {
let table = asTable.configure ({ 'delimiter': ' | ' }) (jsonArray)
let lines = table.split ("\n")
lines.splice (1,0, lines[0].replace (/[^\|]/g, '-'))
let headerLine = lines[1].split ('|')
headerLine[3] = ':' + headerLine[3].slice (1, headerLine[3].length - 1) + ':'
headerLine[4] = ':' + headerLine[4].slice (1, headerLine[4].length - 1) + ':'
lines[1] = headerLine.join ('|')
return lines.map (line => '|' + line + '|').join ("\n")
}
let exchangesTable = makeTable (tableData)
let numExchanges = keys (exchanges).length
let beginning = "The ccxt library currently supports the following "
let ending = " cryptocurrency exchange markets and trading APIs:\n\n"
let totalString = beginning + numExchanges + ending
let howMany = totalString + exchangesTable + "$1"
let allExchangesRegex = new RegExp ("[^\n]+[\n]{2}\\|[^#]+\\|([\n][\n]|[\n]$|$)", 'm')
replaceInFile ('README.md', allExchangesRegex, howMany)
replaceInFile (wikiPath + '/Manual.md', allExchangesRegex, howMany)
replaceInFile (wikiPath + '/Exchange-Markets.md', allExchangesRegex, howMany)
let certifiedFieldIndex = tableHeadings.indexOf ('certified')
let certified = tableData.filter ((x) => x[certifiedFieldIndex] !== '' )
let allCertifiedRegex = new RegExp ("^(## Certified Cryptocurrency Exchanges\n{3})(?:\\|.+\\|$\n)+", 'm')
let certifiedTable = makeTable (certified)
let certifiedTableReplacement = '$1' + certifiedTable + "\n"
replaceInFile ('README.md', allCertifiedRegex, certifiedTableReplacement)
let exchangesByCountries = []
keys (countries).forEach (code => {
let country = countries[code]
let result = []
keys (exchanges).forEach (id => {
let exchange = exchanges[id]
let logo = exchange.urls['logo']
let website = Array.isArray (exchange.urls.www) ? exchange.urls.www[0] : exchange.urls.www
let url = exchange.urls.referral || website
let doc = Array.isArray (exchange.urls.doc) ? exchange.urls.doc[0] : exchange.urls.doc
let version = exchange.version ? exchange.version : '\*'
let matches = version.match (/[^0-9]*([0-9].*)/)
if (matches)
version = matches[1];
let shouldInclude = false
if (Array.isArray (exchange.countries)) {
if (exchange.countries.indexOf (code) > -1)
shouldInclude = true
} else {
if (code == exchange.countries)
shouldInclude = true
}
if (shouldInclude) {
let entry = [
country,
'[![' + exchange.id + '](' + logo + ')](' + url + ')',
exchange.id,
'[' + exchange.name + '](' + url + ')',
version,
'[API](' + doc + ')',
// doesn't fit in width
// exchange.certified ? ccxtCertifiedBadge : '',
]
result.push (entry)
}
})
exchangesByCountries = exchangesByCountries.concat (result)
});
let countryKeyIndex = exchangesByCountryHeading.indexOf ('country / region')
exchangesByCountries = exchangesByCountries.sort ((a, b) => {
let countryA = a[countryKeyIndex].toLowerCase ()
let countryB = b[countryKeyIndex].toLowerCase ()
if (countryA > countryB) {
return 1
} else if (countryA < countryB) {
return -1;
} else {
if (a['id'] > b['id'])
return 1;
else if (a['id'] < b['id'])
return -1;
else
return 0;
}
})
exchangesByCountries.splice (0, 0, exchangesByCountryHeading)
let lines = makeTable (exchangesByCountries)
let result = "# Exchanges By Country\n\nThe ccxt library currently supports the following cryptocurrency exchange markets and trading APIs:\n\n" + lines + "\n\n"
let filename = wikiPath + '/Exchange-Markets-By-Country.md'
fs.truncateSync (filename)
fs.writeFileSync (filename, result)
log.bright ('Exporting exchange ids to'.cyan, 'exchanges.json'.yellow)
fs.writeFileSync ('exchanges.json', JSON.stringify ({ ids: keys (exchanges) }, null, 4))
// ----------------------------------------------------------------------------
const ccxtWikiFileMapping = {
'README.md': 'Home.md',
'Install.md': 'Install.md',
'Manual.md': 'Manual.md',
'Exchange-Markets.md': 'Exchange-Markets.md',
'Exchange-Markets-By-Country.md': 'Exchange-Markets-By-Country.md',
}
keys (ccxtWikiFileMapping)
.forEach (file =>
fs.writeFileSync (gitWikiPath + '/' + ccxtWikiFileMapping[file], fs.readFileSync (wikiPath + '/' + file)))
// ----------------------------------------------------------------------------
log.bright ('Exporting exchange keywords to'.cyan, 'package.json'.yellow)
const packageJSON = require ('./package.json')
const keywords = new Set (packageJSON.keywords)
for (const ex of values (exchanges)) {
for (const url of Array.isArray (ex.urls.www) ? ex.urls.www : [ex.urls.www]) {
keywords.add (url.replace (/(http|https):\/\/(www\.)?/, '').replace (/\/.*/, ''))
}
keywords.add (ex.name)
}
packageJSON.keywords = [...keywords]
fs.writeFileSync ('./package.json', JSON.stringify (packageJSON, null, 2))
// ----------------------------------------------------------------------------
log.bright.green ('Exchanges exported successfully.')