跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

UrlHelper.cs

149 行 · 4.9 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Text;
6using System.Text.RegularExpressions;
7using System.Threading.Tasks;
8
9namespace 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 /// 谷歌浏览器URL编码
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}