-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
140 lines (123 loc) · 3.43 KB
/
main.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
exports.init = init;
var models = sp.require("sp://import/scripts/api/models");
var views = sp.require("sp://import/scripts/api/views");
var settings = sp.require("settings");
var currentSinceId;
function init() {
var playlist = new models.Playlist();
playlist.subscribed = false;
var listView = new views.List(playlist);
var content = document.getElementById("content");
content.appendChild(listView.node);
fetchTweetsAndAddToPlaylist(playlist);
}
function fetchTweetsAndAddToPlaylist(playlist) {
searchTwitter(function(uriArray){
if(uriArray.length === 0) {
setTimeout(fetchTweetsAndAddToPlaylist, 10000, playlist);
return;
}
addToPlaylist(playlist, uriArray, function(){
setTimeout(fetchTweetsAndAddToPlaylist, 10000, playlist);
});
});
}
function searchTwitter(callback) {
var reqUrl = "http://search.twitter.com/search.json?q=spotify%20filter:links&include_entities=true&result_type=recent&rpp=100";
if(currentSinceId) {
reqUrl += "&since_id="+currentSinceId;
}
$.ajax({
type: "GET",
url: reqUrl,
dataType: "jsonp",
timeout : 5000,
success: function(data){
processTweets(data.results, callback);
},
error: function(){
callback([]);
}
});
}
function processTweets(data, callback) {
if(!data || data.length === 0) {
callback([]);
return;
}
currentSinceId = data[0].id_str;
// create a new array with the url's only
var arr = data.map(function(item){
if(!item.entities || !item.entities.urls) {
return "none";
}
return item.entities.urls[0].expanded_url;
});
// check the url's
async.map(arr, function(item, callback){
if(!item) { //TODO why are the items sometimes undefined?
callback(null, "none");
} else if(item.indexOf("open.spotify.com") !== -1) {
callback(null, item);
} else if(item.indexOf("spoti.fi") !== -1) {
$.ajax({
type: "GET",
url: "http://api.bitly.com/v3/expand?shortUrl="+encodeURIComponent(item)+"&login="+settings.bitlyUsername+"&apiKey="+settings.bitlyApiKey,
dataType: "jsonp",
success: function (data) {
if(!data.data || data.data.expand[0].error) {
console.log("bit.ly url not found");
callback(null, "none");
} else {
var longUrl = data.data.expand[0].long_url;
callback(null, longUrl);
}
},
error: function() {
console.log("bit.ly error");
callback(null, "none");
}
});
} else {
console.log("none spotify: "+item);
callback(null, "none");
}
}, function(err, results){
// remove the "none" once
var arr = results.filter(function(item, index, array){
return item !== "none";
});
console.log("Stats: "+data.length+" tweets, "+arr.length+" contained spotify links");
callback(arr);
});
}
function addToPlaylist(playlist, uriArray, callback) {
async.forEachSeries(uriArray, function(item, callback){
processUri(item, playlist, callback);
}, function(err){
callback();
});
};
function processUri(uri, playlist, callback) {
var type = models.Link.getType(uri);
if(type === 4) { //track
var track = models.Track.fromURI(uri, function(a) {
if(a.data.availableForPlayback) {
playlist.add(a);
}
callback();
});
} else if(type === 2) { //album
var album = models.Album.fromURI(uri, function(a){
a.data.tracks.forEach(function(element, index, array) {
var o = new models.Track(element);
if(element.availableForPlayback) {
playlist.add(o);
}
});
callback();
});
} else {
callback();
}
}