﻿{"id":1346,"date":"2013-03-07T21:15:16","date_gmt":"2013-03-07T20:15:16","guid":{"rendered":"http:\/\/www.flapane.com\/blog\/?p=1346"},"modified":"2013-09-29T18:15:24","modified_gmt":"2013-09-29T17:15:24","slug":"mostrare-le-statistiche-di-flightmemory-sul-proprio-sito","status":"publish","type":"post","link":"https:\/\/www.flapane.com\/blog\/2013\/03\/mostrare-le-statistiche-di-flightmemory-sul-proprio-sito\/","title":{"rendered":"Mostrare le statistiche di Flightmemory sul proprio sito"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1347 image\" alt=\"Flightmemory.com logo\" src=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2013\/01\/flightmemory_logo.png\" width=\"544\" height=\"121\" \/><\/p>\n<p><strong><a title=\"Flightmemory statistiche dei voli\" href=\"http:\/\/www.flightmemory.com\/\" target=\"_blank\">Flightmemory.com<\/a><\/strong> \u00e8 un ottimo sito per<strong> salvare e mostrare tutte le statistiche sui nostri voli<\/strong>, la durata, il numero del volo, la compagnia, il tipo di aeromobile, le marche, permette di esprimere giudizi sugli aeroporti e le compagnie aeree.<br \/>\nInoltre, genera automaticamente<strong> una mappa dove si possono vedere tutte le tratte volate<\/strong>.<\/p>\n<p>Volevo mostrare alcune mie <strong>statistiche tratte dal mio profilo su Flightmemory<\/strong> nella mia homepage ma, purtroppo, ad oggi, Flightmemory NON dispone ancora di API per estrarre i dati.<\/p>\n<p>Per ovviare a ci\u00f2, \u00e8 bastato modificare <a title=\"PHP parser per Sportstracklive e runkeeper\" href=\"https:\/\/www.flapane.com\/blog\/2012\/06\/interfaccia-in-php-per-runkeeper\/\" target=\"_blank\">lo <strong>script<\/strong> che avevo gi\u00e0 creato per Runkeeper e Sportstracklive<\/a>, ed estrarre i dati che volevo, ovvero:<!--more--><\/p>\n<ul>\n<li>Il <strong>tempo totale<\/strong> trascorso in volo<\/li>\n<li>Il computo totale dei<strong> kilometri volati<\/strong><\/li>\n<li><strong>Durata media<\/strong> di un volo<\/li>\n<\/ul>\n<p>Ecco il risultato, visibile nella mia homepage:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1349 image\" alt=\"flightmemory_stats parser\" src=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2013\/03\/flightmemory_stats.png\" width=\"329\" height=\"127\" \/><\/p>\n<p>Andiamo a vedere un pezzo dello script:<span class=\"no_translate\"><\/span><\/p>\n<pre class=\"brush:php\">&lt;?\r\n\/* 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.\r\n(c) Flapane.com - Feb-2013*\/\r\n\r\ndefine('FLIGHTMEMORY_USER','type_your_username');\t\t\/\/Flightmemory Username\r\n$expiretime=3; \t\t \/\/ cache expire time in days\r\n$cachename=\"flightmemory.cache\"; \/\/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\", 0644); \/\/set chmod 644\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 * 3600 * 24) || 0 == filesize($cachename))\r\n{\r\n\tinclude_once('simple_html_dom.php');\r\n     $opts = array(\r\n\u00a0 'http'=&gt;array(\r\n\u00a0\u00a0\u00a0 'user_agent'=&gt;'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)',\r\n\u00a0\u00a0 \u00a0'Accept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,*\/*;q=0.8'\r\n\u00a0 )\r\n);\r\n\u00a0\u00a0 \u00a0 $context = stream_context_create($opts);\u00a0\u00a0 \u00a0\/\/create an useragent for simple_html_dom\r\n\t$html = file_get_html('http:\/\/my.flightmemory.com\/'.FLIGHTMEMORY_USER, 0, $context);\r\n\t$chilometri_table = $html-&gt;find('table', 2);\t\/\/ chilometri totali\r\n\t$ore_table = $html-&gt;find('table', 3);\t\/\/ ore totali\r\n\t$statsvoli_table = $html-&gt;find('table', 5);\t\/\/longest-shortest-avg flight\r\n\r\n\t$chilometri = strip_tags($chilometri_table-&gt;find('.liste', 1)-&gt;find('td', 1)); \/\/strip tags\r\n\t$ore = preg_replace(\"\/(?:\\s|\u00a0)+\/\", \"\", strip_tags($ore_table-&gt;find('.liste', 0)-&gt;find('td', 1)), -1); \/\/strip tags and delete white spaces from hours\r\n\t$statsvoli = strip_tags($statsvoli_table-&gt;find('tr', 3)-&gt;find('td', 1)); \/\/strip tags\r\n\r\n\t\/\/ Now refresh the cachefile with newer content\r\n\r\n\t$stats = 'Distanza totale:&lt;b&gt; '.$chilometri.'km&lt;\/b&gt;&lt;br \/&gt;Tempo totale:&lt;b&gt; '.$ore.'h&lt;\/b&gt;&lt;br \/&gt;Media per volo:&lt;b&gt; '.$statsvoli.'&lt;\/b&gt;';\r\n\r\n\t$handle = fopen($cachename, 'wb');\r\n\tfwrite($handle, $stats);\t\t\t\r\n}\r\n\r\n\/\/cachefile is fresh enough... outputting data\r\n$data=file_get_contents($cachename);\r\necho $data;\r\n?&gt;<\/pre>\n<p><span class=\"no_translate\"><\/span>Una volta scaricato il file zip linkato alla fine dell&#8217;articolo, caricatelo su un server ftp.<\/p>\n<p>Le uniche cose che dovremo andare a modificare sono\u00a0<strong>type_yourusername<\/strong> con il vostro username di Flightmemory.com, ed il valore <strong>expiretime<\/strong> (in giorni) dopo il quale il file di cache con le statistiche dovr\u00e0 essere aggiornato.<\/p>\n<p>Lanciando lo script, verr\u00e0 creato un file di cache, chiamato flightmemory.cache, che potr\u00e0 essere posizionato dove volete.<\/p>\n<p>A titolo di esempio, ecco un possibile contenuto del file di cache:<\/p>\n<pre class=\"brush:php\">Distanza totale:&lt;b&gt; 56,040km&lt;\/b&gt;&lt;br \/&gt;Tempo trascorso in volo:&lt;b&gt; 78:07h&lt;\/b&gt;&lt;br \/&gt;Media per volo:&lt;b&gt; 2,949 km, 4:07 h&lt;\/b&gt;<\/pre>\n<p>Lo script (completo di simple_html_dom) \u00e8 scaricabile a questo indirizzo: <strong><a href=\"https:\/\/www.flapane.com\/blog\/wp-content\/uploads\/2013\/03\/flightmemory_parser.zip\" target=\"_blank\">Flightmemory PHP Parser<\/a><\/strong><\/p>\n<p>Qualsiasi feedback o consiglio \u00e8 il benvenuto!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flightmemory.com \u00e8 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.<br \/>\nVolevo 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.[&#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":[217,200,226,201,14,135],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/posts\/1346"}],"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=1346"}],"version-history":[{"count":0,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/posts\/1346\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/media?parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flapane.com\/blog\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}