public void TestNormal() { var ub = new UrlBuilder("http://llf.hanzify.org/llf/search.asp"); ub.Add("Where", "title"); ub.Add("keyword", "DbEntry"); string dst = ub.ToString(); Assert.AreEqual("http://llf.hanzify.org/llf/search.asp?Where=title&keyword=DbEntry", dst); }
public void TestChineseGbk() { var ub = new UrlBuilder("http://llf.hanzify.org/llf/search.asp"); ub.Add("Where", "title"); ub.Add("keyword", "中文", Util.GetGbkEncoding()); string dst = ub.ToString(); Assert.AreEqual("http://llf.hanzify.org/llf/search.asp?Where=title&keyword=%d6%d0%ce%c4", dst); }
public void TestUnicodeAsDefault() { var ub = new UrlBuilder("http://llf.hanzify.org/llf/search.asp", Encoding.Unicode); ub.Add("Where", "title"); ub.Add("keyword", "vb"); string dst = ub.ToString(); Assert.AreEqual("http://llf.hanzify.org/llf/search.asp?Where=t%00i%00t%00l%00e%00&keyword=v%00b%00", dst); }
/// <summary> /// get weather with city /// </summary> /// <param name="city"></param> /// <returns></returns> public Weather GetWeather(string city) { const string baseUrl = @"https://www.google.com"; UrlBuilder urlBuilder = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl)); urlBuilder.Add("hl", "zh-cn"); urlBuilder.Add("weather", city); string weatherXml = webHelper.Get(urlBuilder); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(weatherXml); XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information"); Weather.CityInfomaition cityInfo = new Weather.CityInfomaition( nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText, Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText), Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText)); XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions"); Weather.TodayWeather today = new Weather.TodayWeather( Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText), Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText), nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText, new ImageHelper(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText).Image); XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions"); Weather.DayWeather[] dayWeathers = new Weather.DayWeather[nodeList.Count]; for (int i = 0; i < nodeList.Count; i++) { string dayOfWeek = nodeList.Item(i).SelectSingleNode("day_of_week").Attributes["data"].InnerText; string height = nodeList.Item(i).SelectSingleNode("high").Attributes["data"].InnerText; string width = nodeList.Item(i).SelectSingleNode("low").Attributes["data"].InnerText; string condition = nodeList.Item(i).SelectSingleNode("condition").Attributes["data"].InnerText; string icon = nodeList.Item(i).SelectSingleNode("icon").Attributes["data"].InnerText; Weather.DayWeather dayWeather = new Weather.DayWeather( dayOfWeek, Convert.ToInt16(height), Convert.ToInt16(width), condition, new ImageHelper(string.Concat(baseUrl, icon)).Image ); dayWeathers[i] = dayWeather; } Weather weather = new Weather(cityInfo, today, dayWeathers); return(weather); }
public string TranslateV2(string word, string target) { const string api = "https://www.googleapis.com/language/translate/v2/languages"; UrlBuilder urlBuilder = new UrlBuilder(api); urlBuilder.Add("key","11111"); urlBuilder.Add("target","en"); urlBuilder.Add("q", word); string resJson = webHelper.Get(urlBuilder); NameValueCollection nameValue = JsonConvert.ToNameValue(resJson); return nameValue["responseData"]; }
public string TranslateV2(string word, string target) { const string api = "https://www.googleapis.com/language/translate/v2/languages"; UrlBuilder urlBuilder = new UrlBuilder(api); urlBuilder.Add("key", "11111"); urlBuilder.Add("target", "en"); urlBuilder.Add("q", word); string resJson = webHelper.Get(urlBuilder); NameValueCollection nameValue = JsonConvert.ToNameValue(resJson); return(nameValue["responseData"]); }
async Task HandleNewLevel7(GuildSave save, SocketGuildUser user) { var bUser = Program.GetUser(user); if (bUser.IsVerified) { return; } var responsible = await getAdmin(user, save.VerifyRole); var respStr = responsible?.Username ?? $"An administrator for `{user.Guild.Name}`"; await user.RemoveRoleAsync(save.VerifyRole, new RequestOptions() { AuditLogReason = responsible == null ? "Role added only by verification" : $"User must verify via website" }); if (responsible != null) { save.Fails[responsible.Id] = save.Fails.GetValueOrDefault(responsible.Id, 0) + 1; } bUser.IsApproved = true; // prevent them being locked from verifing var url = new UrlBuilder(Handler.LocalAPIUrl + "/verify"); url.Add(AuthToken.SessionToken, getSession(bUser)); await user.SendMessageAsync(embed : new EmbedBuilder() .WithTitle("Verification") .WithDescription($"{respStr} has attempted to give you " + $"a role which requires verification to recieve. To verify, you will require a valid email address " + $"registered to {Program.Configuration["ms_auth:name"]}\r\n" + $"To verify, [click here]({url})") .Build()); }
public string TranslateV1(string sourceWord, string fromLanguage, string toLanguage) { /* 调用: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=zh-CN|en&q=中国 返回的json格式如下: {"responseData": {"translatedText":"Chinese people are good people"}, "responseDetails": null, "responseStatus": 200}*/ const string api = "http://ajax.googleapis.com/ajax/services/language/translate"; UrlBuilder urlBuilder = new UrlBuilder(api); urlBuilder.Add("v", "1.0"); urlBuilder.Add("langpair", string.Format("{0}|{1}", fromLanguage, toLanguage)); urlBuilder.Add("q",sourceWord); string resJson = webHelper.Get(urlBuilder); NameValueCollection nameValue = JsonConvert.ToNameValue(resJson); return nameValue["responseData"]; }
public static UrlBuilder Format(this UrlBuilder target, ResizedImageFormat format) { if (format == ResizedImageFormat.Preserve) target.QueryCollection.Remove("format"); else target.Add("format", format.ToString().ToLowerInvariant()); return target; }
/// <summary> /// Generate image with focal point /// </summary> /// <param name="target">End of UrlBuilder</param> /// <example>>img src="@Html.ResizeImage(Model.CurrentPage.Image).Width(500).Height(200).FitMode(FitMode.Crop).UseFocalPoint()" /<</example> /// <returns></returns> public static string UseFocalPoint(this UrlBuilder target) { var focalPointData = target.GetFocalPointData(); var resizeSettings = new ResizeSettings(target.QueryCollection); if (focalPointData?.FocalPoint != null && focalPointData.ShouldApplyFocalPoint(resizeSettings)) { target.Add(Crop, CropDimensions.Parse(focalPointData, resizeSettings).ToString()); } return target.ToString(); }
public string TranslateV1(string sourceWord, string fromLanguage, string toLanguage) { /* * 调用: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=zh-CN|en&q=中国 * 返回的json格式如下: * {"responseData": {"translatedText":"Chinese people are good people"}, "responseDetails": null, "responseStatus": 200}*/ const string api = "http://ajax.googleapis.com/ajax/services/language/translate"; UrlBuilder urlBuilder = new UrlBuilder(api); urlBuilder.Add("v", "1.0"); urlBuilder.Add("langpair", string.Format("{0}|{1}", fromLanguage, toLanguage)); urlBuilder.Add("q", sourceWord); string resJson = webHelper.Get(urlBuilder); NameValueCollection nameValue = JsonConvert.ToNameValue(resJson); return(nameValue["responseData"]); }
/// <summary> /// Generate responsive image with focal point /// </summary> /// <param name="target">End of UrlBuilder</param> /// <param name="defaultWidth">Default image size</param> /// <param name="htmlAttributes">Html attributes: new {@class="class-name"}</param> /// <param name="widthSizes">Different dimensions for selecting closest size"</param> /// <example>@Html.ResizeImage(Model.CurrentPage.Image).UseFocalPoint(800, null, 1600, 1200, 1000, 800)</example> /// <returns>MvcHtmlString</returns> public static MvcHtmlString UseFocalPoint(this UrlBuilder target, int defaultWidth, object htmlAttributes, params int[] widthSizes) { // Idea borrowed from https://www.creuna.com/fi/blogit/responsive-images-with-episerver-and-imageresizer/ target.Remove(Height).Remove(HeightAlt).Remove(Width).Remove(WidthAlt); var focalPointData = target.GetFocalPointData(); var originalImageWidth = focalPointData?.OriginalWidth ?? 1; var urlBuilder = new UrlBuilder(target).Add(Width, $"{defaultWidth}"); var resizeSettings = new ResizeSettings(urlBuilder.QueryCollection); if (focalPointData?.FocalPoint != null) { urlBuilder.Add(Crop, CropDimensions.Parse(focalPointData, resizeSettings).ToString()); } var tagBuilder = new TagBuilder("img"); tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); tagBuilder.MergeAttribute("src", urlBuilder.ToString(), true); if (widthSizes != null) { var srcSet = new List<string>(); var pushOriginal = true; foreach (var width in widthSizes) { if (width > originalImageWidth) { continue; } urlBuilder = new UrlBuilder(target).Add(Width, $"{width}"); resizeSettings = new ResizeSettings(urlBuilder.QueryCollection); if (focalPointData?.FocalPoint != null) { urlBuilder.Add(Crop, CropDimensions.Parse(focalPointData, resizeSettings).ToString()); } srcSet.Add($"{urlBuilder} {width}{Width}"); if (originalImageWidth == width) { pushOriginal = false; } } if (pushOriginal) { urlBuilder = new UrlBuilder(target).Add(Width, $"{originalImageWidth}"); resizeSettings = new ResizeSettings(urlBuilder.QueryCollection); if (focalPointData?.FocalPoint != null) { urlBuilder.Add(Crop, CropDimensions.Parse(focalPointData, resizeSettings).ToString()); } srcSet.Add($"{urlBuilder} {originalImageWidth}{Width}"); } tagBuilder.MergeAttribute("srcset", string.Join(",\n", srcSet.ToArray()), true); tagBuilder.MergeAttribute("sizes", $"(min-width: {defaultWidth}px) {defaultWidth}px, 100vw", true); } return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing)); }
public static string GenerateUrl(string controller, string action, object[] parameters, List <KeyValuePair <string, string> > urlParam) { string appPath = HttpContextHandler.Instance.ApplicationPath.ToLower(); var url = new StringBuilder(); url.Append(appPath); if (!string.IsNullOrEmpty(appPath) && !appPath.EndsWith("/")) { url.Append("/"); } url.Append(controller.ToLower()).Append("/"); if (!string.IsNullOrEmpty(action)) { url.Append(action.ToLower()).Append("/"); } if (parameters != null) { foreach (var o in parameters) { if (o != null) { AppendParameter(url, o); } } } url.Length--; if (WebSettings.MvcPostfix != "") { url.Append(WebSettings.MvcPostfix); } var s = url.ToString(); if (urlParam != null) { var ub = new UrlBuilder(s, Encoding.UTF8); foreach (var kv in urlParam) { ub.Add(kv.Key, kv.Value); } return(ub.ToString()); } return(s); }
public static string GenerateUrl(string controller, string action, object[] parameters, List<KeyValuePair<string, string>> urlParam) { string appPath = HttpContextHandler.Instance.ApplicationPath.ToLower(); var url = new StringBuilder(); url.Append(appPath); if (!string.IsNullOrEmpty(appPath) && !appPath.EndsWith("/")) { url.Append("/"); } url.Append(controller.ToLower()).Append("/"); if (!string.IsNullOrEmpty(action)) { url.Append(action.ToLower()).Append("/"); } if (parameters != null) { foreach (var o in parameters) { if (o != null) { AppendParameter(url, o); } } } url.Length--; if (WebSettings.MvcPostfix != "") { url.Append(WebSettings.MvcPostfix); } var s = url.ToString(); if(urlParam != null) { var ub = new UrlBuilder(s, Encoding.UTF8); foreach (var kv in urlParam) { ub.Add(kv.Key, kv.Value); } return ub.ToString(); } return s; }
public static UrlBuilder Height(this UrlBuilder target, int height) { return target.Add("h", height.ToString()); }
public static UrlBuilder Zoom(this UrlBuilder target, double zoom) { return target.Add("zoom", zoom.ToString("0.##")); }
public static UrlBuilder Width(this UrlBuilder target, int width) { return target.Add("w", width.ToString()); }
public static UrlBuilder Mode(this UrlBuilder target, ScaleMode mode) { if (mode == ScaleMode.Default) return target; return target.Add("mode", mode.ToString().ToLowerInvariant()); }
public static UrlBuilder Quality(this UrlBuilder target, int quality) { return target.Add("quality", quality.ToString()); }
public static UrlBuilder Crop(this UrlBuilder target, CropSettings settings) { return target.Add("crop", settings.ToString()); }
/// <summary> /// get weather with city /// </summary> /// <param name="city"></param> /// <returns></returns> public Weather GetWeather(string city) { const string baseUrl = @"https://www.google.com"; UrlBuilder urlBuilder = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl)); urlBuilder.Add("hl", "zh-cn"); urlBuilder.Add("weather", city); string weatherXml = webHelper.Get(urlBuilder); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(weatherXml); XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information"); Weather.CityInfomaition cityInfo = new Weather.CityInfomaition( nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText, Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText), Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText)); XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions"); Weather.TodayWeather today = new Weather.TodayWeather( Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText), Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText), nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText, new ImageHelper(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText).Image); XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions"); Weather.DayWeather[] dayWeathers = new Weather.DayWeather[nodeList.Count]; for (int i = 0; i < nodeList.Count; i++) { string dayOfWeek = nodeList.Item(i).SelectSingleNode("day_of_week").Attributes["data"].InnerText; string height = nodeList.Item(i).SelectSingleNode("high").Attributes["data"].InnerText; string width = nodeList.Item(i).SelectSingleNode("low").Attributes["data"].InnerText; string condition = nodeList.Item(i).SelectSingleNode("condition").Attributes["data"].InnerText; string icon = nodeList.Item(i).SelectSingleNode("icon").Attributes["data"].InnerText; Weather.DayWeather dayWeather = new Weather.DayWeather( dayOfWeek, Convert.ToInt16(height), Convert.ToInt16(width), condition, new ImageHelper(string.Concat(baseUrl, icon)).Image ); dayWeathers[i] = dayWeather; } Weather weather = new Weather(cityInfo, today, dayWeathers); return weather; }
public static Weather GoogleWeather(string city) { const string baseUrl = @"https://www.google.com"; WebHelper connectionBase = new WebHelper(); UrlBuilder parameters = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl)); parameters.Add("hl","zh-cn"); parameters.Add("weather",city); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(connectionBase.Get(parameters.ToString())); XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information"); Weather.CityInfomaition cityInfo = new Weather.CityInfomaition( nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText, nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText, Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText), Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText)); XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions"); Weather.TodayWeather today = new Weather.TodayWeather( Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText), Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText), nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText, nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText, ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText)); XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions"); Weather.DayWeather tomorrow = new Weather.DayWeather( Convert.ToInt16(nodeList.Item(1).SelectSingleNode("high").Attributes["data"].InnerText), Convert.ToInt16(nodeList.Item(1).SelectSingleNode("low").Attributes["data"].InnerText), nodeList.Item(1).SelectSingleNode("condition").Attributes["data"].InnerText, ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText)); Weather.DayWeather third = new Weather.DayWeather( Convert.ToInt16(nodeList.Item(2).SelectSingleNode("high").Attributes["data"].InnerText), Convert.ToInt16(nodeList.Item(2).SelectSingleNode("low").Attributes["data"].InnerText), nodeList.Item(2).SelectSingleNode("condition").Attributes["data"].InnerText, ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText)); Weather.DayWeather fourth = new Weather.DayWeather( Convert.ToInt16(nodeList.Item(3).SelectSingleNode("high").Attributes["data"].InnerText), Convert.ToInt16(nodeList.Item(3).SelectSingleNode("low").Attributes["data"].InnerText), nodeList.Item(3).SelectSingleNode("condition").Attributes["data"].InnerText, ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText)); Weather weather = new Weather(cityInfo,today, tomorrow, third, fourth); return weather; }