Mostrare le statistiche di Flightmemory sul proprio sito | Flavio's blog


Mostrare le statistiche di Flightmemory sul proprio sito

Flightmemory.com logo

Flightmemory.com è un ottimo sito per salvare e mostrare tutte le statistiche sui nostri voli, la durata, il numero del volo, la compagnia, il tipo di aeromobile, le marche, permette di esprimere giudizi sugli aeroporti e le compagnie aeree.
Inoltre, genera automaticamente una mappa dove si possono vedere tutte le tratte volate.

Volevo mostrare alcune mie statistiche tratte dal mio profilo su Flightmemory nella mia homepage ma, purtroppo, ad oggi, Flightmemory NON dispone ancora di API per estrarre i dati.

Per ovviare a ciò, è bastato modificare lo script che avevo già creato per Runkeeper e Sportstracklive, ed estrarre i dati che volevo, ovvero:

  • Il tempo totale trascorso in volo
  • Il computo totale dei kilometri volati
  • Durata media di un volo

Ecco il risultato, visibile nella mia homepage:

flightmemory_stats parser

Andiamo a vedere un pezzo dello script:

<?
/* Flightmemory.com data parser. It automatically shows data from the stats page for a given username. Set your username, cache file name, and expiration time of the cachefile.
(c) Flapane.com - Feb-2013*/

define('FLIGHTMEMORY_USER','type_your_username');		//Flightmemory Username
$expiretime=3; 		 // cache expire time in days
$cachename="flightmemory.cache"; //name of the cachefile

//create the cachefile if it doesn't exist
if (!file_exists($cachename)) {
     $create = fopen($cachename, 'w');
 	 chmod ("$cachename", 0644); //set chmod 644
	 fclose($create);
}

// Is the file older than $expiretime, or the file is new/empty?
$FileAge = time() - filectime($cachename);	// Calculate file age in seconds
if ($FileAge > ($expiretime * 3600 * 24) || 0 == filesize($cachename))
{
	include_once('simple_html_dom.php');
     $opts = array(
  'http'=>array(
    'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.2.3) Gecko/20100401 MRA 5.6 (build 03278) Firefox/3.6.3 (.NET CLR 3.5.30729)',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  )
);
     $context = stream_context_create($opts);    //create an useragent for simple_html_dom
	$html = file_get_html('http://my.flightmemory.com/'.FLIGHTMEMORY_USER, 0, $context);
	$chilometri_table = $html->find('table', 2);	// chilometri totali
	$ore_table = $html->find('table', 3);	// ore totali
	$statsvoli_table = $html->find('table', 5);	//longest-shortest-avg flight

	$chilometri = strip_tags($chilometri_table->find('.liste', 1)->find('td', 1)); //strip tags
	$ore = preg_replace("/(?:\s| )+/", "", strip_tags($ore_table->find('.liste', 0)->find('td', 1)), -1); //strip tags and delete white spaces from hours
	$statsvoli = strip_tags($statsvoli_table->find('tr', 3)->find('td', 1)); //strip tags

	// Now refresh the cachefile with newer content

	$stats = 'Distanza totale:<b> '.$chilometri.'km</b><br />Tempo totale:<b> '.$ore.'h</b><br />Media per volo:<b> '.$statsvoli.'</b>';

	$handle = fopen($cachename, 'wb');
	fwrite($handle, $stats);			
}

//cachefile is fresh enough... outputting data
$data=file_get_contents($cachename);
echo $data;
?>

Una volta scaricato il file zip linkato alla fine dell’articolo, caricatelo su un server ftp.

Le uniche cose che dovremo andare a modificare sono type_yourusername con il vostro username di Flightmemory.com, ed il valore expiretime (in giorni) dopo il quale il file di cache con le statistiche dovrà essere aggiornato.

Lanciando lo script, verrà creato un file di cache, chiamato flightmemory.cache, che potrà essere posizionato dove volete.

A titolo di esempio, ecco un possibile contenuto del file di cache:

Distanza totale:<b> 56,040km</b><br />Tempo trascorso in volo:<b> 78:07h</b><br />Media per volo:<b> 2,949 km, 4:07 h</b>

Lo script (completo di simple_html_dom) è scaricabile a questo indirizzo: Flightmemory PHP Parser

Qualsiasi feedback o consiglio è il benvenuto!



0 commenti »

Per piacere accetta i cookie di terze parti per poter commentare il post! Il pulsante CAMBIA LE SCELTE DEI COOKIE si trova nel footer del sito. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.