Below you may find examples of use of sbcore build-in functionality.
Online detector
This will wait for internet connection and execute the function RunWhenConnected when connected.
function IfConnected( func )
{ var fcall = function ( ) { IfConnected( func ); };
if(WiFi() && MyIP()) return func();
setTimeout(fcall, 1000);
}
function RunWhenConnected()
{ Print( "I am connected!" );
}
IfConnected( RunWhenConnected );
Download JSON file
Download and parse JSON file and run OnJson function. In case of error, OnJson(null) will be called.
Http(http_reply, "https://somewebsite.com/file.json");
function http_reply(res)
{
if(res.data) {
try
{ var j = JSON.parse(res.data);
OnJson( j );
} catch ( e )
{ Print( "Wrong JSON." );
OnJson( null );
}
}
else {
Print( "Error downloading JSON." );
OnJson( null );
}
}
function OnJson( j )
{
}
Actual HTTP request is:
GET /file.json HTTP/1.0
Host: somewebsite.com
User-Agent: Mozilla/5.0 (Windows NT 11.0; Win64; x64) ... Chrome/99.0.4844.84 Safari/537.36
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
POST data
Send POST request to server with data post_data and print server reply.
var post_data = "hello=world&Foo=Bar";
Http(http_reply, "https://somewebsite.com/file.json", post_data);
function http_reply(res)
{
Print( res.data ? res.data : "ERROR: COULD NOT CONNECT" );
}
Actual HTTP request is:
POST /file.json HTTP/1.0
Host: somewebsite.com
User-Agent: Mozilla/5.0 (Windows NT 11.0; Win64; x64) ... Chrome/99.0.4844.84 Safari/537.36
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 19
hello=world&Foo=Bar
OPTIONS request
You may send another verb, rather than GET or POST:
Http(http_reply, "https://yahoo.com/", null, null, "OPTIONS");
function http_reply(res)
{
if(res.data) {
Print(JSON.stringify(r.data));
}
}
Actual HTTP request is:
OPTIONS / HTTP/1.0
Host: yahoo.com
User-Agent: Mozilla/5.0 (Windows NT 11.0; Win64; x64) ... Chrome/99.0.4844.84 Safari/537.36
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Full control on request headers
First line of request substutution
Http(http_reply, "https://yahoo.com/", null, null, "OPTIONS /foo BAR/7.8");
function http_reply(res)
{
if(res.data) {
Print(JSON.stringify(r.data));
}
}
Actual HTTP request is:
OPTIONS /foo BAR/7.8
Host: yahoo.com
User-Agent: Mozilla/5.0 (Windows NT 11.0; Win64; x64) ... Chrome/99.0.4844.84 Safari/537.36
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Changing user agent
Http(http_reply, "https://yahoo.com/", null, null, "User-Agent: sbcore");
function http_reply(res)
{
if(res.data) {
Print(JSON.stringify(r.data));
}
}
Actual HTTP request is:
GET / HTTP/1.0
User-Agent: sbcore
Host: yahoo.com
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Specifying several HTTP headers
Http(http_reply, "https://yahoo.com/", null, null, "User-Agent: sbcore\nHost: example.com");
function http_reply(res)
{
if(res.data) {
Print(JSON.stringify(r.data));
}
}
Actual HTTP request is:
GET / HTTP/1.0
User-Agent: sbcore
Host: example.com
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Using proxy server
Scraper supports SOCKS4, SOCKS5 and HTTPS types of proxy servers.
It's recommended to specify proxy type as a prefix. For example:
SOCKS5:10.11.12.13:8088
It's Ok to skip the proxy type. In this case Scrapper application will try to use find out proxy type automatically. This may take some time, of cause.
Http(http_reply, "https://yahoo.com/", null, "SOCKS5:10.11.12.13:8088");
function http_reply(res)
{
if(res.data) {
Print(JSON.stringify(r.data));
}
}
Check if the proxy is alive
It's recommended to specify proxy type as a prefix. For example:
SOCKS5:10.11.12.13:8088
We use proxyCheckerURL variable instead of specifying the URL. Read more here.
IfConnected ( start_proxy_check );
function start_proxy_check()
{ var proxy_addr = "10.11.12.13:8088";
Http(proxy_reply, proxyCheckerURL, null, proxy_addr);
}
function proxy_reply(res)
{ var addr = res.proxy.protocol + ":" + res.proxy.host + ":" + res.proxy.port;
if(res.data) {
var j, found=false, anonynous=true;
try
{ j = res.data ? JSON.parse(res.data) : false;
if(j && j.address)
for(k in j.address)
{ found = true;
if(j.address[k] == MyIP())
anonynous = false;
}
} catch(e) { }
if(found) {
Print("The proxy " + addr + " is " +
( anonynous? "anonymous" : "transparent"));
}
else {
Print("The proxy " + addr + " error: " +
res.data);
}
} else {
Print("The proxy " + addr + " did not reply");
}
}
It's very easy to develop a small script to find alive proxies by parsing the list. Contact us for assistance.
See also
|