electron based twitter client
Revisão | b7fe8a4204b069e06ab9bc560e72f95e18f9a484 (tree) |
---|---|
Hora | 2016-02-10 23:55:11 |
Autor | Hiromichi Matsushima <hylom@Hiro...> |
Commiter | Hiromichi Matsushima |
rewrite models module and move tomodel directory
@@ -1,34 +0,0 @@ | ||
1 | -var OAuth = require('oauth'); | |
2 | -var authInfo = require('./.account.json'); | |
3 | - | |
4 | -var oauthToken = ''; | |
5 | -var oauthSecret = ''; | |
6 | - | |
7 | -var oauth = new OAuth.OAuth( | |
8 | - 'https://api.twitter.com/oauth/request_token', | |
9 | - 'https://api.twitter.com/oauth/access_token', | |
10 | - authInfo.consumer_key, | |
11 | - authInfo.consumer_secret, | |
12 | - '1.0A', | |
13 | - 'sprnkr://token', | |
14 | - 'HMAC-SHA1' | |
15 | -); | |
16 | - | |
17 | -exports.callbackUrl = function callbackUrl (callback) { | |
18 | - | |
19 | - function sendCallback(err, token, secret, results) { | |
20 | - oauthToken = token; | |
21 | - oauthSecret = secret; | |
22 | - | |
23 | - var authUrl = 'https://twitter.com/oauth/authenticate?oauth_token=' + token; | |
24 | - callback(err, authUrl); | |
25 | - }; | |
26 | - | |
27 | - oauth.getOAuthRequestToken(sendCallback); | |
28 | -} | |
29 | - | |
30 | -exports.getTokens = function getTokens (token, verifier, callback) { | |
31 | - console.log('token: ' + token); | |
32 | - console.log('verifier: ' + verifier); | |
33 | - oauth.getOAuthAccessToken(token, oauthSecret, verifier, callback); | |
34 | -}; |
@@ -1,14 +1,18 @@ | ||
1 | 1 | |
2 | 2 | var electron = require('electron'); |
3 | -var app = require('app'); | |
3 | +var app = electron.app; | |
4 | 4 | var BrowserWindow = require('browser-window'); |
5 | -var auth = require('./auth'); | |
6 | 5 | var url = require('url'); |
6 | +var authInfo = require('./.account.json'); | |
7 | +var models = require('./model/models.js'); | |
8 | + | |
7 | 9 | var mainWindow = null; |
8 | -var models = require('./models'); | |
9 | 10 | |
10 | 11 | function createWindow (url) { |
11 | 12 | mainWindow = new BrowserWindow({width: 400, height: 800}); |
13 | + if (client) { | |
14 | + mainWindow.client = client; | |
15 | + } | |
12 | 16 | if (url) { |
13 | 17 | mainWindow.loadURL(url); |
14 | 18 | } else { |
@@ -21,25 +25,23 @@ function createWindow (url) { | ||
21 | 25 | } |
22 | 26 | |
23 | 27 | app.on('ready', function() { |
28 | + //createWindow(); | |
29 | + var client = models.createClient(authInfo); | |
24 | 30 | var protocol = electron.protocol; |
25 | 31 | protocol.registerFileProtocol('sprnkr', function (req, callback) { |
26 | 32 | // req.url is 'sprnkr://token?oauth_token=***&oauth_verifier=***' |
27 | 33 | var u = url.parse(req.url, true); |
28 | - //console.log('token: ' + u.query.oauth_token); | |
29 | - | |
30 | - auth.getTokens(u.query.oauth_token, u.query.oauth_verifier, | |
31 | - function (err, token, secret, results) { | |
32 | - if (err) throw err; | |
33 | - console.log('access token: ', token); | |
34 | - console.log('access secret: ', secret); | |
35 | - models.setTokens(token, secret); | |
36 | - createWindow(); | |
37 | - }); | |
34 | + var token = u.query.oauth_token; | |
35 | + var verifier = u.query.oauth_verifier; | |
36 | + client.authorize(token, verifier, function (err) { | |
37 | + if (err) throw err; | |
38 | + app._clients = [client]; | |
39 | + createWindow(); | |
40 | + }); | |
38 | 41 | }); |
39 | 42 | |
40 | - //createWindow(); | |
41 | - auth.callbackUrl( function (err, url) { | |
42 | - createWindow(url); | |
43 | + client.getCallbackUrl(function(err, authUrl) { | |
44 | + createWindow(authUrl); | |
43 | 45 | }); |
44 | 46 | }); |
45 | 47 |
@@ -0,0 +1,70 @@ | ||
1 | +// client.js | |
2 | + | |
3 | +var OAuth = require('oauth'); | |
4 | +var Twitter = require('twitter'); | |
5 | + | |
6 | +var Client = function () {}; | |
7 | + | |
8 | +Client.prototype.getCallbackUrl = function getCallbackUrl (callback) { | |
9 | + var client = this; | |
10 | + function sendCallback(err, token, secret, results) { | |
11 | + client.requestToken = token; | |
12 | + client.requestSecret = secret; | |
13 | + | |
14 | + var authUrl = 'https://twitter.com/oauth/authenticate?oauth_token=' + token; | |
15 | + callback(err, authUrl); | |
16 | + }; | |
17 | + this.oauth.getOAuthRequestToken(sendCallback); | |
18 | +}; | |
19 | + | |
20 | +Client.prototype.authorize = function authorize(token, verifier, callback) { | |
21 | + var client = this; | |
22 | + function createTwitterClient(err, token, secret, results) { | |
23 | + if (err) { | |
24 | + callback(err); | |
25 | + } | |
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 | + }); | |
32 | + callback(false); | |
33 | + }; | |
34 | + client.oauth.getOAuthAccessToken(token, client.requestSecret, verifier, createTwitterClient); | |
35 | +} | |
36 | + | |
37 | +Client.prototype.getHomeTimeline = function getHomeTimeline(callback) { | |
38 | + client.twitter.get('statuses/home_timeline', callback); | |
39 | +}; | |
40 | + | |
41 | +Client.prototype.stream = function stream(callback) { | |
42 | + client.twitter.stream('user', {}, function (stream) { | |
43 | + stream.on('data', callback); | |
44 | + stream.on('error', function(error) { | |
45 | + throw error; | |
46 | + }); | |
47 | + }); | |
48 | +} | |
49 | + | |
50 | +function createClient(consumerKey, consumerSecret) { | |
51 | + if (consumerSecret === undefined) { | |
52 | + consumerSecret = consumerKey.consumer_secret; | |
53 | + consumerKey = consumerKey.consumer_key; | |
54 | + } | |
55 | + client = new Client(); | |
56 | + client.consumerKey = consumerKey; | |
57 | + client.consumerSecret = consumerSecret; | |
58 | + client.oauth = new OAuth.OAuth( | |
59 | + 'https://api.twitter.com/oauth/request_token', | |
60 | + 'https://api.twitter.com/oauth/access_token', | |
61 | + consumerKey, | |
62 | + consumerSecret, | |
63 | + '1.0A', | |
64 | + 'sprnkr://token', | |
65 | + 'HMAC-SHA1' | |
66 | + ); | |
67 | + return client; | |
68 | +}; | |
69 | + | |
70 | +exports.createClient = createClient; |
@@ -1,30 +0,0 @@ | ||
1 | - | |
2 | -var Twitter = require('twitter'); | |
3 | - | |
4 | -// OAuth parameters | |
5 | -var authInfo = require('./.account.json'); | |
6 | -var client = new Twitter(authInfo); | |
7 | -var myAuth = {}; | |
8 | -myAuth.consumer_key = authInfo.consumer_key; | |
9 | -myAuth.consumer_secret = authInfo.consumer_secret; | |
10 | -myAuth.access_token_key = authInfo.acess_token_key; | |
11 | -myAuth.access_token_secret = authInfo.access_token_secret; | |
12 | - | |
13 | -exports.setTokens = function (token, secret) { | |
14 | - myAuth.access_token_key = token; | |
15 | - myAuth.access_token_secret = secret; | |
16 | - client = new Twitter(myAuth); | |
17 | -}; | |
18 | - | |
19 | -exports.getHomeTimeline = function (callback) { | |
20 | - client.get('statuses/home_timeline', callback); | |
21 | -}; | |
22 | - | |
23 | -exports.stream = function (callback) { | |
24 | - client.stream('user', {}, function (stream) { | |
25 | - stream.on('data', callback); | |
26 | - stream.on('error', function(error) { | |
27 | - throw error; | |
28 | - }); | |
29 | - }); | |
30 | -} |
@@ -1,49 +1,50 @@ | ||
1 | -var util = require('util'); | |
2 | -var $ = require('jquery'); | |
3 | -var ejs = require('ejs'); | |
4 | -var moment = require('moment'); | |
5 | - | |
6 | -function sprnkrViewMain () { | |
7 | - var tweetWriter = {}; | |
8 | - tweetWriter.put = function write(tweet) { | |
9 | - if (tweet.user === undefined) { | |
10 | - return; | |
11 | - } | |
12 | - var tweetData = { | |
13 | - name: tweet.user.name + ' (@' + tweet.user.screen_name + ')', | |
14 | - timestamp: moment(tweet.created_at).format('YYYY-MM-DD hh:mm:ss'), | |
15 | - text: tweet.text, | |
16 | - iconUrl: tweet.user.profile_image_url | |
17 | - } | |
18 | - var templ = ''; | |
19 | - templ += '<div class="icon"><img src="<%= iconUrl %>"/></div>'; | |
20 | - templ += '<div class="header"><span class="username"><%= name %></span><span class="timestamp"><%= timestamp %></span></div>'; | |
21 | - templ += '<div class="body"><%= text %></div>'; | |
22 | - var content = ejs.render(templ, tweetData); | |
23 | - var elem = $("<li>" + content + "</li>"); | |
24 | - $("#sprnkr-items").prepend(elem); | |
25 | - } | |
26 | - tweetWriter.write = function (tweet) { | |
27 | - if (util.isArray(tweet)) { | |
28 | - tweet.reverse(); | |
29 | - tweet.forEach(this.put); | |
30 | - } else { | |
31 | - this.put(tweet); | |
32 | - } | |
33 | - }; | |
34 | - | |
35 | - const remote = require('electron').remote; | |
36 | - var models = remote.require('./models'); | |
37 | - models.getHomeTimeline(initTimeline); | |
38 | - | |
39 | - function initTimeline(err, tweets, resp) { | |
40 | - if (err) throw err; | |
41 | - tweetWriter.write(tweets); | |
42 | - models.stream(function(tweets) { | |
43 | - tweetWriter.write(tweets); | |
44 | - }); | |
45 | - }; | |
46 | - | |
47 | -} | |
48 | - | |
49 | -sprnkrViewMain(); | |
1 | +var util = require('util'); | |
2 | +var $ = require('jquery'); | |
3 | +var ejs = require('ejs'); | |
4 | +var moment = require('moment'); | |
5 | + | |
6 | +function sprnkrViewMain () { | |
7 | + var tweetWriter = {}; | |
8 | + tweetWriter.put = function write(tweet) { | |
9 | + if (tweet.user === undefined) { | |
10 | + return; | |
11 | + } | |
12 | + var tweetData = { | |
13 | + name: tweet.user.name + ' (@' + tweet.user.screen_name + ')', | |
14 | + timestamp: moment(tweet.created_at).format('YYYY-MM-DD hh:mm:ss'), | |
15 | + text: tweet.text, | |
16 | + iconUrl: tweet.user.profile_image_url | |
17 | + } | |
18 | + var templ = ''; | |
19 | + templ += '<div class="icon"><img src="<%= iconUrl %>"/></div>'; | |
20 | + templ += '<div class="header"><span class="username"><%= name %></span><span class="timestamp"><%= timestamp %></span></div>'; | |
21 | + templ += '<div class="body"><%= text %></div>'; | |
22 | + var content = ejs.render(templ, tweetData); | |
23 | + var elem = $("<li>" + content + "</li>"); | |
24 | + $("#sprnkr-items").prepend(elem); | |
25 | + } | |
26 | + tweetWriter.write = function (tweet) { | |
27 | + if (util.isArray(tweet)) { | |
28 | + tweet.reverse(); | |
29 | + tweet.forEach(this.put); | |
30 | + } else { | |
31 | + this.put(tweet); | |
32 | + } | |
33 | + }; | |
34 | + | |
35 | + const remote = require('electron').remote; | |
36 | + const app = remote.app; | |
37 | + var client = app._clients[0]; | |
38 | + client.getHomeTimeline(initTimeline); | |
39 | + | |
40 | + function initTimeline(err, tweets, resp) { | |
41 | + if (err) throw err; | |
42 | + tweetWriter.write(tweets); | |
43 | + client.stream(function(tweets) { | |
44 | + tweetWriter.write(tweets); | |
45 | + }); | |
46 | + }; | |
47 | + | |
48 | +} | |
49 | + | |
50 | +sprnkrViewMain(); |