Skip to content

Commit

Permalink
[#142] Adding support to define a proxy server to overcome the Binanc…
Browse files Browse the repository at this point in the history
…e's IP restrictions.
  • Loading branch information
diegomanuel committed Dec 10, 2022
1 parent 15c1f49 commit 1378cd7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A lightweight **Google Spreadsheets Add-On** to GET data _directly_ from **Binance API** _without_ any intermediaries!

> Since Binance decided to **block API requests** coming from restricted countries like USA (from which the Google servers in where our spreadsheets run are located), you'll probably need a proxy.
Check [Binance to Google Sheets Add-On Proxy](https://github.com/diegomanuel/binance-to-google-sheets-proxy) for a basic proxy server implementation that I did for myself and is currently working fine. It may be useful for you too!
See the [README.md](https://github.com/diegomanuel/binance-to-google-sheets-proxy#readme) for further details.

This `add-on` is basically an **API client** specially hand-crafted to work between Google Spreadsheets and Binance.
By using the `BINANCE()` formula in your spreadsheet, you can get data fetched from Binance API like:
* Current market [prices](#operation-prices-public)
Expand Down
8 changes: 7 additions & 1 deletion appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"https://api2.binance.com/",
"https://api3.binance.com/",
"https://fapi.binance.com/",
"https://dapi.binance.com/"
"https://dapi.binance.com/",
"https://btgs-proxy.setupme.io:4000/",
"https://btgs-proxy.setupme.io:4001/",
"https://btgs-proxy.setupme.io:4002/",
"https://btgs-proxy.setupme.io:4003/",
"https://btgs-proxy.setupme.io:4004/",
"https://btgs-proxy.setupme.io:4005/"
]
}
1 change: 1 addition & 0 deletions main.gs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Binance to Google Sheets!
* Diego Manuel - [email protected]
* https://github.com/diegomanuel/binance-to-google-sheets
* https://github.com/diegomanuel/binance-to-google-sheets-proxy
*/


Expand Down
5 changes: 4 additions & 1 deletion misc/config.gs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*/

let DEBUG = false;
const VERSION = "v0.5.2";
const VERSION = "v0.5.3";
const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
const USE_PROXY = false;
// See: https://github.com/diegomanuel/binance-to-google-sheets-proxy
//const USE_PROXY = "https://btgs-proxy.setupme.io"
const SPOT_API_URL = "https://api.binance.com";
const FUTURES_API_URL = "https://fapi.binance.com";
const DELIVERY_API_URL = "https://dapi.binance.com";
Expand Down
31 changes: 22 additions & 9 deletions misc/request.gs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function BinRequest(OPTIONS) {
}
if (response.getResponseCode() == 200) {
BinDoLastUpdate().run(new Date()); // Refresh last update ts
const data = JSON.parse(response.getContentText());
const data = JSON.parse(response.getContentText() || "{}");
if (!opts["no_cache_ok"]) { // Keep last OK response
_setLastCacheResponseOK(CACHE_OK_KEY, da_payload, data);
}
Expand Down Expand Up @@ -142,24 +142,37 @@ function BinRequest(OPTIONS) {
}

function _makeApiUrl(opts) {
if (USE_PROXY) {
return _makeProxyApiUrl(opts)
}
if (opts["futures"]) {
return FUTURES_API_URL;
}
if (opts["delivery"]) {
return DELIVERY_API_URL;
}
return _makeSpotApiUrl();
}

/**
* Builds an URL for the Spot API, using one of the 4 available clusters at random.
* Thank you @fabiob for the PR! :: https://github.com/fabiob
* @see {@link https://binance-docs.github.io/apidocs/spot/en/#general-api-information}
*/
function _makeSpotApiUrl() {
/**
* Builds an URL for the Spot API, using one of the 4 available clusters at random.
* Thank you @fabiob for the PR! :: https://github.com/fabiob
* @see {@link https://binance-docs.github.io/apidocs/spot/en/#general-api-information}
*/
return SPOT_API_URL.replace(/api/, `api${Math.floor(Math.random() * 4) || ''}`);
}

// The ports below should match the ones defined at:
// https://github.com/diegomanuel/binance-to-google-sheets-proxy/blob/main/config/config.exs
function _makeProxyApiUrl(opts) {
if (opts["futures"]) {
return USE_PROXY+":4004";
}
if (opts["delivery"]) {
return USE_PROXY+":4005";
}
// Ports 4000 to 4003
return `${USE_PROXY}:400${Math.floor(Math.random() * 4) || '0'}`;
}

/**
* Retries an execution for given status code.
*/
Expand Down

0 comments on commit 1378cd7

Please sign in to comment.