Skip to content

Instantly share code, notes, and snippets.

@alloy-d
Created March 13, 2012 23:05
Show Gist options
  • Save alloy-d/2032485 to your computer and use it in GitHub Desktop.
Save alloy-d/2032485 to your computer and use it in GitHub Desktop.
.js script to check ghosting status of a Craigslist post
var MONTH_NAMES = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
var clMonthNameToInt = function (name) {
// Most of these are guesses.
return MONTH_NAMES.indexOf(name);
};
var intToClMonthName = function (int) {
return MONTH_NAMES[int];
};
var nextIndexURL = function (indexURL) {
var matcher = /index(\d{3,})\.html/,
match;
if (match = indexURL.match(matcher)) {
return indexURL.replace(matcher, "index" + (parseInt(match[1])+100) + ".html");
} else {
return indexURL + "index100.html";
}
};
var checkForListing = function (indexURL, postingURL, postedTime, dateSeenOnce) {
console.log("Checking " + indexURL);
if (!dateSeenOnce) { dateSeenOnce = false; }
$.get(indexURL, function (content) {
var links = $("a[href='" + postingURL + "']", content);
var updatedTimeText = $("#messages", content).siblings("td").first().text();
var updatedTimeMatcher = /[A-Za-z]{3},\s+(\d{1,2})\s+([A-Za-z]{3})\s+(\d{2}):(\d{2})/;
var updatedTime, match, minUpdatedTime, matchingDates, dateString;
if (match = updatedTimeText.match(updatedTimeMatcher)) {
updatedTime = new Date((new Date()).getFullYear(),
clMonthNameToInt(match[2]),
match[1],
match[3],
match[4]);
} else {
throw "Couldn't get the time of the last index update!";
}
minUpdatedTime = new Date(postedTime.getFullYear(),
postedTime.getMonth(),
postedTime.getDate(),
postedTime.getHours(),
postedTime.getMinutes() + 20);
if (updatedTime.getTime() < minUpdatedTime.getTime()) {
$("#ghosting-status").text(" (Too soon to check ghosting)");
title[0].style.color = 'gray';
return;
}
dateString = intToClMonthName(postedTime.getMonth()) + " ";
dateString += ((postedTime.getDate() < 10) ? "0" : "") + postedTime.getDate();
matchingDates = $.grep($("h4", content), function (dateHeader, i) {
return $(dateHeader).text().indexOf(dateString) >= 0;
});
console.log(matchingDates.length + " matching dates for " + dateString);
if (matchingDates.length > 0) {
links = $("a[href='" + postingURL + "']", content);
if (links.length > 0) {
$("#ghosting-status").text("\u2713");
title[0].style.color = "green";
} else {
checkForListing(nextIndexURL(indexURL), postingURL, postedTime, true);
}
} else if (dateSeenOnce) {
$("#ghosting-status").text(" (Ghosted)");
title[0].style.color = 'red';
} else {
checkForListing(nextIndexURL(indexURL), postingURL, postedTime, true);
}
});
}
if (document.body.className === "posting") {
var postingURL = document.location.href;
var sectionURL = $(".bchead").children("a").last().attr("href");
var title = $("h2").first();
var postedTimeText = $("h2").siblings("hr").first()[0].nextSibling.data;
var postedTimeMatcher = /(\d{4})-(\d{2})-(\d{2}),\s+(\d{1,2}):(\d{1,2})(PM|AM)/;
var postedTime, match, hour;
title.append("<span id='ghosting-status' style='color:black;font-style:italic;text-decoration:none;'/>");
if (match = postedTimeText.match(postedTimeMatcher)) {
hour = parseInt(match[4]);
postedTime = new Date(match[1],
parseInt(match[2])-1,
match[3],
hour + ((match[6] === "PM" && hour !== 12) ? 12 : 0),
match[5]);
} else {
throw "Couldn't get the time of posting!";
}
checkForListing(sectionURL, postingURL, postedTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment