TanyaGorbunova
7 years agoAdvisor
Solution: Get a list users with recently earned badges
Hi, At the moment, there is no way to get a list of users with earned badges. I've submitted a feature request here. Please vote up the idea if you have this feature to be implemented. In the me...
- 7 years ago
Code:
// Get a list of users who visited the community during the specified period static string APIQueryLastVisitedUsers = "http://<PATH TO YOUR COMMUNITY WITH ID>/restapi/vc/users/visited?restapi.response_format=json&page_size=1000"; //Get a list of badges of last visited users static string APIQueryBadges = "<PATH TO YOUR COMMUNITY WITH ID>/restapi/vc/users/id/<USERID>/badges?restapi.response_format=json"; static void Main(string[] args) { date_start = "2018-03-05"; date_end = "2018-03-11"; APIQueryLastVisitedUsers += "&date_start=" + date_start + "&date_end=" + date_end; GetUsers().Wait(); Console.ReadLine(); } static async Task<LithiumUser> GetUsers() { HttpClient client = new HttpClient(); LithiumUser lithiumUser = new LithiumUser(); client.BaseAddress = new Uri(APIQueryLastVisitedUsers); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string path = @"E:\EarnedBadges.csv"; string badgesTitle = ""; string filecontent = ""; try { HttpResponseMessage response = await client.GetAsync(client.BaseAddress.ToString()); if (response.IsSuccessStatusCode) { //get a list of users who visited the site during the specified period lithiumUser = JsonConvert.DeserializeObject<LithiumUser>(await response.Content.ReadAsStringAsync()); foreach (var user in lithiumUser.Response.Users.User) { // we will get badges of each user badgesTitle = ""; APIQueryBadges = APIQueryBadges.Replace("<USERID>", user.Id.Empty.ToString()); response = await client.GetAsync(APIQueryBadges); if (response.IsSuccessStatusCode) { LithiumBadges lithiumBadge = new LithiumBadges(); lithiumBadge = JsonConvert.DeserializeObject<LithiumBadges>(await response.Content.ReadAsStringAsync()); foreach (var badge in lithiumBadge.Response2.UserBadges.UserBadge) { string[] date_start_arr = date_start.Split('-'); string[] date_end_arr = date_end.Split('-'); // we need the badges earned only during a specified period if (DateTime.Compare(badge.EarnedDate.Empty.DateTime.Date, new DateTime(Convert.ToInt32(date_start_arr[0]), Convert.ToInt32(date_start_arr[1]), Convert.ToInt32(date_start_arr[2]))) >= 0 & DateTime.Compare(badge.EarnedDate.Empty.DateTime.Date, new DateTime(Convert.ToInt32(date_end_arr[0]), Convert.ToInt32(date_end_arr[1]), Convert.ToInt32(date_end_arr[2]))) <= 0) { badgesTitle += badge.Badge.Title.Empty.Trim() + ","; } } } if (badgesTitle!="") filecontent += user.Id.Empty.ToString() + "," + badgesTitle + "\r\n"; APIQueryBadges = APIQueryBadges.Replace(user.Id.Empty.ToString(),"<USERID>"); } using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine(filecontent); } Console.WriteLine(filecontent); } } catch (Exception e) { // return null; } return lithiumUser; }
This app returns me a CSV file with the following format:
<userId>,<badgetitle>,<badgetitle>,<badgetitle>,...
<userId>,<badgetitle>,<badgetitle>,<badgetitle>,...
I hope it will help someone.