protected virtual void CopyRankedData([NotNull] RankedKeyword keyword, [NotNull] KeywordsEverywhereResponseItem item) { if (int.TryParse(item.Vol, out var volume)) { keyword.Volume = volume; } keyword.CostPerClick = item.Cpc; if (double.TryParse(item.Competition, out var competition)) { keyword.Competition = competition; } }
public RankedKeyword RankKeyword(string keyword) { var rankedKeyword = new RankedKeyword { Text = keyword }; try { var url = GetRequestUrl(keyword); using (var client = new WebClient { Encoding = Encoding.UTF8 }) { var jsonString = client.DownloadString(url); if (string.IsNullOrWhiteSpace(jsonString)) { Log.Error($"Cannot get ranking information for '{keyword}'.", this); return(rankedKeyword); } var response = JsonConvert.DeserializeObject <KeywordsEverywhereResponse>(jsonString); if (response == null || !string.IsNullOrWhiteSpace(response.Error)) { Log.SingleError($"Cannot get ranking information for '{keyword}'. Error: {response?.Error}", this); return(rankedKeyword); } if (!string.IsNullOrWhiteSpace(response.Alert)) { Log.Warn($"Got an alert({response.Alert}) while was ranking '{keyword}'.", this); } CopyRankedData(rankedKeyword, response.Data?.SingleOrDefault(x => x.Keyword.Equals(keyword, StringComparison.InvariantCultureIgnoreCase))); } } catch (Exception e) { Log.Error($"Cannot rank keyword '{keyword}' using {nameof(KeywordsEverywhereKeywordRankingProvider)}.", e, this); } return(rankedKeyword); }