Skip to content

Instantly share code, notes, and snippets.

@scripting
Created November 6, 2014 18:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scripting/2199a65a684f89fca9ea to your computer and use it in GitHub Desktop.
Save scripting/2199a65a684f89fca9ea to your computer and use it in GitHub Desktop.
View a Radio3 user's links, in reverse-chronologic order
<html>
<head>
<title>View Linkblog</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://fargo.io/code/bootstrap.css" rel="stylesheet">
<script src="http://fargo.io/code/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://fargo.io/code/fontAwesome/css/font-awesome.min.css"/>
<link href="http://fargo.io/code/ubuntuFont.css" rel="stylesheet" type="text/css">
<script src="http://fargo.io/code/jquery-1.9.1.min.js"></script>
<script>
//utility routines
function dateYesterday (d) {
return (new Date (new Date (d) - (24 * 60 * 60 * 1000)));
}
function dayGreaterThanOrEqual (d1, d2) {
d1 = new Date (d1);
d1.setHours (0);
d1.setMinutes (0);
d1.setSeconds (0);
d2 = new Date (d2);
d2.setHours (0);
d2.setMinutes (0);
d2.setSeconds (0);
return (d1 >= d2);
}
function padWithZeros (num, ctplaces) {
var s = num.toString ();
while (s.length < ctplaces) {
s = "0" + s;
}
return (s);
}
function filledString (ch, ct) {
var s = "";
for (var i = 0; i < ct; i++) {
s += ch;
}
return (s);
}
function trimLeading (s, ch) {
while (s.charAt (0) === ch) {
s = s.substr (1);
}
return (s);
}
function beginsWith (s, possibleBeginning, flUnicase) {
if (s.length == 0) { //1/1/14 by DW
return (false);
}
if (flUnicase == undefined) {
flUnicase = true;
}
if (flUnicase) {
for (var i = 0; i < possibleBeginning.length; i++) {
if (stringLower (s [i]) != stringLower (possibleBeginning [i])) {
return (false);
}
}
}
else {
for (var i = 0; i < possibleBeginning.length; i++) {
if (s [i] != possibleBeginning [i]) {
return (false);
}
}
}
return (true);
}
function stringLower (s) {
return (s.toLowerCase ());
}
function stringDelete (s, ix, ct) {
var start = ix - 1;
var end = (ix + ct) - 1;
var s1 = s.substr (0, start);
var s2 = s.substr (end);
return (s1 + s2);
}
function secondsSince (when) {
var now = new Date ();
when = new Date (when);
return ((now - when) / 1000);
}
function urlSplitter (url) {
var pattern = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result = pattern.exec (url);
if (result == null) {
result = [];
result [5] = url;
}
var splitUrl = {
scheme: result [1],
host: result [3],
port: result [4],
path: result [5],
query: result [6],
hash: result [7]
};
return (splitUrl);
}
function readHttpFile (url, callback) {
var jxhr = $.ajax ({
url: url,
dataType: "text" ,
timeout: 30000
})
.success (function (data, status) {
callback (data);
})
.error (function (status) {
console.log ("readHttpFile: url == " + url + ", error == " + status.statusText + ".");
callback (undefined);
});
}
function getDatePath (theDate, flLastSeparator) {
if (theDate == undefined) {
theDate = new Date ();
}
else {
theDate = new Date (theDate); //8/12/14 by DW -- make sure it's a date type
}
if (flLastSeparator == undefined) {
flLastSeparator = true;
}
var month = padWithZeros (theDate.getMonth () + 1, 2);
var day = padWithZeros (theDate.getDate (), 2);
var year = theDate.getFullYear ();
if (flLastSeparator) {
return (year + "/" + month + "/" + day + "/");
}
else {
return (year + "/" + month + "/" + day);
}
}
function viewLinkblog (idDiv, twitterScreenname, whenLinkblogStart, baseUrl, callback) {
var ctDaysOnPage = 25, daysTable;
function loadOneDay (theDay, callback) {
var urlFolder = baseUrl + twitterScreenname + "/";
var url = urlFolder + getDatePath (theDay) + "history.json", whenReadStart = new Date ();
console.log ("loadOneDay: " + theDay);
readHttpFile (url, function (jsontext) {
if (callback != undefined) {
try {
var jstruct = JSON.parse (jsontext);
callback (jstruct);
}
catch (err) {
callback ();
}
}
});
}
function loadDaysTable (callback) {
var whenBlogStart = dateYesterday (whenLinkblogStart), whenstart = new Date ();
function loadOne (theDay) {
loadOneDay (theDay, function (jstruct) {
var yesterday = dateYesterday (theDay);
if (jstruct != undefined) {
daysTable.unshift ({
when: theDay,
jstruct: jstruct
});
}
if ((daysTable.length < ctDaysOnPage) && (dayGreaterThanOrEqual (yesterday, whenBlogStart))) {
loadOne (yesterday);
}
else {
if (callback != undefined) { //10/24/14 by DW
callback ();
}
console.log ("loadDaysTable: " + secondsSince (whenstart) + " secs.");
}
});
}
daysTable = new Array ();
loadOne (new Date ());
}
function addDay (jstruct) {
var htmltext = "", indentlevel = 0;
var datestring = new Date (jstruct.when).toLocaleDateString ();
function add (s) {
htmltext += filledString ("\t", indentlevel) + s + "\n";
}
add ("<div class=\"divLinkblogDayTitle\">" + datestring + "</div>");
add ("<div class=\"divLinkblogDay\">"); indentlevel++;
for (var i = 0; i < jstruct.dayHistory.length; i++) {
try {
var item = jstruct.dayHistory [i], linktext = "", icon = "";
//set linktext, icon
if ((item.link != undefined) && (item.link.length > 0)) {
var splitUrl = urlSplitter (trimLeading (item.link, " ")); //10/15/14 by DW -- remove leading blanks
var host = splitUrl.host;
if (beginsWith (host, "www.")) {
host = stringDelete (host, 1, 4);
}
linktext = " <a class=\"aHost\" href=\"" + item.link + "\" target=\"blank\">" + host + "</a>";
}
add ("<p>" + icon + item.text + linktext + "</p>");
}
catch (error) {
console.log ("appendDay: error == " + error + " while adding item == " + item.text);
}
}
add ("</div>"); indentlevel--;
$("#idLinkblogDays").append (htmltext);
}
loadDaysTable (function () {
$("#idLinkblogDays").html ("");
for (var i = daysTable.length - 1; i >= 0; i--) {
addDay (daysTable [i].jstruct);
}
});
}
function startup () {
viewLinkblog ("idLinkblogDays", "davewiner", new Date ("9/1/2014"), "http://radio3.io/users/", function () {
});
}
</script>
<style>
body {
font-family: Ubuntu;
font-size: 18px;
background-color: whitesmoke;
}
.divPageBody {
width: 50%;
margin-top: 80px;
margin-bottom: 400px;
margin-left: auto;
margin-right: auto;
}
.divLinkblogDayTitle {
font-family: "Ubuntu";
font-size: 22px;
font-weight: bold;
margin-bottom: 10px;
}
.divLinkblogDay {
font-family: "Ubuntu";
font-size: 16px;
margin-bottom: 40px;
}
</style>
</head>
<body>
<div class="divPageBody">
<div id="idLinkblogDays">
</div>
</div>
<script>
$(document).ready (function () {
startup ();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment