Permalink
Browse files

Merge pull request #23 from ArneCL/feature/update-wunderlist-api

Feature/update wunderlist api
  • Loading branch information...
paviro committed Jul 15, 2018
2 parents c911dae + a98b7dd commit ccb23dc119a0cf32917afac91777a69fb74b506e
Showing with 276 additions and 72 deletions.
  1. +63 −0 api_debug_client.js
  2. +74 −20 fetcher.js
  3. +3 −1 fetcher_debug_client.js
  4. +32 −25 node_helper.js
  5. +27 −26 package.json
  6. +77 −0 wunderlist-api.js
View
@@ -0,0 +1,63 @@
var Wunderlist = require("./wunderlist-api");
require("request-promise").debug = true;
const accessToken = "";
const clientID = "";
function retrieveTodos(wunderlist, listID) {
wunderlist
.retrieveTodos(listID)
.then(function(resp) {
console.log(JSON.stringify(resp));
})
.catch(function(err) {
console.log(err);
});
}
function retrieveUsers(wunderlist) {
wunderlist
.retrieveUsers()
.then(function(resp) {
console.log(JSON.stringify(resp));
})
.catch(function(err) {
console.log(err);
});
}
function retrieveList(wunderlist, listID) {
wunderlist
.retrieveList(listID)
.then(function(resp) {
console.log(JSON.stringify(resp));
})
.catch(function(err) {
console.log(err);
});
}
function retrieveLists(wunderlist) {
return new Promise(function(resolve, reject) {
wunderlist
.retrieveLists()
.then(function(resp) {
console.log(JSON.stringify(resp));
resolve(resp);
})
.catch(function(err) {
console.log(err);
reject();
});
});
}
var wunderlist = new Wunderlist(clientID, accessToken);
retrieveLists(wunderlist)
.then(function(lists) {
retrieveTodos(wunderlist, lists[0].id);
retrieveList(wunderlist, lists[0].id);
})
.catch();
retrieveUsers(wunderlist);
View
@@ -7,7 +7,7 @@
* MIT Licensed.
*/
var WunderlistSDK = require("wunderlist");
var Wunderlist = require("./wunderlist-api");
var moment = require("moment");
/* Fetcher
@@ -34,6 +34,8 @@ var Fetcher = function(
var reloadTimer = null;
var items = [];
var revision = 0;
var wunderlist = new Wunderlist(clientID, accessToken);
var fetchFailedCallback = function() {};
var itemsReceivedCallback = function() {};
@@ -44,27 +46,81 @@ var Fetcher = function(
* Request the new items.
*/
var fetchTodos = function() {
var fetchTodos = function(wunderlist) {
clearTimeout(reloadTimer);
reloadTimer = null;
var WunderlistAPI = new WunderlistSDK({
accessToken: accessToken,
clientID: clientID
});
WunderlistAPI.http.tasks
.forList(listID)
.done(function(tasks) {
items = localizeTasks(tasks);
self.broadcastItems();
scheduleTimer();
fetchList(wunderlist, listID)
.then(function(list) {
if (list.revision > revision || revision == 0) {
wunderlist
.retrieveTodos(listID)
.then(function(tasks) {
items = localizeTasks(tasks);
self.broadcastItems();
scheduleTimer();
})
.catch(function(err) {
// Wunderlist is known to occasionally have interal server-errors
if (isWunderlistAPIError(err) && revision != 0) {
console.log("Wunderlist returned an internal server-error. Recovering.")
scheduleTimer();
} else {
console.error(
"Failed to retrieve list: " +
listID +
" - accessToken: " +
accessToken +
" - clientID: " +
clientID +
"Reason: " +
err.stack
);
}
});
} else {
scheduleTimer();
}
revision = list.revision;
})
.fail(function(resp, code) {
console.error("there was a Wunderlist problem", resp, code);
.catch(function(err) {
// Wunderlist is known to occasionally have interal server-errors
if (isWunderlistAPIError(err) && revision != 0) {
console.log("Wunderlist returned an internal server-error. Recovering.")
scheduleTimer();
} else {
console.error(
"Failed to retrieve status for list: " +
listID +
" - accessToken: " +
accessToken +
" - clientID: " +
clientID +
"Reason: " +
err.stack
);
}
});
};
var isWunderlistAPIError = function(err) {
return err.statusCode == 500;
};
var fetchList = function(wunderlist, listID) {
return new Promise(function(resolve, reject) {
wunderlist
.retrieveList(listID)
.then(function(list) {
resolve(list);
})
.catch(function(err) {
reject(err);
});
});
};
/* localizeTasks(tasks)
* Localize the given array of tasks
*/
@@ -84,7 +140,7 @@ var Fetcher = function(
var scheduleTimer = function() {
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function() {
fetchTodos();
fetchTodos(wunderlist);
}, reloadInterval);
};
@@ -105,16 +161,14 @@ var Fetcher = function(
* Initiate fetchTodos();
*/
this.startFetch = function() {
fetchTodos();
fetchTodos(wunderlist);
};
/* broadcastItems()
* Broadcast the exsisting items.
*/
this.broadcastItems = function() {
if (items.length <= 0) {
return;
}
itemsReceivedCallback(self);
};
View
@@ -8,6 +8,8 @@
*/
var Fetcher = require("./fetcher.js");
var accessToken = ""
var clientID = ""
function createFetcher(listID, list, reloadInterval, accessToken, clientID) {
var self = this;
@@ -34,7 +36,7 @@ function createFetcher(listID, list, reloadInterval, accessToken, clientID) {
});
fetchers[listID] = {
name: list,
listID,
instance: fetcher
};
} else {
View
@@ -7,11 +7,10 @@
* MIT Licensed.
*/
var Wunderlist = require("./wunderlist-api");
var NodeHelper = require("node_helper");
const Fetcher = require("./fetcher.js");
var WunderlistSDK = require("wunderlist");
module.exports = NodeHelper.create({
start: function() {
this.instances = [];
@@ -24,18 +23,21 @@ module.exports = NodeHelper.create({
},
getLists: function(options, callback) {
var self = this;
var wunderlist = new WunderlistSDK({
accessToken: options.accessToken,
clientID: options.clientID
});
wunderlist.http.lists
.all()
.done(function(lists) {
var wunderlist = new Wunderlist(options.clientID, options.accessToken);
wunderlist
.retrieveLists()
.then(function(lists) {
callback(lists);
})
.fail(function(resp, code) {
console.error("there was a Wunderlist problem", code);
.catch(function(err) {
console.error(
"MMM-Wunderlist: Failed to retrieve lists for clientID:" +
options.clientID +
" - accessToken: " +
options.accessToken +
"Reason: " +
err.stack
);
});
},
@@ -64,22 +66,25 @@ module.exports = NodeHelper.create({
var self = this;
var retrievedAccounts = 0;
accounts.forEach(function(account) {
var wunderlist = new WunderlistSDK({
accessToken: account.accessToken,
clientID: account.clientID
});
wunderlist.http.users
.all()
.done(function(users) {
var wunderlist = new Wunderlist(account.clientID, account.accessToken);
wunderlist
.retrieveUsers()
.then(function(users) {
retrievedAccounts++;
self.addUsers(users);
if (retrievedAccounts == accounts.length) {
self.sendSocketNotification("users", self.users);
}
})
.fail(function(resp, code) {
console.error("there was a Wunderlist problem", code);
.catch(function(err) {
console.error(
"MMM-Wunderlist: Failed to retrieve users for clientID:" +
account.clientID +
" - accessToken: " +
account.accessToken +
"Reason: " +
err.stack
);
});
});
},
@@ -113,8 +118,10 @@ module.exports = NodeHelper.create({
var self = this;
console.log(
"Create new todo fetcher for list: " +
list.title + " - Account: " + config.clientID +
"MMM-Wunderlist: Create new todo fetcher for list: " +
list.title +
" - Account: " +
config.clientID +
" - Interval: " +
config.interval * 1000
);
@@ -143,7 +150,7 @@ module.exports = NodeHelper.create({
instance: fetcher
};
} else {
console.log("Use exsisting todo fetcher for list: " + list);
console.log("MMM-Wunderlist: Use exsisting todo fetcher for list: " + list.id);
fetcher = this.fetchers[list.id].instance;
fetcher.setReloadInterval(config.interval);
fetcher.broadcastItems();
View
@@ -1,28 +1,29 @@
{
"name": "Magic-Mirror-Module-Wunderlist",
"description": "Wunderlist module for the Magic Mirror.",
"version": "2.0.0",
"repository": {
"type": "git",
"url": "git+https://github.com/paviro/MMM-Wunderlist"
},
"keywords": [
"magic mirror",
"smart mirror",
"wunderlist",
"module"
],
"author": "Paul-Vincent Roll",
"contributors": [
"https://github.com/paviro/MMM-Wunderlist/graphs/contributors"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/paviro/MMM-Wunderlist/issues"
},
"homepage": "https://github.com/paviro/MMM-Wunderlist#readme",
"dependencies": {
"wunderlist": "latest",
"moment": "latest"
}
"name": "Magic-Mirror-Module-Wunderlist",
"description": "Wunderlist module for the Magic Mirror.",
"version": "2.0.0",
"repository": {
"type": "git",
"url": "git+https://github.com/paviro/MMM-Wunderlist"
},
"keywords": [
"magic mirror",
"smart mirror",
"wunderlist",
"module"
],
"author": "Paul-Vincent Roll",
"contributors": [
"https://github.com/paviro/MMM-Wunderlist/graphs/contributors"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/paviro/MMM-Wunderlist/issues"
},
"homepage": "https://github.com/paviro/MMM-Wunderlist#readme",
"dependencies": {
"moment": "latest",
"request": "^2.86.0",
"request-promise": "^4.2.2"
}
}
Oops, something went wrong.

0 comments on commit ccb23dc

Please sign in to comment.