/
expander_lib.js
executable file
·165 lines (158 loc) · 4.6 KB
/
expander_lib.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
function Expander() {
this.services = null;
this.servicesTimer = null;
this.urlsCache = {};
this.retryTimeout = 10000;
this.populateServicesCache();
this.currentServiceIdx = 0;
}
Expander.servicesArray = ['untiny', 'longshore'];
Expander.services = {
untiny: {
expand: function(url) {
return ['http://untiny.me/api/1.0/extract/', {url: url, format: 'json', apiKey: 'cfba0bb3e37778459d1fd245b5c6c7890f3167a6'}];
},
services: function() {
return ['http://untiny.me/api/1.0/services/', {format: 'json', apiKey: 'cfba0bb3e37778459d1fd245b5c6c7890f3167a6'}];
},
parseServices: function(services, data) {
if(!services) {
services = {};
}
for(var domain in data) {
if(!services[domain]) {
services[domain] = {};
}
services[domain].untiny = true;
}
return services;
},
parseExpand: function(data) {
return data.org_url;
}
},
longshore: {
expand: function(url) {
return ['http://long-shore.com/api/expand/format/json', {url: url}];
},
services: function() {
return ['http://long-shore.com/api/services/format/json', {}];
},
parseServices: function(services, data) {
if(!services) {
services = {};
}
for(var i = 0; i < data.length; ++i) {
var domain = data[i].domain;
if(!services[domain]) {
services[domain] = {};
}
services[domain].longshore = true;
services[domain].regexp = data[i].regexp;
}
return services;
},
parseExpand: function(data) {
return data.long_url;
}
}
};
Expander.prototype = {
doAjaxRequest: function(url, params, successCallback, errorCallback) {
$.ajax({
type: 'GET',
url: url,
data: params,
dataType: 'json',
success: successCallback,
error: errorCallback
});
},
populateServicesCache: function() {
for(var service in Expander.services) {
this.updateServicesCache(Expander.services[service]);
}
},
updateServicesCache: function(service) {
this.servicesTimer = null;
var _this = this;
var result = service.services();
var url = result[0], params = result[1];
this.doAjaxRequest(url, params,
function success(data, status) {
if(!data) {
return;
}
_this.services = service.parseServices(_this.services, data);
},
function error(request, status, error) {
// Failed to populate services list, we're going to try again
// upon the first expand request or in a few seconds.
_this.servicesTimer = setTimeout(function() {
_this.updateServicesCache(service);
}, _this.retryTimeout);
_this.retryTimeout = _this.retryTimeout * 2;
}
);
},
expand: function(url, callback) {
var longUrl = this.urlsCache[url];
var isShortened = true;
var success = true;
if(longUrl) {
callback(success, isShortened, longUrl);
return;
}
if(this.services) {
var urlDomain = url.match(/(https?:\/\/|www\.)(.*?)(\/|$)/i)[2];
var shortenerService = this.services[urlDomain];
if(shortenerService) {
while(true) {
var serviceName = this.getCurrentService();
if(shortenerService[serviceName]) {
this.runExpander(Expander.services[serviceName], url, callback);
break;
}
}
} else {
isShortened = false;
success = true;
callback(success, isShortened, url);
}
} else {
callback(true, false);
}
},
getCurrentService: function() {
var chosenService = Expander.servicesArray[this.currentServiceIdx];
this.currentServiceIdx += 1;
this.currentServiceIdx = this.currentServiceIdx % Expander.servicesArray.length;
return chosenService;
},
runExpander: function(service, shortUrl, callback) {
var _this = this;
var result = service.expand(shortUrl);
var url = result[0], params = result[1];
this.doAjaxRequest(url, params,
function success(data, status) {
var success = false;
var isShortened = true;
if(data) {
success = true;
var longUrl = service.parseExpand(data);
if(shortUrl == longUrl) {
isShortened = false;
} else {
_this.urlsCache[shortUrl] = longUrl;
}
}
callback(success, isShortened, longUrl);
},
function error(request, status, error) {
var success = false;
var isShortened = true;
callback(success, isShortened, '"' + error + '"(' + status + ')');
}
);
}
};