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
révision a72ed0292c
2 fichiers modifiés avec 247 ajouts et 272 suppressions

7
chrome/content/TODO.txt Normal file
Voir le fichier

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

Voir le fichier

@ -28,7 +28,7 @@ function $ec(className, parentNode) {
return elements[i];
}
}
}
//Xpath
function $xp(aNode, aExpr) {
@ -66,7 +66,7 @@ function fetchData(url, handler) {
request.open("GET", url, true);
request.send(null);
//$ei('diggIndicator').style.display = '';
}
function handleError(e) {
@ -75,191 +75,165 @@ function handleError(e) {
}
function populateMenu(e) {
XHR = e.target;
//myDump(XHR.responseText);
data = XHR.responseXML;
topics = $xp(data, "/topics/topic");
while ($ei('topicPopup').firstChild) {
$ei('topicPopup').removeChild($ei('topicPopup').firstChild);
XHR = e.target;
data = decodeJson(XHR.responseText);
topics = data.topics;
containers = new Array();
while ($ei('topicPopup').firstChild) {
$ei('topicPopup').removeChild($ei('topicPopup').firstChild);
}
for (i=0; i<topics.length; i++) {
topic = topics[i];
menu = document.createElement('menu');
menu.setAttribute('id', topic.short_name + 'Menu');
menu.setAttribute('label', topic.name);
menu.setAttribute('accesskey', topic.name.charAt(0));
menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', topic.short_name + 'Popup');
['All', 'Popular', 'Upcoming'].forEach(
function (label) {
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', label);
menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase());
menuitem.setAttribute('accesskey', label.charAt(0));
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
menupopup.appendChild(menuitem);
}
);
menu.appendChild(menupopup);
$ei('topicPopup').appendChild(menu);
containers.push(topic.container);
}
while ($ei('containerPopup').firstChild) {
$ei('containerPopup').removeChild($ei('containerPopup').firstChild);
}
filteredContainers = {name: [], short_name: []};
for (i=0; i<containers.length; i++) {
name = containers[i].name;
short_name = containers[i].short_name;
if (filteredContainers.name.indexOf(name) == -1) {
filteredContainers.name.push(name);
filteredContainers.short_name.push(short_name);
}
for (i=0; i<topics.length; i++) {
menu = document.createElement('menu');
menu.setAttribute('id', $a(topics[i], 'short_name') + 'Menu');
menu.setAttribute('label', $a(topics[i], 'name'));
menu.setAttribute('accesskey', $a(topics[i], 'name').substring(0, 1));
menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', $a(topics[i], 'short_name') + 'Popup');
}
for (i=0; i<filteredContainers.name.length; i++) {
menu = document.createElement('menu');
menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
menu.setAttribute('label', filteredContainers.name[i]);
menu.setAttribute('accesskey', filteredContainers.name[i].charAt(0));
menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
['All', 'Popular', 'Upcoming'].forEach(
function (label) {
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'All');
menuitem.setAttribute('value', '/topic/' + $a(topics[i], 'short_name') + '/all');
menuitem.setAttribute('accesskey', 'A');
menuitem.setAttribute('label', label);
menuitem.setAttribute('value', '/container/' +
filteredContainers.short_name[i] + '/' + label.toLowerCase());
menuitem.setAttribute('accesskey', label.charAt(0));
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
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);
//alert($a(topics[i], 'name'));
$ei('topicPopup').appendChild(menu);
}
containers = $xp(data, "/topics/topic/container");
while ($ei('containerPopup').firstChild) {
$ei('containerPopup').removeChild($ei('containerPopup').firstChild);
}
filteredContainers = {name: [], short_name: []};
for (i=0; i<containers.length; i++) {
name = $a(containers[i], 'name');
short_name = $a(containers[i], 'short_name');
if (filteredContainers.name.indexOf(name) == -1) {
filteredContainers.name.push(name);
filteredContainers.short_name.push(short_name);
}
}
//myDump(uneval(filteredContainers));
for (i=0; i<filteredContainers.name.length; i++) {
menu = document.createElement('menu');
menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
menu.setAttribute('label', filteredContainers.name[i]);
menu.setAttribute('accesskey', filteredContainers.name[i].substring(0, 1));
menupopup = document.createElement('menupopup');
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', 'All');
menuitem.setAttribute('value', '/container/' + filteredContainers.short_name[i] + '/all');
menuitem.setAttribute('accesskey', 'A');
menuitem.setAttribute('oncommand', "setEndPoint(this.value)");
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)");
menupopup.appendChild(menuitem);
menu.appendChild(menupopup);
//alert($a(topics[i], 'name'));
$ei('containerPopup').appendChild(menu);
}
$ei('diggIndicator').style.display = 'none';
}
);
menu.appendChild(menupopup);
$ei('containerPopup').appendChild(menu);
}
$ei('diggIndicator').style.display = 'none';
}
function populateStoryList(e) {
//alert("inside populateStoryList");
XHR = e.target;
//alert(XHR.responseText);
data = XHR.responseXML;
stories = $xp(data, "/stories/story");
//alert(stories.length);
//dumpProperties(titles[0]);
//selectedItem = $ei('storyList').selectedItem;
while ($ei('storyList').firstChild) {
$ei('storyList').removeChild($ei('storyList').firstChild);
}
var newStories = new Array();
for (i=0; i<stories.length; i++) {
//alert(stories[i]);
title = $et('title', stories[i]).textContent;
id = $a(stories[i], 'id');
newStories.push(id);
//alert(title + " " + id);
listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
listitem.id = "story_" + id;
htmllistitem = listitem.appendChild(document.createElementNS("http://www.w3.org/1999/xhtml","html:div"));
htmllistitem.innerHTML = $ei('storyTitleFormat').innerHTML;
storyTitle = listitem.getElementsByAttribute('class', 'storyTitle')[0];
storyTitle.appendChild(document.createTextNode(title));
htmllistitem.innerHTML += $ei('storyDetailsFormat').innerHTML;
storyNew= listitem.getElementsByAttribute('class', 'storyNew')[0];
if (currentStories.indexOf(id) != -1) {
storyNew.style.textDecoration = "line-through";
storyNew.style.backgroundColor = "black";
storyNew.style.color = "white";
storyNew.title = "Old";
}
}
currentStories = newStories;
//$ei('diggIndicator').style.display = 'none';
//Adaptive update interval code START
sum = 0;
weights = [0.4, 0.3, 0.2, 0.1]
for (i=0; i<4; i++) {
if ($a(stories[0], 'promote_date') != null)
diff = parseInt($a(stories[i], 'promote_date')) - parseInt($a(stories[i+1], 'promote_date'));
else
diff = parseInt($a(stories[i], 'submit_date')) - parseInt($a(stories[i+1], 'submit_date'));
sum += weights[i]*diff;
}
updateInterval = Math.round(sum*1000);
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch);
previousUpdateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
prefs.setIntPref("extensions.diggsidebar.updateinterval", updateInterval);
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
//Application.console.log(XHR.responseText);
data = decodeJson(XHR.responseText);
stories = data.stories;
if (previousUpdateInterval == updateInterval)
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", (updateIntervalDecay + 1));
while ($ei('storyList').firstChild) {
$ei('storyList').removeChild($ei('storyList').firstChild);
}
var newStories = new Array();
Application.console.log(stories.length);
for (i=0; i<stories.length; i++) {
story = stories[i];
newStories.push(story.id);
//Application.console.log(i + " " + story.title + " " + story.id);
listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
listitem.id = "story_" + story.id;
htmllistitem = listitem.appendChild(
document.createElementNS("http://www.w3.org/1999/xhtml","html:div"));
htmllistitem.innerHTML = $ei('storyTitleFormat').innerHTML;
storyTitle = listitem.getElementsByAttribute('class', 'storyTitle')[0];
storyTitle.appendChild(document.createTextNode(story.title));
htmllistitem.innerHTML += $ei('storyDetailsFormat').innerHTML;
storyNew= listitem.getElementsByAttribute('class', 'storyNew')[0];
if (currentStories.indexOf(story.id) != -1) {
storyNew.style.textDecoration = "line-through";
storyNew.style.backgroundColor = "black";
storyNew.style.color = "white";
storyNew.title = "Old";
}
}
currentStories = newStories;
//Adaptive update interval code START
sum = 0;
weights = [0.4, 0.3, 0.2, 0.1]
for (i=0; i<4; i++) {
if (stories[0].promote_date != null)
diff = stories[i].promote_date - stories[i+1].promote_date;
else
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", 0);
window.clearTimeout(gsti);
updateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
gsti = window.setTimeout(getStories, timeout);
//Adaptive update interval code END
$ei('diggIndicator').style.display = 'none';
//prefs.setIntPref("extensions.diggsidebar.progress", 100);
//$ei("diggIndicator").setAttribute('mode', 'determined');
//$ei("diggIndicator").setAttribute('value', 100);
//psi = window.setInterval(showProgress, Math.round(timeout/100));
//window.setTimeout("window.clearInterval(psi)", timeout);
diff = stories[i].submit_date - stories[i+1].submit_date;
sum += weights[i]*diff;
}
updateInterval = Math.round(sum*1000);
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch);
previousUpdateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
prefs.setIntPref("extensions.diggsidebar.updateinterval", updateInterval);
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
if (previousUpdateInterval == updateInterval)
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", (updateIntervalDecay + 1));
else
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", 0);
window.clearTimeout(gsti);
updateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
gsti = window.setTimeout(getStories, timeout);
//Adaptive update interval code END
$ei('diggIndicator').style.display = 'none';
//prefs.setIntPref("extensions.diggsidebar.progress", 100);
//$ei("diggIndicator").setAttribute('mode', 'determined');
//$ei("diggIndicator").setAttribute('value', 100);
//psi = window.setInterval(showProgress, Math.round(timeout/100));
//window.setTimeout("window.clearInterval(psi)", timeout);
}
//function showProgress() {
@ -267,108 +241,104 @@ function populateStoryList(e) {
// getService(Components.interfaces.nsIPrefBranch);
// progress = prefs.getIntPref("extensions.diggsidebar.progress");
// prefs.setIntPref("extensions.diggsidebar.progress", (progress - 1));
//
//
// $ei("diggIndicator").setAttribute('value', (parseInt($ei("diggIndicator").getAttribute('value')) - 1));
//}
function populateDescription(e) {
XHR = e.target;
myDump(XHR.responseText);
data = XHR.responseXML;
story = $xp(data, "/stories/story")[0];
//alert(story);
now = new Date();
if ($a(story, 'promote_date') != null)
then = new Date(parseInt($a(story, 'promote_date').escapeEntities())*1000);
else
then = new Date(parseInt($a(story, 'submit_date').escapeEntities())*1000);
diff = now - then;
if (diff < 0) diff = 0;
hr = Math.floor(diff/(1000*3600));
min = Math.floor(diff/(1000*60)) - 60*hr;
listitems = document.getElementsByTagName('richlistitem');
for (i=0; i<listitems.length; i++) {
listitems[i].getElementsByAttribute('class', 'storyDetails')[0].style.display = 'none';
listitems[i].getElementsByAttribute('class', 'storyCEIcon')[0].src = "chrome://diggsidebar/content/image/up.jpg";
}
storyListItem = $ei('story_'+ $a(story, 'id'))
storyDetails = storyListItem.getElementsByAttribute('class', 'storyDetails')[0];
storyDate = storyListItem.getElementsByAttribute('class', 'storyDate')[0];
storyStatus = storyListItem.getElementsByAttribute('class', 'storyStatus')[0];
storyContainer = storyListItem.getElementsByAttribute('class', 'storyContainer')[0];
storyTopic = storyListItem.getElementsByAttribute('class', 'storyTopic')[0];
storyUserName = storyListItem.getElementsByAttribute('class', 'storyUserName')[0];
storyDiggs = storyListItem.getElementsByAttribute('class', 'storyDiggs')[0];
storyComments = storyListItem.getElementsByAttribute('class', 'storyComments')[0];
storyDesc = storyListItem.getElementsByAttribute('class', 'storyDesc')[0];
storyLink = storyListItem.getElementsByAttribute('class', 'storyLink')[0];
storyHref = storyListItem.getElementsByAttribute('class', 'storyHref')[0];
storyCEIcon = storyListItem.getElementsByAttribute('class', 'storyCEIcon')[0];
storyRead = storyListItem.getElementsByAttribute('class', 'storyRead')[0];
//Application.console.log(XHR.responseText);
data = decodeJson(XHR.responseText);
story = data.stories[0];
now = new Date();
if (story.promote_date != null)
then = new Date(story.promote_date*1000);
else
then = new Date(story.submit_date*1000);
diff = Math.max(now - then, 0)
hr = Math.floor(diff/(1000*3600));
min = Math.floor(diff/(1000*60)) - 60*hr;
listitems = document.getElementsByTagName('richlistitem');
for (i=0; i<listitems.length; i++) {
listitems[i].getElementsByAttribute('class', 'storyDetails')[0].style.display = 'none';
listitems[i].getElementsByAttribute('class', 'storyCEIcon')[0].src =
"chrome://diggsidebar/content/image/up.jpg";
}
storyListItem = $ei('story_'+ story.id)
storyDetails = storyListItem.getElementsByAttribute('class', 'storyDetails')[0];
storyDate = storyListItem.getElementsByAttribute('class', 'storyDate')[0];
storyStatus = storyListItem.getElementsByAttribute('class', 'storyStatus')[0];
storyContainer = storyListItem.getElementsByAttribute('class', 'storyContainer')[0];
storyTopic = storyListItem.getElementsByAttribute('class', 'storyTopic')[0];
storyUserName = storyListItem.getElementsByAttribute('class', 'storyUserName')[0];
storyDiggs = storyListItem.getElementsByAttribute('class', 'storyDiggs')[0];
storyComments = storyListItem.getElementsByAttribute('class', 'storyComments')[0];
storyDesc = storyListItem.getElementsByAttribute('class', 'storyDesc')[0];
storyLink = storyListItem.getElementsByAttribute('class', 'storyLink')[0];
storyHref = storyListItem.getElementsByAttribute('class', 'storyHref')[0];
storyCEIcon = storyListItem.getElementsByAttribute('class', 'storyCEIcon')[0];
storyRead = storyListItem.getElementsByAttribute('class', 'storyRead')[0];
storyDate.innerHTML = ((hr > 0) && (min > 0)) ?
(hr + " hr " + min + " mins ago") :
((hr == 0) && (min > 0)) ?
(min + " mins ago") :
((hr == 0) && (min == 0)) ?
"just now" : "";
storyStatus.innerHTML = $a(story, 'status').escapeEntities();
//alert($a($et('container', story), 'name').escapeEntities());
storyContainer.innerHTML = $a($et('container', story), 'name').escapeEntities();
storyTopic.innerHTML = $a($et('topic', story), 'name').escapeEntities();
storyUserName.href = "http://digg.com/users/" + $a($et('user', story), 'name').escapeEntities();
storyUserName.innerHTML = $a($et('user', story), 'name').escapeEntities();
storyDiggs.innerHTML = $a(story, 'diggs').escapeEntities();
storyComments.innerHTML = $a(story, 'comments').escapeEntities();
storyDesc.innerHTML = $et('description', story).textContent.escapeEntities();
storyLink.href = $a(story, 'link');
storyHref.href = $a(story, 'href');
storyCEIcon.src = "chrome://diggsidebar/content/image/down.jpg";
storyRead.style.textDecoration = "line-through";
storyRead.style.backgroundColor = "black";
storyRead.style.color = "white";
storyRead.title = "Read";
storyNew.style.textDecoration = "line-through";
storyNew.style.backgroundColor = "black";
storyNew.style.color = "white";
storyNew.title = "Old";
(hr + " hr " + min + " mins ago") :
((hr == 0) && (min > 0)) ?
(min + " mins ago") :
((hr == 0) && (min == 0)) ?
"just now" : "";
storyStatus.innerHTML = story.status.escapeEntities();
storyContainer.innerHTML = story.container.name.escapeEntities();
storyTopic.innerHTML = story.topic.name.escapeEntities();
storyUserName.href = "http://digg.com/users/" + story.user.name;
storyUserName.innerHTML = story.user.name.escapeEntities();
storyDiggs.innerHTML = story.diggs;
storyComments.innerHTML = story.comments;
storyDesc.innerHTML = story.description.escapeEntities();
storyLink.href = story.link;
storyHref.href = story.href;
storyCEIcon.src = "chrome://diggsidebar/content/image/down.jpg";
storyRead.style.textDecoration = "line-through";
storyRead.style.backgroundColor = "black";
storyRead.style.color = "white";
storyRead.title = "Read";
storyNew.style.textDecoration = "line-through";
storyNew.style.backgroundColor = "black";
storyNew.style.color = "white";
storyNew.title = "Old";
storyDetails.style.display = 'block';
$ei('diggIndicator').style.display = 'none';
$ei('diggIndicator').style.display = 'none';
}
function getStories() {
//alert("inside getStories");
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch);
ep = prefs.getCharPref("extensions.diggsidebar.endpoint") || '';
//alert(ep);
$ei('diggEndPoint').value = "digg" + ep;
//ep ='';
fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') + "?count=30" +
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch);
ep = prefs.getCharPref("extensions.diggsidebar.endpoint") || '';
$ei('diggEndPoint').value = "digg" + ep;
fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
"?count=30" + "&type=json" +
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateStoryList);
//gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval"));
//gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval"));
}
function getDescription(storyId) {
myDump(storyId);
if (storyId == null) return;
fetchData("http://services.digg.com/story/" + storyId +
"?appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
if (storyId == null) return;
fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateDescription);
}
function createMenu() {
fetchData("http://services.digg.com/topics" +
"?appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
fetchData("http://services.digg.com/topics" + "?type=json" +
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
populateMenu);
}
@ -403,7 +373,7 @@ function togglePlayPause() {
getService(Components.interfaces.nsIPrefBranch);
updateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
//newTimeout = Math.round(timeout*parseInt($ei("diggIndicator").getAttribute('value'))/100);
gsti = window.setTimeout(getStories, timeout);
@ -415,13 +385,11 @@ function togglePlayPause() {
}
}
function myDump(aMessage) {
var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage("Diggsidebar: " + aMessage);
}
function decodeJson(string) {
var json = Components.classes["@mozilla.org/dom/json;1"]
.createInstance(Components.interfaces.nsIJSON);
return json.decode(string);
};
/*TODO
preferences
@ -435,12 +403,12 @@ function myDump(aMessage) {
added error notification
added accesskeys
added new story notification
v0.2.1
adaptive polling interval
promote_date
pause/play
v0.5
new ui
*/
*/