electron based twitter client
修訂 | d74b2b5343f1a9212468eeb6ca0d285eefd54e20 (tree) |
---|---|
時間 | 2016-02-11 00:23:15 |
作者 | Hiromichi Matsushima <hylom@Hiro...> |
Commiter | Hiromichi Matsushima |
implement .sprnkr setting file to store tokens
@@ -40,9 +40,14 @@ app.on('ready', function() { | ||
40 | 40 | }); |
41 | 41 | }); |
42 | 42 | |
43 | - client.getCallbackUrl(function(err, authUrl) { | |
44 | - createWindow(authUrl); | |
45 | - }); | |
43 | + if (client.loadTokens()) { | |
44 | + app._clients = [client]; | |
45 | + createWindow(); | |
46 | + } else { | |
47 | + client.getCallbackUrl(function(err, authUrl) { | |
48 | + createWindow(authUrl); | |
49 | + }); | |
50 | + } | |
46 | 51 | }); |
47 | 52 | |
48 | 53 | app.on('window-all-closed', function() { |
@@ -2,9 +2,36 @@ | ||
2 | 2 | |
3 | 3 | var OAuth = require('oauth'); |
4 | 4 | var Twitter = require('twitter'); |
5 | - | |
5 | +var path = require('path'); | |
6 | +var fs = require('fs'); | |
6 | 7 | var Client = function () {}; |
7 | 8 | |
9 | +var CONFIG_FILE = path.join(process.env.HOME, '.sprnkr'); | |
10 | + | |
11 | +Client.prototype.loadTokens = function loadTokens () { | |
12 | + var jsonData; | |
13 | + try { | |
14 | + jsonData = fs.readFileSync(CONFIG_FILE, 'utf8'); | |
15 | + } catch (e) { | |
16 | + return false; | |
17 | + } | |
18 | + var data = JSON.parse(jsonData); | |
19 | + this.accessToken = data.tokens.access_token; | |
20 | + this.accessTokenSecret = data.tokens.access_token_secret; | |
21 | + this.createTwitterClient(); | |
22 | + return true; | |
23 | +}; | |
24 | + | |
25 | +Client.prototype.saveTokens = function saveTokens () { | |
26 | + var jsonData = JSON.stringify({ | |
27 | + tokens: { | |
28 | + access_token: this.accessToken, | |
29 | + access_token_secret: this.accessTokenSecret | |
30 | + } | |
31 | + }); | |
32 | + fs.writeFileSync(CONFIG_FILE, jsonData, 'utf8'); | |
33 | +}; | |
34 | + | |
8 | 35 | Client.prototype.getCallbackUrl = function getCallbackUrl (callback) { |
9 | 36 | var client = this; |
10 | 37 | function sendCallback(err, token, secret, results) { |
@@ -17,18 +44,25 @@ Client.prototype.getCallbackUrl = function getCallbackUrl (callback) { | ||
17 | 44 | this.oauth.getOAuthRequestToken(sendCallback); |
18 | 45 | }; |
19 | 46 | |
47 | +Client.prototype.createTwitterClient = function createTwitterClient() { | |
48 | + this.twitter = new Twitter({ | |
49 | + consumer_key: this.consumerKey, | |
50 | + consumer_secret: this.consumerSecret, | |
51 | + access_token_key: this.accessToken, | |
52 | + access_token_secret: this.accessTokenSecret | |
53 | + }); | |
54 | +} | |
55 | + | |
20 | 56 | Client.prototype.authorize = function authorize(token, verifier, callback) { |
21 | 57 | var client = this; |
22 | 58 | function createTwitterClient(err, token, secret, results) { |
23 | 59 | if (err) { |
24 | 60 | callback(err); |
25 | 61 | } |
26 | - client.twitter = new Twitter({ | |
27 | - consumer_key: client.consumerKey, | |
28 | - consumer_secret: client.consumerSecret, | |
29 | - access_token_key: token, | |
30 | - access_token_secret: secret | |
31 | - }); | |
62 | + client.accessToken = token; | |
63 | + client.accessTokenSecret = secret; | |
64 | + client.saveTokens(); | |
65 | + client.createTwitterClient(); | |
32 | 66 | callback(false); |
33 | 67 | }; |
34 | 68 | client.oauth.getOAuthAccessToken(token, client.requestSecret, verifier, createTwitterClient); |