-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (157 loc) · 4.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var querystring = require('querystring'),
Oauth = require('oauth');
/*
* @ NodeWeiboTwitter
* ----------------------------------
* - Factory w singleton
* - var twitter = NodeWeiboTwitter.create("twitter", options); twitter.getTweet()
*/
var NodeWeiboTwitter = {
TYPE2API: {
"weibo": Weibo,
"twitter": Twitter
},
create: function (type, options) {
var API = this.TYPE2API[type];
if (!API) {
throw new TypeError(type + " is not supported");
}
if (!options.consumer_key || !options.consumer_secret) {
throw new TypeError("appKey or appSecret is not set");
}
if (!options.access_token_key) {
throw new TypeError("appKey or appSecret is not set");
}
return new API(options);
}
};
/*
* @ Weibo
* ----------------------------------
* - How to get Weibo access_token - read README
*/
function Weibo(options) {
this.oauth = new Oauth.OAuth2(
options.consumer_key,
options.consumer_secret,
"https://api.weibo.com/",
"oauth2/authorize",
"oauth2/access_token"
);
if (!options.access_token_key) {
throw new TypeError("accessTokenKey is needed for Weibo");
} else {
this.accessTokenKey = options.access_token_key;
}
}
Weibo.prototype = {
//TODO: As of 2.0, weibo stop supporting get weibo by screenName service, only allow return user's own weibo
//http://open.weibo.com/wiki/2/statuses/user_timeline
getWeibo: function (screenName, count, cb) {
if (!screenName) {
throw new TypeError("screenName is not set");
}
if (!count) {
count = 10;
}
if (!cb) {
cb = function () {
};
}
var api = "https://api.weibo.com/2/statuses/home_timeline.json?trim_user=true&screen_name=" + screenName + "&count=" + count,
header = {"User-Agent": "node-weibo-twitter"};
this.oauth._request("GET", api, header, "", this.accessTokenKey, this._weiboCallbackWrapper(cb));
},
postWeibo: function (msg, cb) {
if (!cb) {
cb = function () {};
}
var api = "https://api.weibo.com/2/statuses/update.json",
header = {"Content-Type": "application/x-www-form-urlencoded"},
data = {status: msg};
data = querystring.stringify(data);
this.oauth._request(
"POST",
api,
header,
data,
this.accessTokenKey,
this._weiboCallbackWrapper(cb)
);
},
_weiboCallbackWrapper: function (cb) {
return function (err, data, response) {
if (err) {
console.log(err);
return;
}
var result;
try {
result = JSON.parse(data);
} catch (e) {
console.log(e);
return;
}
cb(null, result, response);
};
}
};
/*
* @ Twitter
* ----------------------------------
* - https://dev.twitter.com/docs/api/1.1
*/
function Twitter(options) {
this.oauth = new Oauth.OAuth(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
options.consumer_key,
options.consumer_secret,
'1.0A',
null,
'HMAC-SHA1'
);
this.accessTokenKey = options.access_token_key;
this.accessTokenSecret = options.access_token_secret;
}
Twitter.prototype = {
getTweet: function (screenName, count, cb) {
if (!count) {
count = 10;
}
var api = "https://api.twitter.com/1.1/statuses/user_timeline.json?trim_user=true&exclude_replies=true&screen_name=" + screenName + "&count=" + count;
this.oauth.get(
api,
this.accessTokenKey,
this.accessTokenSecret,
cb
);
},
postTweet: function (msg, cb) {
var api = "https://api.twitter.com/1.1/statuses/update.json";
this.oauth.post(
api,
this.accessTokenKey,
this.accessTokenSecret,
{"status": msg},
cb
);
},
postDM: function (screenName, msg, cb) {
if (!screenName) {
throw new TypeError("screenName is not set");
}
var api = "https://api.twitter.com/1.1/direct_messages/new.json";
this.oauth.post(
api,
this.accessTokenKey,
this.accessTokenSecret,
{
"screen_name": screenName,
"text": msg
},
cb
);
}
};
module.exports = NodeWeiboTwitter;