Archive for May 28th, 2010
Custom twitter data fetching in php with cache
by z3n on May.28, 2010, under Coding, Tips & Hints
Problem:
I had to fit the twitter posts (“tweets”) on a site using a special layout, without all the regular twitter crapola
Solution:
There’s this old script that return contents as json, although i could simply add it to the javascript, it would slow down the site, everytime a new page was loaded user would need to fetch tweets all over again, so i did a little php function to fetch it cache and add to the output:
function _ld_twitter($profile=_twitter_profile,$tweets=_twitter_tweets) { // v1.0
// check cache
$hash=md5(md5($profile).md5($tweets));
if (_twitter_cache && file_exists(_twitter_cache_path.$hash.".idx") && file_exists(_twitter_cache_path.$hash) && _fs(_twitter_cache_path.$hash.".idx") > getmicrotime())
return _fs(_twitter_cache_path.$hash);
// build
$x=file_get_contents("http://twitter.com/statuses/user_timeline/".$profile.".json?callback=x&count=".$tweets);
if ($x != "") {
$x=json_decode(substr($x,2,-2));
$b=_fs(templates."twitter".file_ext);$r="";$i=count($x)-1;
foreach ($x as $k => $v)
$r.=str_replace(array(
"##WHO##","##IMG##",
"##WHEN##","##MSG##",
"##DELAY##","##LAST##"
),array(
$v->user->screen_name,$v->user->profile_image_url,
date(_dt2,strtotime($v->created_at)),not_utf8($v->text),
duration(getmicrotime()-strtotime($v->created_at),1,0),$k == $i ? " id='last'" : ""
),$b);
// do cache
if (_twitter_cache) {
_fw(_twitter_cache_path.$hash.".idx",(_twitter_cache_life*60) + getmicrotime(),"w");
_fw(_twitter_cache_path.$hash,$r,"w");
}
// return
return $r;
} else {
return false;
}
}
// defines -- yes not formatted correctly
"_twitter_profile" => "your_tweeter_profile_name", // twitter profile
"_twitter_tweets" => 3, // how many tweets to load
"_twitter_cache_life" => 15, // twitter cache life in minutes
"_twitter_cache_path" => "lib/cache/", // twitter cache path
"_twitter_cache" => 1, // enables / disables twitter cache
"_dt2" => "d M Y",
"templates" => "templates/",
"file_ext" => ".html"
/*
functions (not added here)
those functions are enhanced versions of basic functions, to try this script you can just take :
_fs = file_get_contents
_fw = file_put_contents
duration = a function to return a number of seconds into years, months, days, hours, minutes, seconds notation
*/
If you want to implement it as javascript, you can use the url:
http://twitter.com/statuses/user_timeline/”.$profile.”.json?callback=x&count=”.$tweets
where the callback is the function where the json data will be sent to.
IE z-index issues
by z3n on May.28, 2010, under Tips & Hints
Problem:
IE, in all it’s versions, ignores the z-index from non child elements, meaning that if you have a div inside another with z-index it’s fine, however, if there’s an external div (99%) of the cases, it will simply ignore the z-index.
Solution:
There’s no solution for this, but you can do a workaround. Even with IE ignoring the z-indexes it does has an order of placing elements in front of the others which is the order you have them on html, so if you have a header menu that should appear in the top of everything, you can place the html in the footer of the page, with position absolute, to the header position. This might not work for all cases, but helped mine. since the div in question was hidden and would appear when a element at header was clicked.
vdi -> vmdk
by z3n on May.28, 2010, under Linux Happyness, Tips & Hints
Problem:
Convert VirtualBox Disk Image (VDI) to VMWare Harddisk Format (VDMK)
Solution:
Solely using qemu will generate a broken image, the right solution is:
VBoxManage --convertSettings internalcommands converttoraw INPUT.vdi OUTPUT.raw && qemu-img convert -O vmdk OUTPUT.raw FINAL.vmdk && rm -f OUTPUT.raw
Source: