changed the Digg API data format from XML to JSON.

git-svn-id: file:///tmp/snv/trunk@4 12951d8a-c33d-4b7c-b961-822215c816e1
master
Abhinav Sarkar 2008-11-20 09:18:46 +00:00
parent 5aaa7b03f8
commit a72ed0292c
2 changed files with 247 additions and 272 deletions

7
chrome/content/TODO.txt Normal file
View File

@ -0,0 +1,7 @@
move to JSON data format
XBL -> Templates
format JS/refactor JS/Use FUEL
caching/db
threading
new UI
JQuery

View File

@ -76,138 +76,114 @@ function handleError(e) {
function populateMenu(e) { function populateMenu(e) {
XHR = e.target; XHR = e.target;
//myDump(XHR.responseText); data = decodeJson(XHR.responseText);
data = XHR.responseXML; topics = data.topics;
topics = $xp(data, "/topics/topic"); containers = new Array();
while ($ei('topicPopup').firstChild) { while ($ei('topicPopup').firstChild) {
$ei('topicPopup').removeChild($ei('topicPopup').firstChild); $ei('topicPopup').removeChild($ei('topicPopup').firstChild);
} }
for (i=0; i<topics.length; i++) { for (i=0; i<topics.length; i++) {
topic = topics[i];
menu = document.createElement('menu'); menu = document.createElement('menu');
menu.setAttribute('id', $a(topics[i], 'short_name') + 'Menu'); menu.setAttribute('id', topic.short_name + 'Menu');
menu.setAttribute('label', $a(topics[i], 'name')); menu.setAttribute('label', topic.name);
menu.setAttribute('accesskey', $a(topics[i], 'name').substring(0, 1)); menu.setAttribute('accesskey', topic.name.charAt(0));
menupopup = document.createElement('menupopup'); menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', $a(topics[i], 'short_name') + 'Popup'); menupopup.setAttribute('id', topic.short_name + 'Popup');
['All', 'Popular', 'Upcoming'].forEach(
function (label) {
menuitem = document.createElement('menuitem'); menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'All'); menuitem.setAttribute('label', label);
menuitem.setAttribute('value', '/topic/' + $a(topics[i], 'short_name') + '/all'); menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase());
menuitem.setAttribute('accesskey', 'A'); menuitem.setAttribute('accesskey', label.charAt(0));
menuitem.setAttribute('oncommand', "setEndPoint(this.value)"); menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem); menupopup.appendChild(menuitem);
}
menuitem = document.createElement('menuitem'); );
menuitem.setAttribute('label', 'Popular');
menuitem.setAttribute('value', '/topic/' + $a(topics[i], 'short_name') + '/popular');
menuitem.setAttribute('accesskey', 'P');
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem);
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'Upcoming');
menuitem.setAttribute('value', '/topic/' + $a(topics[i], 'short_name') + '/upcoming');
menuitem.setAttribute('accesskey', 'U');
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem);
menu.appendChild(menupopup); menu.appendChild(menupopup);
//alert($a(topics[i], 'name'));
$ei('topicPopup').appendChild(menu); $ei('topicPopup').appendChild(menu);
containers.push(topic.container);
} }
containers = $xp(data, "/topics/topic/container");
while ($ei('containerPopup').firstChild) { while ($ei('containerPopup').firstChild) {
$ei('containerPopup').removeChild($ei('containerPopup').firstChild); $ei('containerPopup').removeChild($ei('containerPopup').firstChild);
} }
filteredContainers = {name: [], short_name: []}; filteredContainers = {name: [], short_name: []};
for (i=0; i<containers.length; i++) { for (i=0; i<containers.length; i++) {
name = $a(containers[i], 'name'); name = containers[i].name;
short_name = $a(containers[i], 'short_name'); short_name = containers[i].short_name;
if (filteredContainers.name.indexOf(name) == -1) { if (filteredContainers.name.indexOf(name) == -1) {
filteredContainers.name.push(name); filteredContainers.name.push(name);
filteredContainers.short_name.push(short_name); filteredContainers.short_name.push(short_name);
} }
} }
//myDump(uneval(filteredContainers));
for (i=0; i<filteredContainers.name.length; i++) { for (i=0; i<filteredContainers.name.length; i++) {
menu = document.createElement('menu'); menu = document.createElement('menu');
menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu'); menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
menu.setAttribute('label', filteredContainers.name[i]); menu.setAttribute('label', filteredContainers.name[i]);
menu.setAttribute('accesskey', filteredContainers.name[i].substring(0, 1)); menu.setAttribute('accesskey', filteredContainers.name[i].charAt(0));
menupopup = document.createElement('menupopup'); menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup'); menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
['All', 'Popular', 'Upcoming'].forEach(
function (label) {
menuitem = document.createElement('menuitem'); menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'All'); menuitem.setAttribute('label', label);
menuitem.setAttribute('value', '/container/' + filteredContainers.short_name[i] + '/all'); menuitem.setAttribute('value', '/container/' +
menuitem.setAttribute('accesskey', 'A'); filteredContainers.short_name[i] + '/' + label.toLowerCase());
menuitem.setAttribute('oncommand', "setEndPoint(this.value)"); menuitem.setAttribute('accesskey', label.charAt(0));
menupopup.appendChild(menuitem);
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'Popular');
menuitem.setAttribute('value', '/container/' + filteredContainers.short_name[i] + '/popular');
menuitem.setAttribute('accesskey', 'P');
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem);
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'Upcoming');
menuitem.setAttribute('value', '/container/' + filteredContainers.short_name[i] + '/upcoming');
menuitem.setAttribute('accesskey', 'U');
menuitem.setAttribute('oncommand', "setEndPoint(this.value)"); menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem); menupopup.appendChild(menuitem);
}
);
menu.appendChild(menupopup); menu.appendChild(menupopup);
//alert($a(topics[i], 'name'));
$ei('containerPopup').appendChild(menu); $ei('containerPopup').appendChild(menu);
} }
$ei('diggIndicator').style.display = 'none'; $ei('diggIndicator').style.display = 'none';
} }
function populateStoryList(e) { function populateStoryList(e) {
//alert("inside populateStoryList");
XHR = e.target; XHR = e.target;
//alert(XHR.responseText); //Application.console.log(XHR.responseText);
data = XHR.responseXML; data = decodeJson(XHR.responseText);
stories = $xp(data, "/stories/story"); stories = data.stories;
//alert(stories.length);
//dumpProperties(titles[0]);
//selectedItem = $ei('storyList').selectedItem;
while ($ei('storyList').firstChild) { while ($ei('storyList').firstChild) {
$ei('storyList').removeChild($ei('storyList').firstChild); $ei('storyList').removeChild($ei('storyList').firstChild);
} }
var newStories = new Array(); var newStories = new Array();
Application.console.log(stories.length);
for (i=0; i<stories.length; i++) { for (i=0; i<stories.length; i++) {
//alert(stories[i]); story = stories[i];
title = $et('title', stories[i]).textContent; newStories.push(story.id);
id = $a(stories[i], 'id'); //Application.console.log(i + " " + story.title + " " + story.id);
newStories.push(id);
//alert(title + " " + id);
listitem = $ei('storyList').appendChild(document.createElement('richlistitem')); listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
listitem.id = "story_" + id; listitem.id = "story_" + story.id;
htmllistitem = listitem.appendChild(document.createElementNS("http://www.w3.org/1999/xhtml","html:div"));
htmllistitem = listitem.appendChild(
document.createElementNS("http://www.w3.org/1999/xhtml","html:div"));
htmllistitem.innerHTML = $ei('storyTitleFormat').innerHTML; htmllistitem.innerHTML = $ei('storyTitleFormat').innerHTML;
storyTitle = listitem.getElementsByAttribute('class', 'storyTitle')[0]; storyTitle = listitem.getElementsByAttribute('class', 'storyTitle')[0];
storyTitle.appendChild(document.createTextNode(title)); storyTitle.appendChild(document.createTextNode(story.title));
htmllistitem.innerHTML += $ei('storyDetailsFormat').innerHTML; htmllistitem.innerHTML += $ei('storyDetailsFormat').innerHTML;
storyNew= listitem.getElementsByAttribute('class', 'storyNew')[0]; storyNew= listitem.getElementsByAttribute('class', 'storyNew')[0];
if (currentStories.indexOf(id) != -1) { if (currentStories.indexOf(story.id) != -1) {
storyNew.style.textDecoration = "line-through"; storyNew.style.textDecoration = "line-through";
storyNew.style.backgroundColor = "black"; storyNew.style.backgroundColor = "black";
storyNew.style.color = "white"; storyNew.style.color = "white";
@ -215,16 +191,15 @@ function populateStoryList(e) {
} }
} }
currentStories = newStories; currentStories = newStories;
//$ei('diggIndicator').style.display = 'none';
//Adaptive update interval code START //Adaptive update interval code START
sum = 0; sum = 0;
weights = [0.4, 0.3, 0.2, 0.1] weights = [0.4, 0.3, 0.2, 0.1]
for (i=0; i<4; i++) { for (i=0; i<4; i++) {
if ($a(stories[0], 'promote_date') != null) if (stories[0].promote_date != null)
diff = parseInt($a(stories[i], 'promote_date')) - parseInt($a(stories[i+1], 'promote_date')); diff = stories[i].promote_date - stories[i+1].promote_date;
else else
diff = parseInt($a(stories[i], 'submit_date')) - parseInt($a(stories[i+1], 'submit_date')); diff = stories[i].submit_date - stories[i+1].submit_date;
sum += weights[i]*diff; sum += weights[i]*diff;
} }
@ -259,7 +234,6 @@ function populateStoryList(e) {
//$ei("diggIndicator").setAttribute('value', 100); //$ei("diggIndicator").setAttribute('value', 100);
//psi = window.setInterval(showProgress, Math.round(timeout/100)); //psi = window.setInterval(showProgress, Math.round(timeout/100));
//window.setTimeout("window.clearInterval(psi)", timeout); //window.setTimeout("window.clearInterval(psi)", timeout);
} }
//function showProgress() { //function showProgress() {
@ -273,17 +247,16 @@ function populateStoryList(e) {
function populateDescription(e) { function populateDescription(e) {
XHR = e.target; XHR = e.target;
myDump(XHR.responseText); //Application.console.log(XHR.responseText);
data = XHR.responseXML; data = decodeJson(XHR.responseText);
story = $xp(data, "/stories/story")[0]; story = data.stories[0];
//alert(story);
now = new Date(); now = new Date();
if ($a(story, 'promote_date') != null) if (story.promote_date != null)
then = new Date(parseInt($a(story, 'promote_date').escapeEntities())*1000); then = new Date(story.promote_date*1000);
else else
then = new Date(parseInt($a(story, 'submit_date').escapeEntities())*1000); then = new Date(story.submit_date*1000);
diff = now - then; diff = Math.max(now - then, 0)
if (diff < 0) diff = 0;
hr = Math.floor(diff/(1000*3600)); hr = Math.floor(diff/(1000*3600));
min = Math.floor(diff/(1000*60)) - 60*hr; min = Math.floor(diff/(1000*60)) - 60*hr;
@ -291,10 +264,11 @@ function populateDescription(e) {
listitems = document.getElementsByTagName('richlistitem'); listitems = document.getElementsByTagName('richlistitem');
for (i=0; i<listitems.length; i++) { for (i=0; i<listitems.length; i++) {
listitems[i].getElementsByAttribute('class', 'storyDetails')[0].style.display = 'none'; listitems[i].getElementsByAttribute('class', 'storyDetails')[0].style.display = 'none';
listitems[i].getElementsByAttribute('class', 'storyCEIcon')[0].src = "chrome://diggsidebar/content/image/up.jpg"; listitems[i].getElementsByAttribute('class', 'storyCEIcon')[0].src =
"chrome://diggsidebar/content/image/up.jpg";
} }
storyListItem = $ei('story_'+ $a(story, 'id')) storyListItem = $ei('story_'+ story.id)
storyDetails = storyListItem.getElementsByAttribute('class', 'storyDetails')[0]; storyDetails = storyListItem.getElementsByAttribute('class', 'storyDetails')[0];
storyDate = storyListItem.getElementsByAttribute('class', 'storyDate')[0]; storyDate = storyListItem.getElementsByAttribute('class', 'storyDate')[0];
storyStatus = storyListItem.getElementsByAttribute('class', 'storyStatus')[0]; storyStatus = storyListItem.getElementsByAttribute('class', 'storyStatus')[0];
@ -316,17 +290,16 @@ function populateDescription(e) {
(min + " mins ago") : (min + " mins ago") :
((hr == 0) && (min == 0)) ? ((hr == 0) && (min == 0)) ?
"just now" : ""; "just now" : "";
storyStatus.innerHTML = $a(story, 'status').escapeEntities(); storyStatus.innerHTML = story.status.escapeEntities();
//alert($a($et('container', story), 'name').escapeEntities()); storyContainer.innerHTML = story.container.name.escapeEntities();
storyContainer.innerHTML = $a($et('container', story), 'name').escapeEntities(); storyTopic.innerHTML = story.topic.name.escapeEntities();
storyTopic.innerHTML = $a($et('topic', story), 'name').escapeEntities(); storyUserName.href = "http://digg.com/users/" + story.user.name;
storyUserName.href = "http://digg.com/users/" + $a($et('user', story), 'name').escapeEntities(); storyUserName.innerHTML = story.user.name.escapeEntities();
storyUserName.innerHTML = $a($et('user', story), 'name').escapeEntities(); storyDiggs.innerHTML = story.diggs;
storyDiggs.innerHTML = $a(story, 'diggs').escapeEntities(); storyComments.innerHTML = story.comments;
storyComments.innerHTML = $a(story, 'comments').escapeEntities(); storyDesc.innerHTML = story.description.escapeEntities();
storyDesc.innerHTML = $et('description', story).textContent.escapeEntities(); storyLink.href = story.link;
storyLink.href = $a(story, 'link'); storyHref.href = story.href;
storyHref.href = $a(story, 'href');
storyCEIcon.src = "chrome://diggsidebar/content/image/down.jpg"; storyCEIcon.src = "chrome://diggsidebar/content/image/down.jpg";
storyRead.style.textDecoration = "line-through"; storyRead.style.textDecoration = "line-through";
@ -344,31 +317,28 @@ function populateDescription(e) {
} }
function getStories() { function getStories() {
//alert("inside getStories");
var prefs = Components.classes["@mozilla.org/preferences-service;1"]. var prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch); getService(Components.interfaces.nsIPrefBranch);
ep = prefs.getCharPref("extensions.diggsidebar.endpoint") || ''; ep = prefs.getCharPref("extensions.diggsidebar.endpoint") || '';
//alert(ep);
$ei('diggEndPoint').value = "digg" + ep; $ei('diggEndPoint').value = "digg" + ep;
//ep =''; fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') + "?count=30" + "?count=30" + "&type=json" +
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateStoryList); populateStoryList);
//gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval")); //gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval"));
} }
function getDescription(storyId) { function getDescription(storyId) {
myDump(storyId);
if (storyId == null) return; if (storyId == null) return;
fetchData("http://services.digg.com/story/" + storyId + fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
"?appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateDescription); populateDescription);
} }
function createMenu() { function createMenu() {
fetchData("http://services.digg.com/topics" + fetchData("http://services.digg.com/topics" + "?type=json" +
"?appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"), "&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateMenu); populateMenu);
} }
@ -415,13 +385,11 @@ function togglePlayPause() {
} }
} }
function decodeJson(string) {
function myDump(aMessage) { var json = Components.classes["@mozilla.org/dom/json;1"]
var consoleService = Components.classes["@mozilla.org/consoleservice;1"] .createInstance(Components.interfaces.nsIJSON);
.getService(Components.interfaces.nsIConsoleService); return json.decode(string);
consoleService.logStringMessage("Diggsidebar: " + aMessage); };
}
/*TODO /*TODO
preferences preferences