Skip to content

Instantly share code, notes, and snippets.

@ollieparsley
Created July 9, 2013 20:06
Show Gist options
  • Save ollieparsley/5960789 to your computer and use it in GitHub Desktop.
Save ollieparsley/5960789 to your computer and use it in GitHub Desktop.
Get alerts for rightmove properties faster then rightmove themselves. Just set this up as a cron job. Fairly certain they won't be happy. Terms as so boring to read!
<?php
//Search options
$options = array(
"alertEmails" => array(
"email@email.com",
),
"cacheFile" => __DIR__ . "/rightmove.json",
"protocol" => "http",
"host" => "www.rightmove.co.uk",
"port" => 80,
"propertyFor" => "sale",
"params" => array(
"sortType" => "6", //Don't change this. This is ordered by most recent needed for alerts
"partBuyPartRent" => "false", //Don't change this. Required
"searchLocation" => "Reading, Berkshire",
"minPrice" => "150000",
"maxPrice" => "160000",
"minBedrooms" => "1",
"maxBedrooms" => "5",
"displayPropertyType" => "houses", //houses, flats, bungalows, land, commercial, other
"retirement" => "false", //Include retirement properties
"radius" => "1.0" //Miles
),
);
//Load in properties
if (file_exists($options["cacheFile"])) {
$propertiesCacheString = file_get_contents($options["cacheFile"]);
$propertiesCache = json_decode($propertiesCacheString);
} else {
$propertiesCache = new stdClass();
}
//Build the URL
$url = $options["protocol"] . "://" . $options["host"] . ":" . $options["port"] . "/ajax/property-for-" . $options["propertyFor"] . "/map-search.html?" . http_build_query($options["params"]);
//Get the contents
$content = file_get_contents($url);
//Decode json
$json = json_decode($content);
if (!$json || !isset($json->mappedProperties)) {
die("Rightmove response was not json\n");
}
//Types of line break
$breaks = array("\r\n", "\n", "\r");
//Properties
$propertiesToAlert = array();
foreach($json->mappedProperties as $propertyData) {
//Get property details
$propertyUrl = "http://www.rightmove.co.uk/ajax/maps/property-summaries.html?propertyId=" . $propertyData->id;
$propertyContent = mb_convert_encoding(file_get_contents($propertyUrl), "HTML-ENTITIES", "UTF-8");
$propertyJson = json_decode($propertyContent);
//Parse the XML of the first item in the property json
$fixedXmlString = '<temp>' . html_entity_decode(utf8_encode(str_replace('<li class="regular', '<div class="regular', $propertyJson[0]))) . '</div></temp>';
$xml = simplexml_load_string($fixedXmlString);
//Property details node
$propertyDetailsNode = $xml->div->div->div[1];
//Get details
$propertyDetails = (object)array(
"id" => (string)$propertyData->id,
"link" => "http://www.rightmove.co.uk/property-for-" . $options["propertyFor"] . "/property-" . $propertyData->id . ".html",
"price" => (string)$propertyDetailsNode->p[0],
"description" => (string)$propertyDetailsNode->p[1]->span,
"address" => str_replace($breaks, " ", $propertyDetailsNode->h2),
"thumbnail" => (string)$xml->div->div->div[0]->div->div->a->img["src"],
);
//See if this property has been alerted before and the price is the same?
if (isset($propertiesCache->{$propertyDetails->id}) === false || $propertiesCache->{$propertyDetails->id}->price !== $propertyDetails->price) {
//Add old price if required
if (isset($propertiesCache->{$propertyDetails->id})) {
$propertyDetails->oldPrice = $propertiesCache->{$propertyDetails->id}->price;
}
//Add to alert list
$propertiesToAlert[] = $propertyDetails;
//Add the property to the cache
$propertiesCache->{$propertyDetails->id} = $propertyDetails;
}
}
//Alert Count
$alertCount = count($propertiesToAlert);
//Do we have properties to alert?
if ($alertCount) {
//Set the subject
$subject = $alertCount . ' new properties on Rightmove';
//Headers
$headers = "From: email@email.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//Create the message
$message = '<html><p><body><strong>' . $alertCount . ' new properties have been listed</strong></p>';
$message .= '<table style="border-color: #666;" cellpadding="10">';
foreach($propertiesToAlert as $property) {
$message .= "<tr style='background: #eee;'>\n";
$message .= "<td><a href=\"" . $property->link . "\"><img src=\"" . $property->thumbnail . "\"</td>\n";
$message .= "<td><strong>Price:</strong> " . $property->price . "<br />\n";
if (isset($property->oldPrice)) {
$message .= "<strong>Old price:</strong> " . $property->oldPrice . "<br />\n";
}
$message .= "<strong>Address:</strong> " . $property->address . "<br />\n";
$message .= "<strong>Description:</strong> " . $property->description . "<br />\n";
$message .= "<a href=\"" . $property->link . "\">View property details</a></td>\n";
$message .= "</tr>\n\n";
}
$message .= "</table><p>Thanks<br />Rightmove super-bot</p></body></html>";
//Send to each email alert
foreach($options["alertEmails"] as $to) {
mail($to, $subject, $message, $headers);
}
//Update the cache
file_put_contents($options["cacheFile"], json_encode($propertiesCache, JSON_PRETTY_PRINT));
}
@JFCaBa
Copy link

JFCaBa commented Sep 25, 2016

Unfortunately not working now....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment