Using Raspberry Pi to automate searching craigslist for an item using node.js




Raspberry Pi

images

Raspberry Pi is a cool little gadget . If you have one lying around , it can be used to automate many tasks .
Raspberry Pi with Raspbian installed will become a powerful low cost device for automation .

 Automating craigslist search

Looking for stuff in craigslist will be time consuming . If you want the item you are looking for in a good price , craigslist will be your best friend . The key us to have it grabbed as soon as its posted by someone . But you never know when it will be posted . So have to keep refreshing the craigslist search .

Using a Raspberry Pi , this search task can be automated

Tools Needed

  • Raspberry Pi with raspbian OS installed
  • A Twilio account  – Don’t worry a free account will do our job here
  • node.js  – I Used Node.js as  I wanted to show in this blog about the ease of installation of Node.js in a PI  . Also the availability of modules and easy installation of those will make this a best fit
  • Internet connection

Install Node.js on Raspberry PI

Installation of node.js is straight forward in a PI with Raspbian .

  1. Switch on the PI and login to raspbian your home will be /home/pi
  2. create a directory nodejs
    1. mkdir nodejs
    2. cd nodejs
  3. Dowload the node.js arm package from node.js website and unzip it
    1. wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-arm-pi.tar.gz
    2. gunzip node-v0.10.28-linux-arm-pi.tar.gz
    3. tar -xvf  node-v0.10.28-linux-arm-pi.tar
  4. Now you have node js ready  (Note : This is easy way . You might use a pre built package also )
  5. Download twilio and node-craigslist modules
    1. cd node-v0.10.28-linux-arm-pi
    2. cd bin
    3. ./npm install twilio
    4. ./npm install node-craigslist

Getting a Twilio API account 

Twilio api can be used in many programming language to send / receive SMS and voice calls  . We are making use of SMS feature of twilio here . A free account will allow sending message to one verified mobile number . Go to twilio and register your phone and note down the SID and Auth Tocken to be used in our program

The Code

// Replace your twilio account SID and TOKEN here
var twilio = require('twilio')('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN'); 
 
// This module will do the craigslist searching 
var craigslist = require('node-craigslist');  
 
// Change your city here 
 client = craigslist({
 city : 'phoenix'  
 }),
 
// Change the price options here 
 options = {
 maxAsk :'12000', 
 minAsk : '1000',
 };

// Give the item to search in title here
client.search(options,'2005 altima', function (err, listings) {
var pids = '' ;
var pidsToText = '';
// play with listings here...
if (listings.length > 0 ) {
found = 1;
listings.forEach(function (listing) {
console.log(listing.pid);
pids = listing.pid + ',' + pids;
pidsToText = pids.substring(0, pids.length-1);
} )

// Give your mobile and twilio mobile number here
twilio.sendSms({
to:'yourMobile',
from:'twilioNumber',
body: pidsToText }
,
// a little output of success/failure
function(error, message) {
if (!error) {
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
}
else {
console.log('Oops! There was an error.');
console.log(error);
}
});

}
});

Save the above code as nodeCraigslist.js in /home/pi/code in the raspberry pi
Now we have to change some code in the node_craigslist for better search

  • Go to /home/pi/nodejs/node-v0.10.28-linux-arm-pi/bin/node_modules/node-craigslist/lib
  • open index.js
  • search for searchPath variable and change as below
    searchPath = ‘/search/sss?postedToday=1&srchType=T&sort=date&query=';
  • This will ensure search for today’s post , search title and sort by date

Running on Raspberry PI 

We have to run this script every 15 min to check for listing of the item of interest . This can be achieved by setting up in crontab

On Raspberry pi

  1. crontab -e
  2. add the below line
    0,15,30,45 * * * * /home/pi/node-v0.10.28-linux-arm-pi/bin/node
    /home/pi/code/nodeCraigslist.js
  3. save

Now your Pi will do the searching for your item and send you message if it finds one . Saving you lot of time  and effort !