public VkontakteController( ILogger <DiagnosticsController> logger, VkontakteSettings vkontakteSettings, RealmSettings realmSettings) { this.logger = logger; this.vkontakteSettings = vkontakteSettings; this.realmSettings = realmSettings; }
public DiagnosticsController( ILogger <DiagnosticsController> logger, RealmSettings realmSettings, TweetSettings tweetSettings, VkontakteSettings vkontakteSettings, PushSettings pushSettings) { this.logger = logger; this.realmSettings = realmSettings; this.tweetSettings = tweetSettings; this.vkontakteSettings = vkontakteSettings; this.pushSettings = pushSettings; }
/// <summary> /// Returns vkontakte posts by community groups /// </summary> /// <returns> /// Returns a list of vkontakte posts. /// </returns> internal static async Task <List <ISocialPost> > GetAsync(VkontakteSettings vkontakteSettings, Dictionary <string, byte> communities) { var result = new List <VkontaktePost>(); try { using var api = new VkApi(); api.Authorize(new ApiAuthParams { AccessToken = vkontakteSettings.ServiceKey }); _communityGroups = (await api.Groups.GetByIdAsync(communities.Select(x => { if (long.TryParse(x.Key, out var groupId)) { return(Math.Abs(groupId).ToString()); } return(x.Key); }), null, GroupsFields.All)).ToList(); foreach (var communityGroup in communities) { try { var wallGetParams = new WallGetParams { Count = communityGroup.Value }; if (long.TryParse(communityGroup.Key, out var groupId)) { wallGetParams.OwnerId = groupId; } else { wallGetParams.Domain = communityGroup.Key; } var wall = await api.Wall.GetAsync(wallGetParams); var communityGroupPosts = wall?.WallPosts; if (communityGroupPosts != null) { result.AddRange(communityGroupPosts.Select(communityGroupPost => GetVkontaktePost(communityGroupPost, communityGroup.Key))); } } catch (Exception e) { Logger.LogError(e, "Unhandled error while getting original vkontakte posts"); } } var postsWithoutDuplicates = result.GroupBy(p => p.PostId).Select(g => g.First()).ToList(); // remove reposted duplicates var postIds = postsWithoutDuplicates.Select(x => x.PostId).ToList(); for (int i = 0; i < postsWithoutDuplicates.Count; i++) { if (postsWithoutDuplicates[i].CopyHistory.Select(x => x.PostId).Intersect(postIds).Any()) { postsWithoutDuplicates.RemoveAt(i); } } //ToDo: exclude duplicates for reposted posts! // fill posts with empty Text, PostedImage and PostedVideo from CopyHistory foreach (var post in postsWithoutDuplicates) { if (string.IsNullOrWhiteSpace(post.Text) && post.CopyHistory.Any() && string.IsNullOrWhiteSpace(post.PostedImage) && post.PostedVideo == null) { post.Text = $"Reposted: {GetPostText(post)}"; } } // set correct name for posts from user var users = await api.Users.GetAsync(postsWithoutDuplicates .Where(x => x.OwnerId != null && x.FromId != null && x.OwnerId != x.FromId) .Select(x => x.FromId.ToString())); foreach (var post in postsWithoutDuplicates.Where(x => x.OwnerId != null && x.FromId != null && x.OwnerId != x.FromId)) { var user = users.FirstOrDefault(x => x.Id == post.FromId); if (user?.LastName != null) { post.Name = $"{user.FirstName} {user.LastName}"; } } var sortedPosts = postsWithoutDuplicates.OrderByDescending(x => x.CreatedDate).Cast <ISocialPost>().ToList(); return(sortedPosts); } catch (Exception e) { Logger.LogError(e, "Unhandled error while getting original vkontakte posts"); } return(new List <ISocialPost>()); }