-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwine.js
More file actions
126 lines (98 loc) · 2.97 KB
/
Copy pathtwine.js
File metadata and controls
126 lines (98 loc) · 2.97 KB
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
var events = require('events');
var eventEmitter = new events.EventEmitter();
var request = require('request');
var _ = require('underscore');
var baseURL = 'https://api.vineapp.com';
var version = '1.3.2';
var userAgent = 'iphone/' + version + ' (iPhone; iOS 7.0; Scale/2.00)';
var vineClient = 'ios/' + version;
var self;
function Twine(args, callback){
self = this;
if(args && args.username && args.password){
self.auth(args.username, args.password, function(user){
self.setSession(user);
})
}
var key;
var userId;
}
Twine.prototype = new events.EventEmitter;
Twine.prototype.auth = function(username, password, callback){
request.post(baseURL + '/users/authenticate', {form:{"deviceToken":randomString(64), "username": username, "password": password}}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body).data;
callback(data);
}
})
}
Twine.prototype.setSession = function(args, callback){
this.key = args.key;
this.userId = args.userId;
self.emit('loggedIn');
if(callback){
callback(args);
}
}
Twine.prototype.getUser = function(userId, callback){
request({url : baseURL + '/users/profiles/' + userId, headers: headers: getHeaders() }, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body).data);
}
else {
console.log(response);
}
})
}
Twine.prototype.getMe = function(callback){
request({url : baseURL + '/users/me', headers: getHeaders() }, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body).data);
}
else {
console.log(response);
}
})
}
Twine.prototype.getTimeline = function(callback){
request({url : baseURL + '/timelines/graph', headers: getHeaders() }, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body).data);
}
else {
console.log(response);
}
})
}
Twine.prototype.getUserTimeline = function(userId, callback){
request({url : baseURL + '/timelines/users/' + userId , headers: getHeaders() }, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body).data);
}
else {
console.log(response);
}
})
}
Twine.prototype.searchUsers = function(query, callback){
request({url : baseURL + '/users/search/' + query , headers: getHeaders() }, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(JSON.parse(body).data);
}
else {
console.log(response);
}
})
}
module.exports = Twine;
// Helpers
function randomString(length){
var data = "";
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
_(length || 16).times(function(){ data += possible[_.random(possible.length-1)]; });
return data;
}
function getHeaders(){
// This is a stupid method. I know this. But there is a reason.
return {"vine-session-id" : this.key , "User-Agent" : userAgent, "X-Vine-Client" : vineClient};
}