Last active
February 27, 2017 22:24
-
-
Save engram-design/645ab1365b3e649a0c26ea91f3e92113 to your computer and use it in GitHub Desktop.
Property Feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get all XML files in directory | |
$files = glob('data/*xml'); | |
$jsons = array(); | |
if (isset($_GET['start']) && isset($_GET['end'])) { | |
$start = $_GET['start']; | |
$end = $_GET['end']; | |
} else { | |
$start = '-2 days'; | |
$end = 'now'; | |
} | |
// If we're looking for a specific ID, check for that | |
$uniqueID = null; | |
if (isset($_GET['id'])) { | |
$uniqueID = $_GET['id']; | |
} | |
if (is_array($files)) { | |
foreach ($files as $filename) { | |
// Only output properties for the last few days - otherwise way too many to process | |
$start_ts = strtotime($start); | |
$end_ts = strtotime($end); | |
$user_ts = strtotime(substr($filename, 13, 10)); | |
if (($user_ts >= $start_ts) && ($user_ts <= $end_ts)) { | |
$xml_file = file_get_contents($filename, FILE_TEXT); | |
$xml = simplexml_load_string($xml_file, null, LIBXML_NOCDATA); | |
$json = json_encode($xml); | |
$arrays = json_decode($json, TRUE); | |
// Strip off extra rubbish in the XML | |
unset($arrays['@attributes']); | |
foreach ($arrays as $type => $array) { | |
// Protect again blank data | |
if (is_string($array)) { | |
continue; | |
} | |
// Check to see if this is a flush of data (multiple listings) | |
if (array_key_exists(0, $array)) { | |
foreach ($array as $index => $item) { | |
$feedItem = processFeed($type, $item); | |
if ($feedItem) { | |
$jsons[$item['uniqueID']] = $feedItem; | |
} | |
} | |
} else { | |
$feedItem = processFeed($type, $array); | |
if ($feedItem) { | |
$jsons[$array['uniqueID']] = $feedItem; | |
} | |
} | |
} | |
} | |
} | |
} | |
// Make sure to remove the unique id key for Feed Me (can't handle non-identical keys) | |
$jsons = array_values($jsons); | |
function processFeed($type, $array) { | |
global $uniqueID; | |
// If we're looking for a specific ID, check for that | |
if ($uniqueID && $array['uniqueID'] != $uniqueID) { | |
return false; | |
} | |
$array['type'] = ucwords($type); | |
$array['type2'] = ucwords($type); | |
// Combine Street Number and Street for VZ Address | |
$array['address']['street'] = $array['address']['streetNumber'] . ' ' . $array['address']['street']; | |
$array['address']['display'] = $array['address']['@attributes']['display']; | |
$array['address']['display'] = ($array['address']['display'] == 'yes') ? true : false; | |
// Don't uppercase the city or state - uncool | |
$array['address']['suburb'] = ucwords(strtolower($array['address']['suburb'])); | |
$array['address']['state'] = ucwords(strtolower($array['address']['state'])); | |
// Duplicate Street Address and Suburb for additional fields we have to have, because Feed Me can't support | |
// Duplication handling for third-party fields yet... We also use suburb for Categories | |
$array['address']['street2'] = $array['address']['street']; | |
$array['address']['suburb2'] = $array['address']['suburb']; | |
// Duplicate Long/Lat | |
$array['Latitude'] = $array['Geocode']['Latitude']; | |
$array['Longitude'] = $array['Geocode']['Longitude']; | |
// Normalise fields across Rental/Buying | |
$array['dateAvailable'] = ($array['dateAvailable']) ? $array['dateAvailable'] : ''; | |
$array['rent'] = ($array['rent']) ? $array['rent'] : ''; | |
$array['bond'] = ($array['bond']) ? $array['bond'] : ''; | |
if (isset($array['inspectionTimes']['inspection'])) { | |
// Inspection time can either be an array or a string | |
$array['inspectionTimes'] = (array)$array['inspectionTimes']['inspection']; | |
} else { | |
$array['inspectionTimes'] = array(); | |
} | |
// Normalise listing agent | |
if (isset($array['listingAgent'])) { | |
if (!isset($array['listingAgent'][0])) { | |
$array['listingAgent'] = array($array['listingAgent']); | |
} | |
} | |
// Organise images a bit better - not so nested! | |
$images = array(); | |
if (is_array($array['images']['img'])) { | |
foreach ($array['images']['img'] as $key => $img) { | |
if ($img['@attributes']['url']) { | |
// for the moment, but use the filename | |
//$images[] = $img['@attributes']['url']; | |
$images[] = basename($img['@attributes']['url']); | |
} | |
} | |
} | |
$array['images'] = $images; | |
// Do the same for Floorplans | |
$floorplans = array(); | |
if ($array['objects']['floorplan']['@attributes']['url']) { | |
$floorplans[] = basename($array['objects']['floorplan']['@attributes']['url']); | |
} else { | |
$floorplans[] = ''; | |
} | |
$array['objects'] = $floorplans; | |
// Add a Category if it doesn't exist | |
if (!isset($array['category'])) { | |
$array['category']['@attributes']['name'] = ucwords($type); | |
} | |
// And YouTube | |
if (isset($array['externalLink']['@attributes']['href'])) { | |
$array['externalLink'] = $array['externalLink']['@attributes']['href']; | |
} else { | |
$array['externalLink'] = ''; | |
} | |
if (isset($array['videoLink']['@attributes']['href'])) { | |
$array['videoLink'] = $array['videoLink']['@attributes']['href']; | |
} else { | |
$array['videoLink'] = ''; | |
} | |
if (isset($array['floorplan_3d'])) { | |
$array['floorplan_3d'] = $array['floorplan_3d']; | |
} else { | |
$array['floorplan_3d'] = ''; | |
} | |
// Check for an empty array for this node | |
if (is_array($array['floorplan_3d'])) { | |
if (count($array['floorplan_3d']) == 0) { | |
$array['floorplan_3d'] = ''; | |
} | |
} | |
// Remove (for now) listings without an address | |
if (!ctype_space($array['address']['street'])) { | |
return $array; | |
} | |
} | |
echo json_encode($jsons); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get all XML files in directory | |
$files = glob('data/*xml'); | |
$imgArray = array(); | |
if (isset($_GET['start']) && isset($_GET['end'])) { | |
$start = $_GET['start']; | |
$end = $_GET['end']; | |
} else { | |
$start = '-2 days'; | |
$end = 'now'; | |
} | |
// If we're looking for a specific ID, check for that | |
$uniqueID = null; | |
if (isset($_GET['id'])) { | |
$uniqueID = $_GET['id']; | |
} | |
if (is_array($files)) { | |
foreach ($files as $filename) { | |
// Only output properties for the last few days - otherwise way too many to process | |
$start_ts = strtotime($start); | |
$end_ts = strtotime($end); | |
$user_ts = strtotime(substr($filename, 13, 10)); | |
if (($user_ts >= $start_ts) && ($user_ts <= $end_ts)) { | |
$xml_file = file_get_contents($filename, FILE_TEXT); | |
$xml = simplexml_load_string($xml_file, null, LIBXML_NOCDATA); | |
$json = json_encode($xml); | |
$arrays = json_decode($json, TRUE); | |
// Strip off extra rubbish in the XML | |
unset($arrays['@attributes']); | |
foreach ($arrays as $type => $array) { | |
// Protect again blank data | |
if (is_string($array)) { | |
continue; | |
} | |
// Check to see if this is a flush of data | |
if (array_key_exists(0, $array)) { | |
foreach ($array as $index => $item) { | |
$feedItem = processFeed($type, $item); | |
if ($feedItem) { | |
$imgArray[] = $feedItem; | |
} | |
} | |
} else { | |
$feedItem = processFeed($type, $array); | |
if ($feedItem) { | |
$imgArray[] = $feedItem; | |
} | |
} | |
} | |
} | |
} | |
} | |
function processFeed($type, $array) { | |
$return = array(); | |
$images = array(); | |
if (isset($array['images'])) { | |
if (is_array($array['images']['img'])) { | |
foreach ($array['images']['img'] as $key => $img) { | |
if (isset($img['@attributes']['url']) && $img['@attributes']['url']) { | |
$images[] = $img['@attributes']['url']; | |
} | |
} | |
} | |
} | |
$return[] = $images; | |
// Do the same for Floorplans | |
$floorplans = array(); | |
if (isset($array['objects'])) { | |
if (isset($array['objects']['floorplan']['@attributes']['url']) && $array['objects']['floorplan']['@attributes']['url']) { | |
$floorplans[] = $array['objects']['floorplan']['@attributes']['url']; | |
} | |
} | |
$return[] = $floorplans; | |
return array_merge($images, $floorplans); | |
} | |
echo json_encode($imgArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment