﻿{"id":841,"date":"2010-11-15T19:30:34","date_gmt":"2010-11-15T18:30:34","guid":{"rendered":"http:\/\/www.flapane.com\/blog\/?p=841"},"modified":"2016-06-27T11:02:28","modified_gmt":"2016-06-27T10:02:28","slug":"tweetlight-twitter-parser-rapido","status":"publish","type":"post","link":"https:\/\/www.flapane.com\/blog\/2010\/11\/tweetlight-twitter-parser-rapido\/","title":{"rendered":"Tweetlight: il migliore twitter parser per mostrare i propri tweets"},"content":{"rendered":"<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-842 image\" title=\"twitter-pic2\" alt=\"\" src=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/twitter-pic2.jpg\" width=\"406\" height=\"198\" \/><\/p>\n<p style=\"text-align: justify;\">Diciamo la verit\u00e0: il widget <a title=\"twitter widget\" href=\"http:\/\/twitter.com\/settings\/widgets\u200e\">widget di twitter<\/a> per mostrare, sul proprio sito o blog, i nostri ultimi tweet (ovvero la timeline), \u00e8 abbastanza brutto.<\/p>\n<p style=\"text-align: justify;\">Non permette la personalizzazione di ogni aspetto, ed \u00e8 in javascript, cosa che non piace a tutti.<\/p>\n<p style=\"text-align: justify;\">In un p\u00f2 di tempo libero ho scritto un twitter parser (cio\u00e8 un codice che mostra gli ultimi tweets di un utente), e l&#8217;ho chiamato <span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\"><strong>Tweetlight &#8211; A lightweight json twitter parser<\/strong><\/span><\/span>.<\/p>\n<p style=\"text-align: justify;\">E&#8217;scritto in php e sfrutta lo stream contenente gli ultimi tweet in formato json.<!--more--><\/p>\n<p style=\"text-align: justify;\">Grazie ad una cache, si pu\u00f2 impostare il numero di minuti dopo cui bisogna andare a controllare se ci sono nuovi tweets (10 minuti vanno bene).<br \/>\nIn questo modo il caricamento \u00e8 velocissimo, in quanto i tweet vengono letti da un file di cache (<strong>tempo di esecuzione dello script: poco meno di 2 millisecondi<\/strong>).<\/p>\n<p style=\"text-align: justify;\">Tweetlight si compone di tre file:<\/p>\n<ul style=\"text-align: justify;\">\n<li><strong>tweetlight.php<\/strong>: Il codice vero e proprio, in php.<\/li>\n<li><strong>tweetfunc.inc<\/strong>: Funzioni richieste da tweetlight.php<\/li>\n<li><strong>output.php<\/strong>: Il file che mostra il risultato finale, l&#8217;output, che pu\u00f2 essere incollato dove si vuole.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Nel primo file, bisogna inserire il proprio username di twitter nel campo $username, il tempo di durata della cache nel campo $expiretime, ed il numero di tweet da mostrare nel campo $number_tweets.<\/p>\n<p style=\"text-align: justify;\">Vediamo una anteprima di questo file:<span class=\"no_translate\"><\/span><\/p>\n<pre class=\"brush:php\">&lt;?\r\n\/* Tweetlight - A lightweight json twitter parser (mean execution ltime less than 2ms, using cache).\r\nSet your user name in $username, the number of minutes you want to refresh the cache after in $expiretime (default 10 minutes), and the number of tweets to show in $number_tweets.\r\nYou can customize the output as you want, this script is meant to be fast, so it doesn't carry any fancy customization.\r\nLast rev: 20-apr-11 by flapane.com *\/\r\n\r\ninclude('tweetfunc.inc'); \/\/ functions\r\n$username='flapane'; \t \/\/ insert your twitter user name\r\n$format='json'; \t\t \/\/ format \r\n$expiretime=10; \t\t \/\/ cache expire time in minutes\r\n$number_tweets=5; \t\t \/\/ number of tweets to display\r\n$cachename=\"tweetcache_\".$username.\".json\"; \/\/name of the cachefile\r\n\r\n\/\/create the cachefile if it doesn't exist\r\nif (!file_exists($cachename)) {\r\n     $create = fopen($cachename, 'w');\r\n \t chmod (\"$cachename\", 0777); \/\/set chmod 777\r\n\t fclose($create);\r\n}\r\n\r\n\/\/ Is the file older than $expiretime, or the file is new\/empty?\r\n$FileAge = time() - filectime($cachename);\t\/\/ Calculate file age in seconds\r\nif ($FileAge &gt; ($expiretime * 60) || 0 == filesize($cachename))\r\n{\r\n\t\/\/ Now refresh the cachefile with newer content (in number equal to $number_tweets)\r\n\t$handle = fopen(\"http:\/\/api.twitter.com\/1\/statuses\/user_timeline\/{$username}.{$format}?count=$number_tweets\", \"r+\");  \r\n\t$jsoncontent = stream_get_contents($handle);\r\n\tfclose($handle);\r\n\t$handle = fopen($cachename, 'wb');\r\n\tfwrite($handle, $jsoncontent);\t\t\t\r\n}\r\n\r\n$tweet=json_decode(file_get_contents($cachename)); \/\/ get tweets and decode them into a php array\r\n\r\n$tweet_avatar = $tweet[0]-&gt;user-&gt;profile_image_url; \/\/display your twitter avatar\r\n\r\necho \"&lt;img src='$tweet_avatar' width='30' height='30' alt='twitter avatar' \/&gt;&lt;b&gt; Last \".$number_tweets.\" tweets for \".$username.\"&lt;\/b&gt;&lt;br \/&gt;&lt;br \/&gt;\";\r\n\r\n\/\/output $number_tweets tweets from cache file\t\r\nfor ($i=0; $i&lt;=($number_tweets-1);$i++) \r\n{\r\n$tweet_text = $tweet[$i]-&gt;text; \/\/the tweet text\r\n$tweet_link = $tweet[$i]-&gt;id_str; \/\/twitter status link\r\n$posted_at = explode(\" \", $tweet[$i]-&gt;created_at, 6); \/\/twitter date\r\n$some_date = \"$posted_at[0] $posted_at[1] $posted_at[2] $posted_at[3] $posted_at[4] $posted_at[5]\";\r\n$postedago = adjust_twitter_date($some_date) .\"&lt;br \/&gt;\"; \/\/adjust the twitter date output\r\n\r\necho \"&lt;div id='tweet_text'&gt;\".$tweet_text = preg_replace(\"\/(?!(?:[^&lt;]+&gt;|[^&gt;]+&lt;\\\/a&gt;))\\b((http:\\\/\\\/)(.*?)\\\/([\\w\\.\\\/\\&amp;\\=\\?\\-\\,\\:\\;\\#\\_\\~\\%\\+]*))\\b\/is\", \"&lt;a href=\\\"\\\\1\\\" target=\\\"\\\\_blank\\\"&gt;\\\\1&lt;\/a&gt;\",$tweet_text).\"&lt;br \/&gt;&lt;\/div&gt;\"; \/\/linkify the links in the tweet, and output the tweet\r\n\r\necho \"&lt;div id='tweet_date'&gt;&lt;b&gt;&lt;a href='http:\/\/twitter.com\/$username\/statuses\/$tweet_link'&gt;# posted \".human_time_diff(time(),$postedago) .\" ago&lt;\/a&gt;&lt;\/b&gt;&lt;\/div&gt;&lt;br \/&gt;\"; \/\/output the tweet age\r\n}\r\n?&gt;<\/pre>\n<p style=\"text-align: justify;\"><\/span>Per chiunque voglia capirne il funzionamento, ci sono i commenti vicino ogni funzione e variabile principale, ed in breve:<\/p>\n<ul style=\"text-align: justify;\">\n<li>Prende lo stream in formato json<\/li>\n<li>Controlla se il file di cache \u00e8 gi\u00e0 esistente<\/li>\n<li>Se il file di cache \u00e8 recente, usa quello senza aggiornarlo<\/li>\n<li>Se il file di cache \u00e8 vecchio, aggiornalo<\/li>\n<li>Mostra il numero di tweet desiderati<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Il file tweetfunc.inc non va toccato, serve per il funzionamento dello script.<\/p>\n<p style=\"text-align: justify;\">La prima volta che si lancia lo script, verr\u00e0 creato un file di cache del tipo <strong>tweetcache_USERNAME.json<\/strong><\/p>\n<p style=\"text-align: justify;\">Questa \u00e8 una screenshoot di come apparir\u00e0 l&#8217;output:<\/p>\n<div id=\"attachment_844\" style=\"width: 487px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/tweetlight_screen.jpg\" rel=\"lightbox[841]\"><img aria-describedby=\"caption-attachment-844\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-844  image\" title=\"tweetlight_screen\" alt=\"Screenshoot di tweetlight, twitter parser\" src=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/tweetlight_screen-530x204.jpg\" width=\"477\" height=\"184\" srcset=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/tweetlight_screen-530x204.jpg 530w, https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/tweetlight_screen.jpg 1034w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/a><p id=\"caption-attachment-844\" class=\"wp-caption-text\">Screenshoot di tweetlight, ecco come si presenta l&#8217;output nudo e crudo.<\/p><\/div>\n<p style=\"text-align: justify;\">Come potete vedere, lo script \u00e8 &#8220;nudo e crudo&#8221;, senza customizzazioni via foglio di stile CSS, in modo che sia il pi\u00f9 semplice, veloce, e modificabile possibile.<\/p>\n<p style=\"text-align: justify;\">Sta a voi, se volete, modificarne l&#8217;aspetto, magari aggiungendo uno sfondo.<\/p>\n<p style=\"text-align: justify;\"><strong>Potete scaricare il file zip contenente lo script (E caricarlo sul vostro server ftp) a questo indirizzo: <a href=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2010\/11\/tweetlight.zip\">Tweetlight &#8211; A lightweight json twitter parser<\/a>.<\/strong><\/p>\n<p style=\"text-align: justify;\">Se volete, scrivete un commento con il vostro parere!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Diciamo la verit\u00e0: il widget offerto da Twitter per mostrare, sul proprio sito o blog, i nostri ultimi tweet (ovvero la timeline), \u00e8 abbastanza brutto.Non permette la personalizzazione di ogni aspetto, ed \u00e8 in javascript, cosa che non piace a tutti.<br \/>\nIn un p\u00f2 di tempo libero ho scritto un twitter parser (cio\u00e8 un codice che mostra gli ultimi tweets di un utente), e l&#8217;ho chiamato Tweetlight[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[200,199,201,14,15,138],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/posts\/841"}],"collection":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":0,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}