public static async Task <string> ConstructCaptionTextAsync(string _text, List <SearchModel> searches, IInstaApi api) { var text = _text + Environment.NewLine; foreach (var search in searches) { if (search.InHashtags) { foreach (var inString in search.GetInStrings()) { if (inString.StartsWith("#") && inString.Length > 1) { var _hashtag = await api.SearchHashtag(inString.Substring(1, inString.Length - 1)); if (_hashtag.Succeeded) { if (_hashtag.Value.Count > 0) { var hashtag = _hashtag.Value.First(_h => _h.MediaCount == _hashtag.Value.Max(h => h.MediaCount)); text += $"#{hashtag.Name} "; } } } } } text += Environment.NewLine; if (search.InUsers) { foreach (var inString in search.GetInStrings()) { if (inString.StartsWith("@") && inString.Length > 1) { var _user = await api.SearchUsersAsync(inString.Substring(1, inString.Length - 1)); if (_user.Succeeded) { if (_user.Value.Count > 0) { text += $"@{_user.Value.First().UserName} "; } } } } } } return(text); }
private static async void RunDMTaskAsync(TaskModel t, IInstaApi api) { var users = new List <InstaUserShort>(); // get essential search results foreach (var search in t.Searches) { foreach (var inString in search.GetInStrings()) { var _temp = await api.SearchUsersAsync(inString); if (_temp.Succeeded) { users.AddRange(_temp.Value.ToList()); } } } var filteredUsers = await Helper.GetFilteredUsersAsync(users, t.Searches, api, 5); // Executing if (filteredUsers.Count > 0) { foreach (var user in filteredUsers) { if (!user.IsPrivate) { var result = await api.SendDirectMessage(user.UserName, "", await Helper.ConstructCaptionTextAsync(t.GetValues().Text, t.Searches, api)); if (result.Succeeded) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Info)); } } } } }
private static async void RunCommentTaskAsync(TaskModel t, IInstaApi api) { List <InstaMedia> medias = new List <InstaMedia>(); foreach (var search in t.Searches) { if (search.InUsers) { var _tempList = new List <InstaUserShort>(); foreach (var inString in search.GetInStrings()) { var _temp = await api.SearchUsersAsync(inString); if (_temp.Succeeded) { _tempList.AddRange(_temp.Value); } } _tempList = await Helper.GetFilteredUsersAsync(_tempList, t.Searches, api, 10); foreach (var user in _tempList) { var _userMedia = await api.GetUserMediaAsync(user.UserName, PaginationParameters.MaxPagesToLoad(5)); if (_userMedia.Succeeded) { medias.AddRange(_userMedia.Value); } } } if (search.InPosts) { var _temp = await api.GetUserTimelineFeedAsync(PaginationParameters.MaxPagesToLoad(5)); if (_temp.Succeeded) { medias.AddRange(_temp.Value.Medias.ToList()); } } var filteredMedia = await Helper.GetFilteredMediaAsync(medias, t.Searches, api, 5); foreach (var media in filteredMedia) { var result = await api.CommentMediaAsync(media.InstaIdentifier, await Helper.ConstructCaptionTextAsync(t.GetValues().Text, t.Searches, api)); if (result.Succeeded) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Info)); } } } }
private static async void RunFollowTaskAsync(TaskModel t, IInstaApi api) { List <InstaUserShort> users = new List <InstaUserShort>(); t = t.Refreshed; foreach (var search in t.Searches) { foreach (var inString in search.GetInStrings()) { var _temp = await api.SearchUsersAsync(inString); if (_temp.Succeeded) { users.AddRange(_temp.Value.ToList()); var _tempList = new List <InstaUserShort>(); foreach (var user in users) { var _fs = await api.GetFriendshipStatusAsync(user.Pk); if (_fs.Succeeded) { if (!_fs.Value.Following) { _tempList.Add(user); } } } users = _tempList; } } var FilteredUsers = await Helper.GetFilteredUsersAsync(users, t.Searches, api, 5); // Execute if (FilteredUsers.Count > 0) { foreach (var user in FilteredUsers) { if (user != null) { var result = await api.FollowUserAsync(user.Pk); if (result.Succeeded) { if (result.Value.Following) { t.TaskExecuted(); Helper.Log($"Task Executed: {t.Name} - {t.TaskType.ToString()} {Environment.NewLine} {JsonConvert.SerializeObject(result.Value)}"); } else { Helper.Log(JsonConvert.SerializeObject(result.Value)); } } } } } } }