Skip to content

Instantly share code, notes, and snippets.

@staberas
Last active April 7, 2018 08:32
Show Gist options
  • Save staberas/5e655c413bffb42ffa2edcb9aef947c8 to your computer and use it in GitHub Desktop.
Save staberas/5e655c413bffb42ffa2edcb9aef947c8 to your computer and use it in GitHub Desktop.
Shell bot
#!/bin/sh
# This is a comment!
echo starting bot; # This is a comment, too!
echo "initialize gpio";
gpio mode 1 out
gpio mode 0 out
VARIP="$(curl v4.ifconfig.co)"; #save external ip addr OR USE: curl ifconfig.me/ip
echo "Got IP addr";
echo "${VARIP}"; #echo ip
loadpages()
{
phantomjs pages.js
echo "completed function";
}
changeipgpio()
{
echo "wait 25 sec";
sleep 3;
echo "turn off internet";
gpio toggle 0;
gpio toggle 1;
echo "wait 20 min";
sleep 3; #600
echo "in the 10 min"
sleep 3; #540
echo "reset in 1 min..";
sleep 3; #60
gpio toggle 0;
gpio toggle 1;
echo "reset complete";
}
verifyip()
{
sleep 1; #360;
echo "verifying new ip";
VARIPNEW="$(curl v4.ifconfig.co)";
if [ ${VARIPNEW} = ${VARIP} ]
then
echo "not new";
changeipgpio;
verifyip;
else
echo "new ip";
loadpages;
verifyip;
fi
}
loadpages;
verifyip;
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
// how long should we wait for the page to load before we exit
// in ms
var WAIT_TIME = 5000;
// if the page hasn't loaded after this long, something is probably wrong.
// in ms
var MAX_EXECUTION_TIME = 15000;
// output error messages
var DEBUG = true
// a list of regular expressions of resources (urls) to log when we load them
var resources_to_log = [
new RegExp('^http(s)?://(www|ssl)\.google-analytics\.com.*'),
new RegExp('^http(s)?://(www|ssl)\.googletagmanager\.com.*'),
new RegExp('^http(s)?://stats\.g\.doubleclick\.net.*')
];
// the urls to navigate to the first url is ignored
var urls = [
'http://example.com/',
'http://sandpunk.me/',
'http://twitter.com/',
'http://sandpunk.me/'
];
var i = 0;
// the recursion function
var genericCallback = function () {
return function (status) {
console.log("URL: " + urls[i]);
console.log("Status: " + status);
// exit if there was a problem with the navigation
if (!status || status === 'fail') phantom.exit();
i++;
if (status === "success") {
//-- YOUR STUFF HERE ----------------------
// do your stuff here...
// create a function that is called every time a resource is requested
// http://phantomjs.org/api/webpage/handler/on-resource-requested.html
page.onResourceRequested = function (res) {
//console.log(res.url);
// loop round all our regexs to see if this url matches any of them
var length = resources_to_log.length;
while(length--) {
if (resources_to_log[length].test(res.url)){
// we have a match, log it
console.log(res.url);
}
}
};
// if debug is true, log errors, else ignore them
page.onError = function(msg, trace){
if (DEBUG) {
console.log('ERROR: ' + msg)
console.log(trace)
}
};
// make a note of any errors so we can print them out
page.onResourceError = function(resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};
//-----------------------------------------
if (i < urls.length) {
// navigate to the next url and the callback is this function (recursion)
page.open(urls[i], genericCallback()).setTimeout(function () {
phantom.exit();
}, WAIT_TIME);
} else {
// try navigate to the next url (it is undefined because it is the last element) so the callback is exit
page.open(urls[i], function () {
phantom.exit();
});
}
}
};
};
// start from the first url
page.open(urls[i], genericCallback());
var page = require('webpage').create();
// the urls to navigate to
var urls = [
'http://phantomjs.org/',
'https://twitter.com/sidanmor',
'https://github.com/sidanmor'
];
var i = 0;
// the recursion function
var genericCallback = function () {
return function (status) {
console.log("URL: " + urls[i]);
console.log("Status: " + status);
// exit if there was a problem with the navigation
if (!status || status === 'fail') phantom.exit();
i++;
if (status === "success") {
//-- YOUR STUFF HERE ----------------------
// do your stuff here... I'm taking a picture of the page
page.render('example' + i + '.png');
//-----------------------------------------
if (i < urls.length) {
// navigate to the next url and the callback is this function (recursion)
page.open(urls[i], genericCallback());
} else {
// try navigate to the next url (it is undefined because it is the last element) so the callback is exit
page.open(urls[i], function () {
phantom.exit();
});
}
}
};
};
// start from the first url
page.open(urls[i], genericCallback());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment