Odesk notifications about new job(Ubuntu 12.04)

June 10, 2013 | No Comments | Programming | cron freelance linux notify-send odesc ubuntu

I guess It’s too hard monitoring new job on Odesk because you need press F5 every time in your web browser. This isn’t programmer way.We can simplify that.

First of all, we need a libxml and curl libraries on PHP.If you want you can do it on your own language.I had done that on php.Also we need libnotify-bin.All this things you can get in Ubuntu repositories.

#!/usr/bin/php
<?php
/* Get content via cURL */
$ch = curl_init();
 
$postdata = array(
    'username'   => 'odeskUsername',
    'password'   => 'odeskPassword',
    'rememberMe' => '1',
);
 
$options = array(
    CURLOPT_URL            => 'https://www.odesk.com/login',
    CURLOPT_USERAGENT      => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36
    (KHTML, like Gecko) Chrome/27.0.1453.110',
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_POST           => TRUE,
    CURLOPT_POSTFIELDS     => $postdata,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_COOKIEFILE     => 'cookie.txt',
    CURLOPT_COOKIEJAR      => 'cookie.txt',
    CURLOPT_RETURNTRANSFER => TRUE,
 
);
 
curl_setopt_array($ch,$options);
$html = curl_exec($ch);
curl_close($ch);
 
/* Parsing content via php native DOM manipulator */
$dom = new DOMDocument();
$dom->loadHTML($html);
 
foreach ($dom->getElementsByTagName('article') as $node) {
 
    if ($node->getAttribute('data-position') == '1') {
 
        $tree = simplexml_import_dom($node);
 
        //Get header and body of message by simplexml
        $body = trim(htmlspecialchars(ucfirst($tree->div[2])));
 
        if($body == NULL){
            $body = trim(htmlspecialchars(ucfirst($tree->div[2]->div[0])));
        }
 
        $header =  htmlspecialchars(ucfirst($tree->h1->a));
        $file = fopen(dirname(__FILE__).'/hash.txt','r+');
 
        //Write hash of header to file and read from this file
        //After that check,post changes or not,if not just exit
        if (file_get_contents(dirname(__FILE__).'/hash.txt') != md5($header)) {
            //path of odesk icon
            $iconpath = dirname(__FILE__).'/logo.png';
            //prepare execute command in current user shell
            $command = "notify-send -t 20000 -i $iconpath "$header" "$body"";
            system($command);
        }
 
        fwrite($file,md5($header));
        break;
    }
}

This script get content from your home odesk page, parse and notify you.In notify box displaying last work at current moment.You can improve this but I guess it’s not necessary.

If you want to run this script you have to type in terminal several commands.

chmod +x odesk.php
./odesk.php

Also place in script folder two empty files for cookie/hash and set permission for them.

Last step – modify crontab.For example, run script every minute:

* * * * * env DISPLAY=:0 ~/Desktop/odesk.php

And now Odesk notifications should display in right part of your screen. I hope it will help you in your freelance career:)


About the Author / Artem Zhuravlev

Artem Zhuravlev. Web developer. Blog writer.

Need help with your website ? Contact with me by email infzanoza@gmail.com for services of experienced web developer.

Follow @infernosquad

LEAVE A COMMENT