2013-08-21

Twitter from the coolest Data Warehouse

Last week a colleague said - Why don’t we Twitter the status from our Data Warehouse servers?

My reply was I tried to set it up a year ago or so but I failed. I used the Zend Framework one dot something. But of course I had to try again. So I started by downloading the Zend Framework 2 version 2.2.2. According to the documentation this was a simple task. After failing a number of times I realised the Twitter class was not in the the vs 2.2.2, after some googling around I found the code and installed it. But still my code did not find the ZF2 twitter code. I could not get the ZF2 autoloader to work so I had to require the ZF2 twitter code manually. Now there were some Oauth classes missing, after some googling I found the code and installed it. I could not get the autoloader to include the Oauth code so I had to require the code manually. And now I descended  into class bonanza hell, the Twitter and Oauth classes referred to other classes and interfaces and abstractions and God knows what. Since the autoloader is too hard for me I had to load all manually, trial and error until I had included all code needed to set up my classes to call Twitter. I was able to verify my credentials and fetch basic information about my Twitter account. But when I tried to post ‘Hello World!’ I got error Http return code 400. After trying a good number of times and googling a lot without any result, I had to give up.

One aspect I do not like with Object Orientation is the extreme complexity. All OO projects starts with a few simple classes with logical methods. But as the system grows (and changes) new classes are introduced which inherits from the original simple classes with almost identical methods. And then hell break loose, now come interfaces, roles, traits, mixins, abstractions, adapters and God knows what. And all these entities should be coupled and attached to each other in ways only logical to the developer. It is no simple task to dig into lots of alien errors like:  

PHP Fatal error:  Uncaught exception 'Zend\Http\Client\Adapter\Exception\RuntimeException' with message 'Error in cURL

request: Received HTTP code 400 from proxy after CONNECT' in /home/tooljn/PHPZF/ZendFramework-2.2.2/library/Zend/Http/Client/Adapter/Curl.php:422

Even if you have stack traces (which always are very long) you wonder what code is executed where and why?

There is seldom intuitivity in OO system, i.e. easy to understand and learn how connect OO object, you very often have to learn case by case.

This is more or less how my ZF2 code looked like when I gave up:

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));

// Register with spl_autoload:

$loader->register();

require_once 'Zend/ZendService/Twitter/Twitter.php';

require_once 'Zend/ZendService/Twitter/Response.php';

require_once 'Zend/ZendOAuth/OAuth.php';

require_once 'Zend/ZendOAuth/Config/ConfigInterface.php';

require_once 'Zend/ZendOAuth/Config/StandardConfig.php';

require_once 'Zend/ZendOAuth/Client.php';

require_once 'Zend/ZendOAuth/Http/Utility.php';

require_once 'Zend/ZendOAuth/Token/TokenInterface.php';

require_once 'Zend/ZendOAuth/Token/AbstractToken.php';

require_once 'Zend/ZendOAuth/Token/Access.php';

require_once 'Zend/ZendOAuth/Signature/SignatureInterface.php';

require_once 'Zend/ZendOAuth/Signature/AbstractSignature.php';

require_once 'Zend/ZendOAuth/Signature/Hmac.php';

array_key_exists('host',$job) ? $hostInfo = $xmlconverter['host'][0]['value'] : $hostInfo = 'twitter';

array_key_exists('app',$job) ? $tapp = $xmlconverter['app'][0]['value'] : $tapp = 'DW status';

$txml = getHostSysDsn($context, $hostInfo, "$tapp");

if (!$txml) return FALSE;

$curl_config = array(

        'adapter' => 'Zend\Http\Client\Adapter\Curl',

        'curloptions' => array(

            CURLOPT_SSL_VERIFYHOST => false,

            CURLOPT_SSL_VERIFYPEER => false));

           

$twitter_config = array(

    'access_token' => array(

        'token'  => $txml['Accesstoken'],

        'secret' => $txml['Accesstokensecret'],

    ),

    'oauth_options' => array(

        'consumerKey' => $txml['Consumerkey'],

        'consumerSecret' => $txml['Consumersecret'],

    ),

    'http_client_options' =>$curl_config

);

//var_dump($twitter_config);

$twitter = new ZendService\Twitter\Twitter($twitter_config);

$twitter->statuses->update('Hello World!');

The last line blows up with an error code 400 from Twitter, I have no idea why.

(In all fairness I should link to a post I wrote a long time ago with a cool example using  Zend Framwork1 at the end  .)

I do not think ZF2 is bad , it’s just too complex for me, and this often the case with OO systems. OO invites you to go complex over time. But this does not have to be, in this particular case I found an OO gem , A very simple class for posting on Twitter (the author  David Grudl  claims you can do any Twitter task from this class). Using David’s OO class I only had to write this code:

$message = trim(substr($job['message'][0]['value'],0,140));

$log->logit('Info',"Enter script=$scriptName message=$message");

require_once '/home/tooljn/TWITTER/twitter-php-master/src/twitter.class.php';

array_key_exists('host',$job) ? $hostInfo = $xmlconverter['host'][0]['value'] : $hostInfo = 'twitter';

array_key_exists('app',$job) ? $tapp = $xmlconverter['app'][0]['value'] : $tapp = 'DW status';

$txml = getHostSysDsn($context, $hostInfo, "$tapp");

if (!$txml) return FALSE;

$twitter = new Twitter($txml['Consumerkey'], $txml['Consumersecret'], $txml['Accesstoken'], $txml['Accesstokensecret']);

try {

        $tweet = $twitter->send("$message");

        $_RESULT = TRUE;

} catch (TwitterException $e) {

        $log->logit('Errror', $e->getMessage());

}

return $_RESULT;

And the code posted on Twitter without any fuzz on first attempt. The code was run from this job :

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>

<schedule mustcomplete='yes' logmsg='This is a Twitter test'>

   <job name='twittaMsg' type='script' pgm='twitter01.php'>

      <iconv input='UTF-8' output='UTF-8' internal='UTF-8'/>

      <message>@TODAY-@NOW Hello Twitter</message>

  </job>

</schedule>

2013-08-12

Bluetooth, Spotify and Keith Richards

Finally I starting to find my new phone  useful. After some training I’m not only able to answer incoming calls but I can also call people with ease. Still I find it impossible to use the phone in the car. (When I figure out how the voice control works I may be able to use the phone in the car.) My sons tell me it’s an age thing, older people have difficulties with these new smart phones.

 

Anyway last week I managed to install Spotify in my phone and connected the phone to my home stereo. This is great, with my telephone I can play any music I like. But there was a slight inconvenience my phone was connected with a cord to my amplifier and I didn’t want a long cord lying across the floor so I can disc-jockey from my sofa. After googling around I found this little wonder . So now I can stream music wirelessly from the net via wifi to my smartphone and via bluetooth to my home stereo, extremely comfortable. I always resisted bluetooth with the argument the wire is a feature, making sure the devices stay in place, but to be honest I go bonkers on all wires I have connecting devices and they just get more. It’s time for a rethink. And I been told everyone is streaming wireless these days but me, I’m out of touch with modern times my sons tell me.  

Spotify and Bluetooth is great, from a comfortable position in my Sofa I can play any music of my own choice. But where do Keith Richards comes in the picture? Well I thought now I should reread his biography Life  and play the music he is writing about while reading. I listen to all kinds of music even some modern House music. But I always come back to old Rolling Stones albums, especially Beggars Banquet, Sticky Fingers & Exile on Main Street . In his book Mr Richards writes a lot about blues musicians like Muddy Waters, Little Walters, Elmore James, Robert Johnson. I probably still have lots of LP’s with these guys but I do not have a record player anymore.

And I’m not likely to buy anyone either. My phone is my music device now. While comfortably reading the book in the sofa, I will be able to listen to the music in Mr Richards book. Not that I listen to music that much these days, but it’s nice anyway. My sons tell me it comes with age, old people do not listen that much to music.

2013-08-06

Vacation again

Last year I went on a vacation road trip with my sons and the older ones girlfriend down to southern France. Same procedure this year, for some reason we set out on a similar trip down to Gignac in France, but this time we went home via Cannes, Nice, Milano and Hamburg. Last year we had a lot of of computers and internet devices with us which I described in Always online , the amount of devices at that time was insane and we said we only use smart phones next time, and we almost did,two Samsung Galaxy s4 and two Iphones 4s. But I didn’t dare to leave my laptop if something happened back home at the office. And of course I had my Kindle and the GPS Navigon 72 from last year. The PC-modem stopped work when we crossed the border to Denmark, fortunately nothing happened that required my intervention while I was away. The new Galaxy S4 phones were very good substitutes for pads as browsers. And if I only were allowed to connect  to the corporate network with the Galaxy it would be a great  emergency replacement for my laptop. But I was able to manage my bank accounts via the phone and this was very important since my credit card stopped work in Germany on our way down to France. Hotel rooms were booked via the phones all along the way, it’s very convenient to compare and book hotels via the web.

In Milano

Give Share Steal

For no reason at all; while I was jogging this morning it occurred to me that letting a subordinate use your knowledge without credit you is giving, while using someones knowledge and credit him/her for it is sharing, all other use of others knowledge is stealing.