Report abuse

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ==UserScript==
// @name                Hello Reco
// @namespace	        http://www.dpg.se/test
// @description	        Shows nearby Gowalla and Foursquare venues to a company on Reco.se.
// @include		http://reco.se/*
// @include		http://www.reco.se/*
// @exclude		http://www.reco.se/questions/*
// @exclude		http://www.reco.se/friends/*
// @exclude		http://www.reco.se/myprofile/*
// @exclude		http://www.reco.se/objectPage/*
// @exclude		http://www.reco.se/*.seam
// ==/UserScript==	

if(typeof(GM_getValue("Gowalla API key")) == 'undefined') {
   var GW_API_key = prompt('Your Gowalla API key','');
   GM_setValue('Gowalla API key', GW_API_key);
} else {
   GM_log('Gowalla API key is ' +  GM_getValue("Gowalla API key"));
}

/// The following part is a translated version of Ivo Ugrina's PHP code for Jaro-Winkler string distance
/// Published on http://www.iugrina.com/files/JaroWinkler/JaroWinkler.phps

/*
version 1.2

  Copyright (c) 2005-2009  Ivo Ugrina <ivo@iugrina.com>

  A PHP library implementing Jaro and Jaro-Winkler
  distance, measuring similarity between strings.

  Theoretical stuff can be found in:
  Winkler, W. E. (1999). "The state of record linkage and current
  research problems". Statistics of Income Division, Internal Revenue
  Service Publication R99/04. http://www.census.gov/srd/papers/pdf/rr99-04.pdf.


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or (at
  your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  ===

  A big thanks goes out to Pierre Senellart <pierre@senellart.com>
  for finding a small bug in code.
*/

Array.min = function( array ){
    return Math.min.apply( Math, array );
};

String.prototype.reverse=function(){return this.split("").reverse().join("");}

function getCommonCharacters( string1, string2, allowedDistance ){
  var i,j;
  str1_len = string1.length;
  str2_len = string2.length;
  temp_string2 = string2;
   
  commonCharacters='';

  for( i=0; i < str1_len; i++){
    
    noMatch = true;

    // compare if char does match inside given allowedDistance
    // and if it does add it to commonCharacters
    for( j= Math.max( 0, i-allowedDistance ); noMatch && j < Math.min( i + allowedDistance + 1, str2_len ); j++){
      if( temp_string2[j] == string1[i] ){
        noMatch = false;

    commonCharacters += string1[i];

    temp_string2[j] = '';
      }
    }
  }

  return commonCharacters;
}
  
function Jaro( string1, string2 ){
  var i;
    
  str1_len = string1.length;
  str2_len = string2.length;
    
  // theoretical distance
  distance = Math.floor(Math.min( str1_len, str2_len ) / 2.0); 
    
  // get common characters
  commons1 = getCommonCharacters( string1, string2, distance );
  commons2 = getCommonCharacters( string2, string1, distance );
    
  if( (commons1_len =  commons1.length) == 0) return 0;
  if( (commons2_len =  commons2.length) == 0) return 0;

  // calculate transpositions
  transpositions = 0;
  upperBound = Math.min( commons1_len, commons2_len );
  for( i = 0; i < upperBound; i++){
    if( commons1[i] != commons2[i] ) transpositions++;
  }
  transpositions /= 2.0;

  // return the Jaro distance
  return (commons1_len/(str1_len) + commons2_len/(str2_len) + (commons1_len - transpositions)/(commons1_len)) / 3.0;
    
}
 
function getPrefixLength( string1, string2, MINPREFIXLENGTH){
  var i;
  MINPREFIXLENGTH = typeof(MINPREFIXLENGTH) != 'undefined' ? MINPREFIXLENGTH : 4;
  
  n = Array.min(  [MINPREFIXLENGTH, string1.length, string2.length]);
  
  for(i = 0; i < n; i++){
    if( string1[i] != string2[i] ){
      // return index of first occurrence of different characters 
      return i;
    }
  }

  // first n characters are the same   
  return n;
}
  
function JaroWinkler(string1, string2, PREFIXSCALE){

  PREFIXSCALE = typeof(PREFIXSCALE) != 'undefined' ? PREFIXSCALE : 0.1;

  
  JaroDistance = Jaro( string1, string2 );
  
  prefixLength = getPrefixLength( string1, string2 );
  
  return JaroDistance + prefixLength * PREFIXSCALE * (1.0 - JaroDistance);
}

/// End of Jaro-Winkler string distance function

var scriptPart = document.evaluate( '//script[contains(.,"vCLat")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
if(scriptPart != null) {
var scriptPartString = scriptPart.textContent;  
var lat = scriptPartString.substring(scriptPartString.indexOf("var vCLat ")+13,scriptPartString.indexOf("';",scriptPartString.indexOf("var vCLat ")));
var lon = scriptPartString.substring(scriptPartString.indexOf("var vCLon ")+13,scriptPartString.indexOf("';",scriptPartString.indexOf("var vCLon ")));
var name = scriptPartString.substring(scriptPartString.indexOf("var vCName ")+13,scriptPartString.indexOf("';",scriptPartString.indexOf("var vCName ")));

if(lat.length > 0 && lon.length > 0) {gowalla(lat, lon); foursquare(lat,lon);}
var elmFoo = document.getElementById('companyPictures');
var statusBox = document.createElement('div');
statusBox.innerHTML = '<div id="gowallaStatusBox" class="dataBoxNew"><div class="header"><div class="leftTopCorner"></div><div class="rightTopCorner"></div><h2>Närliggande inloggningar</h2></div><div class="content clearfix"><div class="load"></div><div id="gowallaInfo"></div><div id="foursquareInfo"></div></div><div class="footer"><div class="leftCorner"></div><div class="rightCorner"></div></div></div>';
elmFoo.parentNode.insertBefore(statusBox, elmFoo);
var elmGow = document.getElementById('gowallaInfo');
elmGow.innerHTML = 'Kontaktar Gowalla...';
var elmFsq = document.getElementById('foursquareInfo');
elmFsq.innerHTML = 'Kontaktar Foursquare...';
}

function foursquare(lat, lon) {
  GM_xmlhttpRequest ({
    method: "GET",
    url: "http://api.foursquare.com/v1/venues.json?geolat="+lat+"&geolong="+lon,
    headers: {
	'Content-Type': 'application/json',
        'Accept': 'application/json',
	'User-Agent': 'Greasemonkey script Hello Reco',
    },
    onload: function(responseDetails) { 
	var result = eval('('+responseDetails.responseText+')');
        var list = '';

var maxe = [];

for(i=0;i<result["groups"][0]["venues"].length;i++) {
  var test = result.groups[0].venues[i].name+"";
  var sim = JaroWinkler(test, name);
  var bsim = JaroWinkler(test.reverse(), name.reverse());
  maxe[i] = sim + .8*bsim;
}
var sortedMaxe = maxe.slice(0);
sortedMaxe.sort().reverse();
threshold = sortedMaxe[5];
      for ( var i = 0; i<result.groups[0].venues.length; ++i ) {
	if(maxe[i]>threshold) {
	  var category;
if(result["groups"][0]["venues"][i]["primarycategory"]) {category = result["groups"][0]["venues"][i]["primarycategory"]["nodename"];} else {category = ''} 
	list += '<a href="http://foursquare.com/venue/'+result["groups"][0]["venues"][i]["id"]+'" title="'+result["groups"][0]["venues"][i]["address"]+', '+category+'">'+result["groups"][0]["venues"][i]["name"]+' [4]</a>';
	}
	}
	elmFsq.innerHTML = list;
	
    },
    onreadystatechange: function(responseDetails) {
    },
    onerror: function(responseDetails) {
      console.debug(responseDetails);
alert('Error');
    }
  });


}

function gowalla(lat, lon) {
GM_log("http://api.gowalla.com/spots?lat="+lat+"&lng="+lon+"&radius=50");
  GM_xmlhttpRequest ({
    method: "GET",
    url: "http://api.gowalla.com/spots?lat="+lat+"&lng="+lon+"&radius=50",
    headers: {
        'X-Gowalla-API-Key': GM_getValue('Gowalla API key'),
	'Content-Type': 'application/json',
        'Accept': 'application/json',
	'User-Agent': 'Greasemonkey script Hello Reco',
    },
    onload: function(responseDetails) { 
	var result = eval('('+responseDetails.responseText+')');
        var list = '';

var maxe = [];

for(i=0;i<result["spots"].length;i++) {
  var test = result.spots[i].name+"";
  var sim = JaroWinkler(test, name);
  var bsim = JaroWinkler(test.reverse(), name.reverse());
  maxe[i] = sim + .8*bsim;
}
var sortedMaxe = maxe.slice(0);
sortedMaxe.sort().reverse();
threshold = sortedMaxe[5];
      for ( var i = 0; i<result.spots.length; ++i ) {
	if(maxe[i]>threshold) {
	  var category;
	if(result["spots"][i]["spot_categories"][0]) {category = result["spots"][i]["spot_categories"][0]["name"];} else {category = ''} 

	list += '<a href="http://gowalla.com'+result["spots"][i]["url"]+'" title="'+category+'">'+result["spots"][i]["name"]+' [G]</a>';
	}
	}
	elmGow.innerHTML = list;
	
    },
    onreadystatechange: function(responseDetails) {
    },
    onerror: function(responseDetails) {
      console.debug(responseDetails);
alert('Error');
    }
  });

}