-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwings.data.js
166 lines (137 loc) · 3.05 KB
/
twings.data.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
var TWINGS = TWINGS || {};
/**
* Create panel object where we will create all element for graph
*/
TWINGS.data = {
storageTemp : {
'friends' : [],
'followers' : []
},
screenName : '',
storage : {}
};
/**
* Get all data for user since that twitter returns chunks of 100
*
* @param {string}
* screen_name
* @param {number}
* cursor
* @param {string}
* ff followers or friends
*/
TWINGS.data.getData = function(screenName, cursor, ff) {
ff = ff || 'followers';
cursor = cursor || -1;
this.screenName = screenName;
if (localStorage['TWINGS_' + screenName]) {
TWINGS.data.storage = JSON.parse(localStorage['TWINGS_' + screenName]);
TWINGS.data.animate();
return '';
}
var tUrl = "http://api.twitter.com/1/statuses/" + ff + ".json?screen_name="
+ screenName + "&cursor=" + cursor;
log(screenName);
window.location.hash = screenName;
$.ajax({
url : tUrl,
dataType : 'jsonp',
success : function(data) {
log('loading ' + cursor);
log('loading next ' + data.next_cursor + ' ' + ff);
TWINGS.data.storageTemp[ff].push(data.users);
if (data.next_cursor > 0 && ff === 'followers') {
TWINGS.data.getData(screenName, data.next_cursor);
} else {
if (data.next_cursor === 0 && ff === 'friends') {
TWINGS.data.prepareData();
return '';
}
TWINGS.data.getData(screenName, data.next_cursor, 'friends');
}
}
});
return '';
};
/**
* Merge all data in one array
*/
TWINGS.data.prepareData = function() {
var tempData = {
data : {},
count : {}
};
var data = this.storageTemp.friends;
var d = '';
var total = {
friends : 0,
followers : 0,
following : 0
};
for ( var i = 0; i < data.length; i++) {
for ( var j = 0; j < data[i].length; j++) {
d = data[i][j];
d.type = 'following';
total.following++;
tempData.data[d.id] = d;
}
log('merging data');
}
data = this.storageTemp.followers;
for ( var i = 0; i < data.length; i++) {
for ( var j = 0; j < data[i].length; j++) {
d = data[i][j];
if (tempData.data[d.id]) {
d.type = 'friend';
total.friends++;
total.following--;
} else {
d.type = 'follower';
total.followers++;
}
tempData.data[d.id] = d;
}
}
tempData.count = total;
tempData.count.totals = total.followers + total.friends + total.following;
if (localStorage) {
localStorage.clear();
localStorage['TWINGS_' + this.screenName] = JSON.stringify(tempData);
}
TWINGS.data.storage = tempData;
TWINGS.data.animate();
return tempData;
};
/**
* Get Twitter id from DOM elem id
*
* @param {String}
* id
* @return {String}
*/
TWINGS.data.getId = function(id) {
return id.split("_")[1];
};
/**
* Show rings with animation
*/
TWINGS.data.animate = function(){
var cnt = 10;
var e = setInterval( function(){
TWINGS.panel.create(cnt);
cnt--;
if (cnt < 1){
clearInterval(e);
}
}, 50);
};
/**
* Get screen_name by id
*
* @param {String}
* id
* @return {String}
*/
TWINGS.data.getScreenName = function(id) {
return TWINGS.data.storage.data[id].screen_name;
};