Advertisement
Brandan

WCCCA / CCOM Firemed twitter V2.0

Sep 14th, 2013
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.48 KB | None | 0 0
  1. <?PHP
  2. //WashCo_FireMed & ClackCo_FireMed Twitter Feed Script
  3. //Version 2.0.1
  4. // This is my attempt to make a cross compatible script with the old version of PITSv1 PHP by Gary Kee - gary.kee@moonrize.com - twitter.com/fractal74
  5.  
  6. // Setting
  7. $Use_Google_API_To_Get_Geo_Coordinates = 0;
  8. $Use_Twitter                           = 0;
  9. $Print_Debug                           = 1;
  10. $WCCCA_Version                         = 2;
  11. $uname                                 = 'washco_firemed'; //twitter login name for washco
  12. $unamec                                = 'clackco_firemed'; //twitter login name for clackco
  13. $pwd                                   = 'PASSWORD'; //twitter password
  14. $twitter_url                           = 'http://twitter.com/statuses/update.xml'; //twitter api url
  15.  
  16. // Ignore WCCCA PITSv2 HTML Errors (probably should fix)
  17. error_reporting(E_ERROR | E_PARSE);
  18.  
  19. // Download WCCCA PITSv2 full CAD site
  20. $HTML = file_get_contents("http://www.wccca.com/PITSv$WCCCA_Version/");
  21.  
  22. // Extract Call and filter to show only call infomation
  23. // WCCCA:
  24. $WCCCA_Calls = (Seperate_Calls($HTML, 0));
  25.  
  26. // CCOM:
  27. $CCOM_Calls = (Seperate_Calls($HTML, 1));
  28.  
  29. $Raw_WCCCA_Calls = split("\n", $WCCCA_Calls);
  30. $Raw_CCOM_Calls  = split("\n", $CCOM_Calls);
  31.  
  32. // Add County infomation for WCCCA
  33. $converted_wccca_calls;
  34. foreach ($Raw_WCCCA_Calls as $call) {
  35.     if (!empty($call)) {
  36.         $converted_wccca_calls .= "County: W Call:" . $call . "\n";
  37.     } //!empty($call)
  38. } //$Raw_WCCCA_Calls as $call
  39.  
  40. // Add County infomation for CCOM
  41. $converted_ccom_calls;
  42. foreach ($Raw_CCOM_Calls as $call) {
  43.     if (!empty($call)) {
  44.         $converted_ccom_calls .= "County: C Call:" . $call . "\n";
  45.     } //!empty($call)
  46. } //$Raw_CCOM_Calls as $call
  47.  
  48.  
  49. // Combind CCOM and WCCCA
  50. $Total_calls = $converted_ccom_calls . $converted_wccca_calls;
  51.  
  52. // Split Total calls for looping
  53. $raw_calls = split("\n", $Total_calls);
  54.  
  55. // Start the call loop
  56. foreach ($raw_calls as $call) {
  57.     if (!empty($call)) {
  58.        
  59.         // Get County
  60.         $county      = explode(' ', $call);
  61.         $cleancounty = $county[1];
  62.        
  63.         // Get Station
  64.         $station      = explode('Time: ', $call);
  65.         $station1     = explode(' ', $station[1]);
  66.         $cleanstation = $station1[5];
  67.        
  68.         // Get Agency
  69.         $agency      = $station1[4];
  70.         $cleanagency = str_replace("/", "", $agency);
  71.        
  72.         // Get Call
  73.         $raw_call  = split("Call: ", $call);
  74.         $raw_call2 = split(" Address:", $raw_call[1]);
  75.         $cleancall = $raw_call2[0];
  76.        
  77.         // Get Address
  78.         $raw_address   = split("Address: ", $call);
  79.         $raw_address1  = split(" Time:", $raw_address[1], 2);
  80.         $cleanlocation = preg_replace('/\W\w+\s*(\W*)$/', '$1', $raw_address1[0]);
  81.        
  82.         // Get GUID
  83.         $raw_guid  = str_replace("$cleanlocation", "", $raw_address1[0]);
  84.         $cleanguid = str_replace(' ', '', $raw_guid);
  85.        
  86.         // Get Units
  87.         $raw_units  = split(" Time:", $call);
  88.         $raw_units1 = split("/", $raw_units[1]);
  89.         $raw_units2 = split("- ", $raw_units1[1]);
  90.         $cleanunits = $raw_units2[1];
  91.        
  92.         if ($Use_Google_API_To_Get_Geo_Coordinates == 1) {
  93.             $raw_geocord  = getgeoloc($cleanlocation, $cleancounty);
  94.             $cleangeocord = split(",", $raw_geocord);
  95.             $llatt        = $cleangeocord[0];
  96.             $llonn        = $cleangeocord[1];
  97.         } //$Use_Google_API_To_Get_Geo_Coordinates == 1
  98.        
  99.         if (!empty($cleancounty)) {
  100.             if ($Print_Debug == 1) {
  101.                 print("County: $cleancounty Call: $cleancall Address: $cleanlocation Units: $cleanunits Agency: $cleanagency Station: $cleanstation GUID $cleanguid\n");
  102.             } //$Print_Debug == 1
  103.             if ($Use_Twitter == 1) {
  104.                 //build a google maps url with an info bubble
  105.                 $map = 'http://maps.google.com/maps?z=12&q=' . $llatt . "+" . $llonn . "+(" . $cleancall . "," . $cleanlocation . ", Units: " . $cleanunits . ")";
  106.                
  107.                 //change the google map url to a tiny url (actually is.gd ur because it's smallerl) and change the spaces in the $map url to %20 so there is no bad http requests
  108.                 $mapfixed = rawurlencode($map);
  109.                 $tiny_url = file_get_contents("http://is.gd/api.php?longurl=" . $mapfixed);
  110.                
  111.                 //put the parts together for the twitter status line
  112.                 $status = $cleancall . " | " . $cleanlocation . " | Units: " . $cleanunits . " | " . $tiny_url; //assemble status for twitter
  113.                
  114.                 if ($cleancounty == "W") //if washington county, then post to washco_firemed
  115.                     {
  116.                     $curl_handle = curl_init();
  117.                     curl_setopt($curl_handle, CURLOPT_URL, "$twitter_url");
  118.                     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  119.                     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  120.                     curl_setopt($curl_handle, CURLOPT_POST, 1);
  121.                     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$status");
  122.                     curl_setopt($curl_handle, CURLOPT_USERPWD, "$uname:$pwd");
  123.                     $buffer = curl_exec($curl_handle);
  124.                     curl_close($curl_handle);
  125.                     if (empty($buffer)) {
  126.                         echo '<br/>Fail-WC';
  127.                     } //empty($buffer)
  128.                     else {
  129.                         echo $status;
  130.                     }
  131.                 } //$cleancounty == "W"
  132.                 elseif ($cleancounty == "C") //if clackamas county, then post to clackco_firemed
  133.                     {
  134.                     $curl_handle = curl_init();
  135.                     curl_setopt($curl_handle, CURLOPT_URL, "$twitter_url");
  136.                     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  137.                     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  138.                     curl_setopt($curl_handle, CURLOPT_POST, 1);
  139.                     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$status");
  140.                     curl_setopt($curl_handle, CURLOPT_USERPWD, "$unamec:$pwd");
  141.                     $buffer = curl_exec($curl_handle);
  142.                     curl_close($curl_handle);
  143.                     if (empty($buffer)) {
  144.                         echo '<br/>Fail-CC';
  145.                     } //empty($buffer)
  146.                     else {
  147.                         echo $status;
  148.                     }
  149.                 } //$cleancounty == "C"
  150.             } //$Use_Twitter == 1
  151.         } //!empty($cleancounty)
  152.     } //!empty($call)
  153. } //$raw_calls as $call
  154. exit;
  155.  
  156. function Seperate_Calls($HTML, $County)
  157. {
  158.     $dom = new DOMDocument();
  159.     $dom->loadHTML($HTML);
  160.     $xpath = new DOMXPath($dom);
  161.    
  162.     // Switch between WCCCA/CCOM
  163.     if ($County == 0) {
  164.         $tags = $xpath->query('//div[@id="wccca-incidents"]');
  165.     } //$County == 0
  166.     else {
  167.         $tags = $xpath->query('//div[@id="ccom-incidents"]');
  168.     }
  169.    
  170.     // For each call delete all the massive amount of blank spaces and new lines.
  171.     foreach ($tags as $tag) {
  172.         $calls = preg_replace('/\s+/', ' ', $tag->textContent);
  173.     } //$tags as $tag
  174.     // Seperate the calls (since we removed all the \n's)
  175.     $Filtered_Calls = split("Call Type:", $calls);
  176.    
  177.     // Loop though seperated calls and recombind them into a list, then add a new line to them for easier processing.
  178.     foreach ($Filtered_Calls as $call) {
  179.         $Call_dump .= $call . "\n";
  180.     } //$Filtered_Calls as $call
  181.     // Remove any unintentional blank spaces that may occur from this.
  182.     $Call_dump = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $Call_dump);
  183.    
  184.     // Return the calls.
  185.     return ($Call_dump);
  186. }
  187.  
  188. function getgeoloc($address, $county)
  189. {
  190.     $prepAddr = str_replace(' ', '+', $address);
  191.     $prepAddr = str_replace('+and+', '/', $prepAddr);
  192.     $prepAddr = str_replace('S ', '', $prepAddr);
  193.     if ($county == "C") {
  194.         $prepAddr = $prepAddr . "+Clackamas+Oregon";
  195.     } //$county == "C"
  196.     else {
  197.         $prepAddr = $prepAddr . "+Washington+Oregon";
  198.     }
  199.     $geocode   = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false');
  200.     $output    = json_decode($geocode);
  201.     $latitude  = $output->results[0]->geometry->location->lat;
  202.     $longitude = $output->results[0]->geometry->location->lng;
  203.     return ($latitude . "," . $longitude);
  204. }
  205.  
  206. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement