| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Net; |
| 5 | using System.Text; |
| 6 | using System.Text.RegularExpressions; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | namespace Core.Librarys.Browser |
| 10 | { |
| 11 | public static class UrlHelper |
| 12 | { |
| 13 | private static readonly Dictionary<string, string> _domainNamesDict = new Dictionary<string, string>() |
| 14 | { |
| 15 | {"www.google.com","Google" }, |
| 16 | {"github.com","Github" }, |
| 17 | {"translate.google.com","Google 翻译" }, |
| 18 | {"v2ex.com","V2EX" }, |
| 19 | {"www.bilibili.com","哔哩哔哩" }, |
| 20 | }; |
| 21 | ///<summary> |
| 22 | /// |
| 23 | ///</summary> |
| 24 | ///<param name="url_"></param> |
| 25 | ///<param name="isRemovePH_"></param> |
| 26 | ///<returns></returns> |
| 27 | public static string GetDomain(string url_, bool isRemovePH_ = true) |
| 28 | { |
| 29 | if (string.IsNullOrEmpty(url_)) { return url_; } |
| 30 | |
| 31 | ChromeURLEncode(url_); |
| 32 | |
| 33 | var ph = Regex.Match(url_, @"((https|http|ftp|rtsp|mms)?:\/\/)").Value; |
| 34 | if (!string.IsNullOrEmpty(ph)) |
| 35 | { |
| 36 | url_ = url_.Replace(ph, string.Empty); |
| 37 | } |
| 38 | |
| 39 | int index = url_.IndexOf("/"); |
| 40 | if (index != -1) |
| 41 | { |
| 42 | url_ = url_.Substring(0, index); |
| 43 | } |
| 44 | |
| 45 | return !isRemovePH_ ? ph + url_ : url_; |
| 46 | } |
| 47 | |
| 48 | ///<summary> |
| 49 | /// |
| 50 | ///</summary> |
| 51 | ///<param name="url_"></param> |
| 52 | ///<returns></returns> |
| 53 | public static string GetName(string url_) |
| 54 | { |
| 55 | string result = url_; |
| 56 | |
| 57 | result = GetDomain(result); |
| 58 | |
| 59 | |
| 60 | if (_domainNamesDict.ContainsKey(result)) |
| 61 | { |
| 62 | return _domainNamesDict[result]; |
| 63 | } |
| 64 | |
| 65 | var arr = result.Split('.'); |
| 66 | |
| 67 | |
| 68 | string[] removeStrArr = { "www" }; |
| 69 | |
| 70 | if (arr.Length == 2) |
| 71 | { |
| 72 | return FirstLetterToUpper(arr[0]); |
| 73 | } |
| 74 | else if (arr.Length >= 3) |
| 75 | { |
| 76 | return removeStrArr.Contains(arr[0]) ? FirstLetterToUpper(arr[1]) : $"{FirstLetterToUpper(arr[1])} {FirstLetterToUpper(arr[0])}"; |
| 77 | } |
| 78 | return FirstLetterToUpper(result); |
| 79 | } |
| 80 | |
| 81 | ///<summary> |
| 82 | /// |
| 83 | ///</summary> |
| 84 | ///<param name="str_"></param> |
| 85 | ///<returns></returns> |
| 86 | private static string FirstLetterToUpper(string str_) |
| 87 | { |
| 88 | if (string.IsNullOrEmpty(str_)) return str_; |
| 89 | |
| 90 | return str_[0].ToString().ToUpper() + str_.Substring(1); |
| 91 | } |
| 92 | ///<summary> |
| 93 | /// |
| 94 | ///</summary> |
| 95 | ///<param name="url_"></param> |
| 96 | ///<returns></returns> |
| 97 | public static bool IsIndexUrl(string url_) |
| 98 | { |
| 99 | url_ = url_.Replace("http://", string.Empty); |
| 100 | url_ = url_.Replace("https://", string.Empty); |
| 101 | return url_.IndexOf("/") == -1 || url_.Last() == '/'; |
| 102 | } |
| 103 | |
| 104 | ///<summary> |
| 105 | /// |
| 106 | ///</summary> |
| 107 | ///<param name="url_"></param> |
| 108 | ///<returns></returns> |
| 109 | public static string ChromeURLEncode(string url_) |
| 110 | { |
| 111 | |
| 112 | url_ = Regex.Replace(url_, @"[가-힣]+", (e) => |
| 113 | { |
| 114 | var result = WebUtility.UrlEncode(e.Value); |
| 115 | return result; |
| 116 | }); |
| 117 | |
| 118 | url_ = Regex.Replace(url_, @"[\u0E00-\u0E7F]", (e) => |
| 119 | { |
| 120 | var result = WebUtility.UrlEncode(e.Value); |
| 121 | return result; |
| 122 | }); |
| 123 | |
| 124 | url_ = Regex.Replace(url_, @"[äöüÄÖÜß]", (e) => |
| 125 | { |
| 126 | var result = WebUtility.UrlEncode(e.Value); |
| 127 | return result; |
| 128 | }); |
| 129 | |
| 130 | url_ = Regex.Replace(url_, @"[ЁёА-я]", (e) => |
| 131 | { |
| 132 | var result = WebUtility.UrlEncode(e.Value); |
| 133 | return result; |
| 134 | }); |
| 135 | |
| 136 | url_ = Regex.Replace(url_, @"[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]", (e) => |
| 137 | { |
| 138 | var result = WebUtility.UrlEncode(e.Value); |
| 139 | return result; |
| 140 | }); |
| 141 | return Regex.Replace(url_, @"/[\u4e00-\u9fa5]+|[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[々〆〤ヶ]+/u", (e) => |
| 142 | { |
| 143 | var result = WebUtility.UrlEncode(e.Value); |
| 144 | return result; |
| 145 | }); |
| 146 | |
| 147 | } |
| 148 | } |
| 149 | } |