-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfriends.gs
160 lines (141 loc) · 5.19 KB
/
friends.gs
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
function getFriendsPage(user_id, relationship, page, limit){
var peloton=getConfigDetails().peloton;
var url=peloton.http_base +'/api/user/'+user_id+"/"+relationship+"?limit="+limit+"&page="+page;
return getProfileListPage(url);
}
function getProfileListPage(url){
var config=getConfigDetails();
var peloton=config.peloton;
var json= UrlFetchApp.fetch(url,peloton.http_options).getContentText();
var result = JSON.parse(json);
var page={
profiles:result.data,
page: result.page,
page_count: result.page_count,
limit: result.limit,
total: result.total,
sort_by: result.sort_by,
show_next: result.show_next,
show_previous: result.show_previous
};
var profiles=page.profiles.length;
Logger.log(page);
Logger.log("Returning page "+ (page.page+1) +" out of "+page.page_count+" pages, containing "+page.limit+" records out of the total "+page.total);
return page;
}
function getFollowers(user_id, page_size){
return getFriends(user_id, "followers", page_size);
}
function getFollowing(user_id, page_size){
return getFriends(user_id, "following", page_size);
}
function testGetFollowing(){
getFollowing("36fb3771921a45e694057faf629288cd", 200);
}
function testGetFollowers(){
getFollowers("36fb3771921a45e694057faf629288cd", 200);
}
/* This is designed to be run manually to trim how many users
we're getting data for, between competitions so we can look for just participant data
NOTE that running this will cause previous competition data to be difficult to re-retrieve since we unsubscribe from the other users' data. A better solution might be to pull the data but ignore it, rather than unfollow the users */
function trimFollowersNotInSubgroups(){
var dataRange="SubGroups!B2:B9999";
var rows=SpreadsheetApp.getActive().getRange(dataRange).getValues();
var names={};
rows.map(name=>{ var n=name[0].toLowerCase().trim(); names[n]=n;});
Logger.log("Rows:" + rows.length);
Logger.log("NAMES: "+Object.keys(names).length);
var peloton=getConfigDetails().peloton;
var following=getFollowing(peloton.user_id,200);
var deleted=[];
following.map(profile=>{
var n=profile.username.toLowerCase().trim();
if (!names[n]) {
// Logger.log("No need to follow:"+n);
deleted.push(profile.id);
}
});
Logger.log("DELETE: "+deleted.length);
Logger.log(JSON.stringify(deleted));
deleted.map(unfollowUser);
}
function getFriends(user_id, relationship, page_size){
var event=eventStart("Get "+relationship.toUpperCase()+" for user",user_id +", PgSz="+page_size);
var profiles=[];
var done=false;
var page=0;
if(!page_size || page_size==0) page_size=200;
while(!done){
// Get Page of workouts
var results=getFriendsPage(user_id, relationship, page, page_size);
Logger.log("Processing page "+page);
if(results.profiles && results.profiles.length >0){
Logger.log("Adding another "+results.profiles.length+" profiles");
profiles.push(...results.profiles);
}
Logger.log("Show Next :"+results.show_next+"; total pages "+results.page_count);
if(!results.show_next || page==(results.page_count-1)){
done=true;
} else {
++page;
}
}
var arr=Object.values(profiles);
eventEnd(event,arr.length);
// Return Profiles in Name order
return arr.sort((a,b)=>{ return a.username.toLowerCase().localeCompare(b.username.toLowerCase());});
}
function getAllFriends(){
var peloton=getConfigDetails().peloton;
var followers=getFollowers(peloton.user_id,200);
var following=getFollowing(peloton.user_id,200);
var friends={};
following.map(profile=>{ friends[profile.username]=profile; })
followers.map(profile=>{ friends[profile.username]=profile; })
return Object.values(friends).sort((a,b)=>{return a.username.toLowerCase().localeCompare(b.username.toLowerCase());});
}
/*
{
"id": "xxxxxxxxxx",
"image_url": "https://s3.amazonaws.com/peloton-profile-images/xxxxx/xxxxxx",
"is_profile_private": false,
"location": "xxxxxxxx",
"total_followers": 733,
"total_following": 529,
"total_workouts": 603,
"username": "xxxxxxx",
"authed_user_follows": true,
"relationship": {
"me_to_user": "following",
"user_to_me": null
},
"category": "following"
},
*/
function refreshFriends(){
var event=eventStart("Refresh Friends");
var sheet=SpreadsheetApp.getActive().getSheetByName(FRIENDS_SHEET_NAME);
sheet.activate();
sheet.clear();
sheet.getRange(1,1).setValue("Loading friends "+new Date()).activateAsCurrentCell();
var friends=getAllFriends();
var rows=[];
rows[0]=["UserID","Leaderboard Name","Private", "Loaded", "Location", "Total Followers", "Total Following", "Total Workouts", "Following Me", "Following Them","Image URL"];
friends.map(profile=>{
rows.push([
profile.id,
profile.username,
profile.is_profile_private,
new Date(),
profile.location,
profile.total_followers,
profile.total_following,
profile.total_workouts,
profile.relationship.user_to_me,
profile.relationship.me_to_user,
profile.image_url
]);
});
eventEnd(event, friends.length);
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows);
}