-
Notifications
You must be signed in to change notification settings - Fork 38
/
starGazerSearchByLocation.js
364 lines (322 loc) · 12.7 KB
/
starGazerSearchByLocation.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var reg = (elm, n) => elm != null ? elm[n] : '';
var cn = (ob, nm) => ob.getElementsByClassName(nm);
var tn = (ob, nm) => ob.getElementsByTagName(nm);
var gi = (ob, nm) => ob.getElementById(nm);
var delay = (ms) => new Promise(res => setTimeout(res, ms));
var rando = (n) => Math.round(Math.random() + n);
var unq = (arr) => arr.filter((e, p, a) => a.indexOf(e) == p);
var currentPage = window.location.href;
var startPage = currentPage.replace(/(?<=stargazers).+/, '');
var csvTable = (arr) => arr.map(itm => itm.toString().replace(/$/, '\r')).toString().replace(/\r,/g, '\r');
var popTarget = "gitStarPop";
var reg = (elm, n) => elm != null ? elm[n] : '';
var cn = (ob, nm) => ob.getElementsByClassName(nm);
var tn = (ob, nm) => ob.getElementsByTagName(nm);
var gi = (ob, nm) => ob.getElementById(nm);
var delay = (ms) => new Promise(res => setTimeout(res, ms));
var rando = (n) => Math.round(Math.random() + n);
var unq = (arr) => arr.filter((e, p, a) => a.indexOf(e) == p);
var csvTable = (arr) => arr.map(itm => itm.toString().replace(/$/, '\r')).toString().replace(/\r,/g, '\r');
function dragElement() {
var elmnt = this.parentElement;
var pos1 = 0,
pos2 = 0,
pos3 = 0,
pos4 = 0;
if (document.getElementById(this.id)) {
document.getElementById(this.id).onmousedown = dragMouseDown;
} else {
this.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
elmnt.style.opacity = "0.85";
elmnt.style.transition = "opacity 1000ms";
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
elmnt.style.opacity = "1";
}
}
function createPopTextArea(id) {
if (document.getElementById(id)) document.getElementById(id).outerHTML = "";
var cd = document.createElement("div");
cd.setAttribute("id", id);
cd.style.display = "inline-block";
cd.style.position = "fixed";
cd.style.top = "10%";
cd.style.left = "50%";
cd.style.width = "22%";
cd.style.height = "16%";
cd.style.background = "transparent";
cd.style.borderRadius = ".15em";
cd.style.padding = "2px";
cd.style.zIndex = "10000";
document.body.appendChild(cd);
var cb = document.createElement("button");
cb.setAttribute("id", id + "_close");
cb.style.float = "left";
cb.style.background = "#000";
cb.style.height = "20px";
cb.style.width = "20px";
cb.style.borderRadius = "50%";
cb.style.boxShadow = "0px";
cb.style.border = "3px solid Crimson";
cb.style.textAlign = "center";
cb.style.cursor = "pointer";
cb.style.userSelect = "none";
cb.style.fontSize = "1em";
cb.style.color = "Crimson";
cb.style.transform = "scale(1, 1) translate(3.5px, 3.5px) rotate(0deg)";
cb.addEventListener("click", killParent);
cb.addEventListener("mousedown", hoverO);
cb.addEventListener("mouseover", hoverI);
cb.addEventListener("mouseout", hoverO);
cd.appendChild(cb);
var hd = document.createElement("div");
hd.setAttribute("id", id + "_mover");
hd.style.width = "99%";
hd.style.height = "25%";
hd.style.backgroundColor = "#000000";
hd.style.borderTopLeftRadius = ".15em";
hd.style.borderTopRightRadius = ".15em";
hd.style.padding = "6px";
hd.style.cursor = 'move';
hd.style.boxShadow = "1px 1px 1px 0px #888888";
hd.addEventListener("mouseover", dragElement);
cd.appendChild(hd);
var tf = document.createElement("input");
tf.setAttribute("id", id + "_textfile");
tf.setAttribute("placeholder", "Atlanta OR London OR New York");
tf.style.width = "66%";
tf.style.height = "100%";
tf.style.padding = "3px";
tf.style.border = "1px solid #000000";
tf.style.background = "#0f0f0f";
tf.style.color = "#ffffff";
tf.style.fontSize = "1em";
tf.style.userSelect = "none";
tf.style.float = "right";
tf.style.boxShadow = "1px 1px 1px 0px #888888";
tf.addEventListener("keydown", (event) => {
if (event.key == "Enter") {
initSearchLoop();
}
});
hd.appendChild(tf);
var tb = document.createElement("div");
tb.setAttribute("id", id + "_textarea");
tb.innerText = "Add your location OR locations and press enter to start";
tb.style.width = "99%";
tb.style.height = "75%";
tb.style.padding = "3px";
tb.style.border = "1px solid #000000";
tb.style.color = "#878787";
tb.style.fontSize = "1em";
tb.style.userSelect = "none";
tb.style.boxShadow = "1px 1px 1px 0px #888888";
cd.appendChild(tb);
tb.style.backgroundColor = "#282828";
}
async function killParent() {
this.style.background = "Crimson";
this.style.transform = "scale(.001, .001) translate(3px, 3px) rotate(495deg)";
this.style.transition = "all 106ms cubic-bezier(.9,.37,.66,.96)";
await delay(206);
this.parentElement.outerHTML = "";
}
async function killElm() {
this.outerHTML = "";
}
async function hoverI() {
this.style.border = "2px solid Crimson";
await delay(40);
this.style.border = "1px solid Crimson";
await delay(30);
this.style.border = "1px solid #000";
await delay(20);
this.style.background = "Crimson";
this.style.color = "#000";
this.style.transition = "all 186ms cubic-bezier(.9,.37,.66,.96)";
}
async function hoverO() {
this.style.background = "#000";
this.style.border = "1px solid Crimson";
await delay(66);
this.style.border = "3px solid Crimson";
this.style.color = "Crimson";
this.style.transition = "all 186ms cubic-bezier(.9,.37,.66,.96)";
}
async function getEmailFromProfile(url) {
var res = await fetch(url + '?tab=repositories');
var text = await res.text();
var doc = new DOMParser().parseFromString(text, 'text/html');
var repos = doc.getElementsByClassName('col-12 d-flex width-full py-4 border-bottom public source');
var targetRepos = Array.from(repos).map(itm => itm.getElementsByTagName('a')[0].href + '/commit/master.patch');
return checkEmailPatch(targetRepos);
}
async function getPatches(link) {
var res = await fetch(link);
var html = await res.text();
var email = reg(/[\w|\.]+@\S+\.[a-zA-Z]+/.exec(html),0);
return email;
}
async function checkEmailPatch(repos) {
for (i = 0; i < repos.length; i++) {
var email = await getPatches(repos[i]);
if (email != '') {
return email;
}
}
}
async function downloadr(str, name) {
var type = "data:text/plain;charset=utf-8,";
var file = new Blob([str], {
type: type
});
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = name;
document.body.appendChild(a);
a.click();
await delay(10);
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}
async function getHoverCard(params,geoSearch){
var {user_path,repo_id,current_path} = params;
var res = await fetch(`https://github.com/users/${user_path}/hovercard?subject=repository%3A${repo_id}¤t_path=${current_path}`, {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"97\", \"Chromium\";v=\"97\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest"
},
"referrer": "https://github.com/bilibili/flv.js/",
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
});
var text = await res.text();
return geoSearch.test(text);
}
async function getNumOfStarGazers(url){
var res = await fetch(url);
var text = await res.text();
var doc = new DOMParser().parseFromString(text, 'text/html');
var repositoryCont = cn(doc, 'repository-content')[0];
var numGazers = cn(repositoryCont,'Counter')[0] ? parseInt(cn(repositoryCont,'Counter')[0].innerText.replace(/\D+/g, '')) : 0;
return numGazers;
}
async function getStarGazers(url,geoSearch){
var res = await fetch(url);
var text = await res.text();
var doc = new DOMParser().parseFromString(text, 'text/html');
var repositoryCont = cn(doc, 'repository-content')[0];
var followers = tn(repositoryCont,'li')?.length ? Array.from(tn(repositoryCont,'li')).map(i=>
[
/(?<=github.com\/).+/.exec(i.getElementsByTagName('a')?.[0]?.href)?.[0],
i.getElementsByTagName('a')?.[0]?.href
]
) : [];
var filteredFollowers = [];
for(var i=0; i<followers.length; i++){
let isMatch = await getHoverCard({
user_path:followers[i][0],
repo_id:Array.from(doc.getElementsByTagName('summary')).map(p=> JSON.parse(p.getAttribute('data-hydro-click'))).filter(p=> p?.payload?.repository_id)?.[0]?.payload?.repository_id,
current_path:encodeURIComponent(/(?<=github.com)\/.+/.exec(url)?.[0])
},geoSearch);
if(isMatch) filteredFollowers.push(followers[i][1]);
}
var pages = cn(doc,'paginate-container')[0] ? Array.from(tn(cn(doc,'paginate-container')[0],'a')) : null;
var pageArray = pages ? pages.filter(i=> i.innerText == 'Next') : [];
var pageLink = pageArray.length > 0 ? pageArray[0].href : null;
var outputObj = {nextPage: pageLink, filteredFollowers: filteredFollowers};
console.log(outputObj);
return outputObj;
}
async function getProfileDetails(url,geoSearch) {
var res = await fetch(url);
var text = await res.text();
var doc = new DOMParser().parseFromString(text, 'text/html');
var fullname = cn(doc, 'p-name vcard-fullname')[0] ? cn(doc, 'p-name vcard-fullname')[0].innerText.trim().replace(/,/g,'') : '';
var vcard = Array.from(tn(cn(doc, 'vcard-details')[0], 'li'));
var geo = vcard.filter(elm => elm.getAttribute('itemprop') == 'homeLocation')[0] ? vcard.filter(elm => elm.getAttribute('itemprop') == 'homeLocation')[0].innerText.trim().replace(/,/g,'') : '';
var empl = vcard.filter(elm => elm.getAttribute('itemprop') == 'worksFor')[0] ? vcard.filter(elm => elm.getAttribute('itemprop') == 'worksFor')[0].innerText.trim().replace(/,/g,'') : '';
if(geoSearch){
var emailCheck = await getEmailFromProfile(url);
var email = emailCheck && /noreply/i.test(emailCheck) === false ? emailCheck : '';
return [fullname, empl, geo, email, url];
}else{
return [];
}
}
async function loopThroughStarGazers(url,geoSearch){
var profilesToScrape = [];
var numGazers = await getNumOfStarGazers(url);
var pagesToLoop = Math.ceil(numGazers/30);
var firstRes = await getStarGazers(url,geoSearch);
var resLink = firstRes.nextPage;
if(firstRes.filteredFollowers.length > 0) firstRes.filteredFollowers.forEach(u=> profilesToScrape.push(u));
for(var i=0; i<numGazers; i++){
var elips = '';
if(/0$|3$|6$/.test(i.toString())){
var elips = '.' ;
}
if(/1$|4$|7$/.test(i.toString())){
var elips = '..';
}
if(/2$|5$|8$/.test(i.toString())){
var elips = '...';
}
var percentComplete = Math.round((i/(pagesToLoop)*10000)/100);
document.getElementById(popTarget+ "_textarea").innerText = 'Building List'+elips+'\n'+percentComplete+'% complete';
if(resLink){
var resObj = await getStarGazers(resLink,geoSearch);
await delay(111);
resLink = resObj.nextPage;
var resArr = resObj.filteredFollowers;
if(resObj.filteredFollowers.length > 0) resObj.filteredFollowers.forEach(u=> profilesToScrape.push(u));
}
}
return profilesToScrape;
}
async function downloadCSVofMatches(url,geoSearch) {
var filename = reg(/(?<=\.com\/).+?\/\w+/.exec(url), 0).replace(/\W+/g, '_') +'_'+ document.getElementById(popTarget+ "_textfile").value.replace(/\s+/g, '_');
var resArr = await loopThroughStarGazers(url,geoSearch);
var temp = [
['Full Name', 'Company', 'Location', 'Email', 'Github Url']
];
for (let i = 0; i < resArr.length; i++) {
let row = await getProfileDetails(resArr[i], geoSearch);
if(row.length > 0) temp.push(row);
}
downloadr(csvTable(temp), filename + '.csv');
}
function initSearchLoop(){
var geoStr = document.getElementById(popTarget+ "_textfile").value;
var searchG = geoStr ? new RegExp(geoStr.replace(/\s+OR\s+/gi, '|').trim(), 'i') : null;
document.getElementById(popTarget+ "_textarea").innerText = 'Initializing...';
downloadCSVofMatches(startPage,searchG);
}
createPopTextArea(popTarget);