lundi 31 janvier 2011

Processing and Twitter

Of course, just when twitter is working in Flash, somebody wants it in Processing.

http://tinkerlondon.com/now/2010/09/14/oauth-twitter-and-processing/
http://twitter4j.org/en/index.html#download

The most difficult thing is to get the Processing lib right (wrong?). 

But do we really need this?
Couldn't we call the same PHP file we used in the last FLASH script?

Better still, there is XMLElement in Processing:


XMLElement xml;

void setup() {
size(800, 480);
xml = new XMLElement(this, "http://twitter.com/statuses/user_timeline/111370674.rss");
}

void draw() {

println( " number of xml children " + xml.getChildCount() );

XMLElement myChannel = xml.getChild(0);
println( " number of xml children " + myChannel.getChildCount() );

XMLElement[] tweets = myChannel.getChildren("item");

for (int i = 0; i < tweets.length; i++) { fill(0); textSize(9);

println(i + " " + tweets[i].getChild(0) ); 
text( tweets[i].getChild(0).getContent() , 10, 20 + i*30); 
} noLoop(); 
}

dimanche 16 janvier 2011

Twitter in FLASH (using OAUTH)

Twitter and Flash, twitter in your FLASH movie, getting the tweets, catching some significant words and provoke a FLASH movie reaction to your tweets....

Getting your twitter timeline is just using the rssfeed of your twitter account, for example contrechoc tweets:
http://www.contrechoc.com/crosslab/twitterRSS/twitter.php

a PHP file which consists of:
$twitterFeed = 'http://twitter.com/statuses/user_timeline/111370674.rss';
$outputFeed = @file_get_contents($twitterFeed);
print $outputFeed;

so the most important line is
http://twitter.com/statuses/user_timeline/111370674.rss

everyone can do this, you can get your twitter number here:
http://api.twitter.com/1/users/show.xml?screen_name=contrechoc
instead of contrechoc, put your own twittername.
or use:
http://idfromuser.com to get your twitter unique key.
Then you see the number above the name line. (This page is also a nice example of XML.)
This page gives simple examples how to use twitter data on your site using php:
http://www.problogdesign.com/wordpress/how-to-get-your-twitter-follower-number-in-plain-text/

More difficult is making a tweet.

Some special twitter vocabulary:
Sending a tweet is called "Updating status". Getting the tweets is called: "Getting Timeline". Then there is: "Sending and receiving direct messages".

For these acctions to be programmed we need to log in into our twitter account, which was rather easy some time ago. The script for this, often done by a programmer, needed to incorporate the login and password. But this was giving your credentials away, letting your password lying around on a desk of somebody else.

Nowadays the script can be made without giving away your credentials using OAUTH. But this security makes life much more complex.

We have to know a little bit about OAUTH, which cannot be done using FLASH. We need a PHP library as an intermediate between the "database" Twitter and our FLASH movie.

What is OAUTH?
 http://dev.twitter.com/pages/oauth_faq

On this page there is an example illustrating the steps to get an app functioning with OAUTH:
http://blogs.sitepoint.com/2010/08/17/oauth-explained-with-foursquar/

Go to http://foursquare.com/ and experience how this looks.

This is a nice graphical representation of this OAUTH authentication process:
http://developers.new.digg.com/authentication

The PHP library we use is the lib of Abraham Williams, which you can get here:
https://github.com/abraham/twitteroauth

Then you need your sercret key and token as described in the documentation at
http://dev.twitter.com/pages/auth
and change the files using your own secret key.

At this lib I added a simple PHP file, which after logging in just writes an update Status:
echo $connection->post('statuses/update', array('status' => $statusText.' at '.date(DATE_RFC822)));


http://www.contrechoc.com/crosslab/abrahamTwitter/writeStatusShort.php?statusText=hello world

From this last line Hello World can be twittered.... (if you are logged in...see OAUTH description).

So putting the things together gives a movie, getting the last tweets, and being able to submit a tweet:
http://www.contrechoc.com/crosslab/abrahamTwitter/twitterFlashWithInput.html

of course as an example this is giving contrechoc's tweets (rssfeed)- submitting tweets is only possible when logged in...the redirection page has to be altered.

FLASH

Then we return to FLASH scripting, to get a media response to a tweet, we will search for a string in a bit of text (tweet = text):

for this we need a RegExp object:
var myPattern:RegExp = /bestseller/gi;

then we can search a String for this RegExp : (
var bitOfText: String = "when the sea of inspiration is empty, all fishing for new ideas is in vain, unless you are a bestseller";
//coming from a tweet for example
if ( bitOfText.search(myPattern1) != -1 )
             {
                 trace ("found ", myPattern );
//do something with it!
             }

When we have found "bestseller", then we can use the media template to show an image, or start a video, sound etc.

We need the FLASH movie also to get updates itself. For this we use a timer:

var myTimer: Timer;
myTimer = new Timer(500);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();

function timerHandler(event:TimerEvent):void {
   //function to get rssfeed from twitter and go to search for certain words
}

The file for this last example can be found here:
http://www.contrechoc.com/crosslab/twitter_media_Example.zip
When the FLASH movie is not the right version, you can take the script from the textfile in the folder, take a new FLASH file and copy this script inside the actions window. To get your own tweets inside a flash, you should alter the rssFeedURL inside the script.

To write yourself tweets from your FLASH, as told, you need the whole bunch of PHP files and get yourself secret keys etc from twitter.

This is an example of FLASH with OAUTH, which won't really work for you, because it is works only when I log in....
http://www.contrechoc.com/crosslab/abrahamTwitter/twitterFlashWithInput.html

Extra:
search a word in all tweets:
http://search.twitter.com/search.atom?q=hello

dimanche 9 janvier 2011

Database excercizes

Email
We practiced making a table. Now look at email as a database-table, find all the necessary fields (and what is missing or not working for a single table?)

Twitter
Look at twitter as database, the same questions. Get to your twitter account, see how the twittering works from the database side. Look here:
In class links are shown for flash showing tweets, and submitting new tweets.

Layar
database, how to do this yourself
now  we understand what we did at hoppala:
http://augmentation.hoppala.eu/
we can find the MYSQL code to make the layar database on our own free server without the hoppala tag:
http://layar.pbworks.com/w/page/30832324/First%20Layar%20Tutorial%20-%20Create%20a%20simple%20layer
make this table on your server!
See this post:
http://crosslabminor-layar.blogspot.com/2010/09/make-layar-layer-hard-way.html

insert
Inserting records in a table from a html page makes life easier:
A link will be shown in class (fearing database injections!) which inserts records in my layar database.
http://www.contrechoc.com/XXX/xxx

Second Life....
Nice 3D world. In fact, it is just a database visualisation. We have put a microphone somewhere, which records what is said in it's neighbourhood. The microphone is illustrating that everything in this world is recorded. The owner didn't want to be reminded of this fact!
http://www.nuggetkidd.byethost11.com/sl/loadToFlash_Nugget_Kidd.php
gives the raw data, which can be shown in a (undesigned) flash movie:
http://www.nuggetkidd.byethost11.com/minorCourse/loadDB_Nugget_Kidd.html

The World
Is our world just a database?

http://rop.gonggri.jp/?p=442

Check this out:
http://www.online-tech-tips.com/google-softwaretips/use-gmail-as-an-online-database-with-super-fast-search/