public static FoundResult <User> TryGet( this IUsersCategory users, string screenName, ProfileFields fields = null, NameCase nameCase = null, bool skipAuthorization = false) { try { var result = users.Get(new List <string> { screenName }, fields, nameCase, skipAuthorization) .FirstOrDefault(); return(result == null ? FoundResult <User> .Error("Пользователь не найден.") : FoundResult <User> .Success(result)); } catch (InvalidUserIdException e) { return(FoundResult <User> .Error("Неверный ID пользователя", e)); } catch (Exception e) { return(FoundResult <User> .Error($"Неизвестная ошибка. {e.Message}", e)); } }
private FoundResult <Dictionary <char, double> > TryCollectCharsStat(User userInfo) { try { var stat = twitStatCollector.Collect(userInfo.Id); return(FoundResult <Dictionary <char, double> > .Success(stat)); } catch (VkApiException e) { return(FoundResult <Dictionary <char, double> > .Error(e.Message)); } }
public FoundResult <NotifyResult> Notify(string target, string login, string password) { log.Info($"Collecting statistic of 5 post for {target}"); var resultAuthorize = vkAuthorizer.TryAuthorize(vkApi, login, password); if (resultAuthorize.WasError) { log.Error($"Cannot authorize. {resultAuthorize.MessageError}"); return(FoundResult <NotifyResult> .Error($"Не удалось авторизоваться. {resultAuthorize.MessageError}")); } var foundResultUser = vkApi.Users.TryGet(target); if (foundResultUser.WasError) { log.Error($"Error finding {target}. {foundResultUser.MessageError}"); return(FoundResult <NotifyResult> .Error($"Во время поиска пользователя {target} произошла ошибка.")); } var userInfo = foundResultUser.Value; var foundResultStat = TryCollectCharsStat(userInfo); if (foundResultStat.WasError) { log.Error($"Error collecting statistic. {foundResultStat.MessageError}"); return(FoundResult <NotifyResult> .Error(foundResultStat.MessageError)); } var json = JsonConvert.SerializeObject(foundResultStat.Value .Select(e => new KeyValuePair <char, double>(e.Key, Math.Round(e.Value, 2))) .ToDictionary(e => e.Key, e => e.Value)); var postId = vkApi.Wall.Post(new WallPostParams { OwnerId = -181436132, Message = $"{userInfo.FirstName} {userInfo.LastName}, статистика для последних 5 постов: {json}" }); return(FoundResult <NotifyResult> .Success(new NotifyResult { JsonStat = json, PostId = postId })); }