|
12 | 12 |
|
13 | 13 | namespace Fritz.Chatbot.Commands
|
14 | 14 | {
|
15 |
| - class HttpPageTitleCommand : IExtendedCommand |
16 |
| - { |
17 |
| - private static readonly Regex UrlRegex = new Regex("(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); |
18 |
| - private static readonly Regex TitleRegex = new Regex("<title>\\s*(.+?)\\s*<\\/title>", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
19 |
| - private const string LINK_MESSAGE_TEMPLATE = "{{username}}'s linked page title: {{title}}"; |
20 |
| - |
21 |
| - public IChatService ChatService { get; private set; } |
22 |
| - |
23 |
| - public string Name => "PageTitle"; |
24 |
| - |
25 |
| - public string Description => "Write linked page title to chat"; |
26 |
| - |
27 |
| - public bool IsInternal => true; |
28 |
| - |
29 |
| - public int Order => 20; |
30 |
| - |
31 |
| - public bool Final => false; |
32 |
| - |
33 |
| - public TimeSpan? Cooldown => null; |
34 |
| - |
35 |
| - private async Task Execute(string userName, string fullCommandText) |
36 |
| - { |
37 |
| - var urls = GetUrls(fullCommandText); |
38 |
| - if (urls == null || !urls.Any()) |
39 |
| - { |
40 |
| - return; |
41 |
| - } |
42 |
| - |
43 |
| - foreach (var url in urls) |
44 |
| - { |
45 |
| - var source = GetSource(url); |
46 |
| - if (string.IsNullOrEmpty(source)) |
47 |
| - { |
48 |
| - continue; |
49 |
| - } |
50 |
| - |
51 |
| - var title = GetTitle(source); |
52 |
| - if (string.IsNullOrEmpty(title)) |
53 |
| - { |
54 |
| - continue; |
55 |
| - } |
56 |
| - |
57 |
| - var message = GetMessageFromTemplate(userName, title); |
58 |
| - await ChatService.SendMessageAsync(message); |
59 |
| - } |
60 |
| - |
61 |
| - return; |
62 |
| - } |
63 |
| - |
64 |
| - private IEnumerable<string> GetUrls(string fullCommandText) |
65 |
| - { |
66 |
| - return UrlRegex.Matches(fullCommandText).Select(m => m.Value); |
67 |
| - } |
68 |
| - |
69 |
| - private string GetSource(string url) |
70 |
| - { |
71 |
| - var uri = new UriBuilder(url).Uri; |
72 |
| - var source = new WebClient().DownloadString(uri); |
73 |
| - return source; |
74 |
| - } |
75 |
| - |
76 |
| - private string GetTitle(string source) |
77 |
| - { |
78 |
| - var match = TitleRegex.Match(source); |
79 |
| - if (!match.Success) |
80 |
| - { |
81 |
| - return null; |
82 |
| - } |
83 |
| - |
84 |
| - var titleStart = match.Value.IndexOf('>') + 1; |
85 |
| - var titleLength = match.Value.LastIndexOf('<') - titleStart; |
86 |
| - var title = match.Value.Substring(titleStart, titleLength); |
87 |
| - return title; |
88 |
| - } |
89 |
| - |
90 |
| - private string GetMessageFromTemplate(string username, string title) |
91 |
| - { |
92 |
| - return LINK_MESSAGE_TEMPLATE.Replace("{{username}}", username).Replace("{{title}}", title); |
93 |
| - } |
94 |
| - |
95 |
| - public static bool ContainsLink(string message) |
96 |
| - { |
97 |
| - return UrlRegex.IsMatch(message); |
98 |
| - } |
99 |
| - |
100 |
| - public bool CanExecute(string userName, string fullCommandText) |
101 |
| - { |
102 |
| - return HttpPageTitleCommand.ContainsLink(fullCommandText); |
103 |
| - } |
104 |
| - |
105 |
| - public Task Execute(IChatService chatService, string userName, string fullCommandText) |
106 |
| - { |
107 |
| - this.ChatService = chatService; |
108 |
| - return Execute(userName, fullCommandText); |
109 |
| - } |
110 |
| - } |
| 15 | + class HttpPageTitleCommand : IExtendedCommand |
| 16 | + { |
| 17 | + private static readonly Regex UrlRegex = new Regex("(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); |
| 18 | + private static readonly Regex TitleRegex = new Regex("<title>\\s*(.+?)\\s*<\\/title>", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| 19 | + private const string LINK_MESSAGE_TEMPLATE = "{{username}}'s linked page title: {{title}}"; |
| 20 | + |
| 21 | + public IChatService ChatService { get; private set; } |
| 22 | + |
| 23 | + public string Name => "PageTitle"; |
| 24 | + |
| 25 | + public string Description => "Write linked page title to chat"; |
| 26 | + |
| 27 | + public bool IsInternal => true; |
| 28 | + |
| 29 | + public int Order => 20; |
| 30 | + |
| 31 | + public bool Final => false; |
| 32 | + |
| 33 | + public TimeSpan? Cooldown => null; |
| 34 | + |
| 35 | + private async Task Execute(string userName, string fullCommandText) |
| 36 | + { |
| 37 | + var urls = GetUrls(fullCommandText); |
| 38 | + if (urls == null || !urls.Any()) |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + foreach (var url in urls) |
| 44 | + { |
| 45 | + var source = GetSource(url); |
| 46 | + if (string.IsNullOrEmpty(source)) |
| 47 | + { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + var title = GetTitle(source); |
| 52 | + if (string.IsNullOrEmpty(title)) |
| 53 | + { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + var message = GetMessageFromTemplate(userName, title); |
| 58 | + await ChatService.SendMessageAsync(message); |
| 59 | + } |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + private IEnumerable<string> GetUrls(string fullCommandText) |
| 65 | + { |
| 66 | + return UrlRegex.Matches(fullCommandText).Select(m => m.Value); |
| 67 | + } |
| 68 | + |
| 69 | + private string GetSource(string url) |
| 70 | + { |
| 71 | + var uri = new UriBuilder(url).Uri; |
| 72 | + var source = new WebClient().DownloadString(uri); |
| 73 | + return source; |
| 74 | + } |
| 75 | + |
| 76 | + private string GetTitle(string source) |
| 77 | + { |
| 78 | + var match = TitleRegex.Match(source); |
| 79 | + if (!match.Success) |
| 80 | + { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + var titleStart = match.Value.IndexOf('>') + 1; |
| 85 | + var titleLength = match.Value.LastIndexOf('<') - titleStart; |
| 86 | + var title = match.Value.Substring(titleStart, titleLength); |
| 87 | + return title; |
| 88 | + } |
| 89 | + |
| 90 | + private string GetMessageFromTemplate(string username, string title) |
| 91 | + { |
| 92 | + return LINK_MESSAGE_TEMPLATE.Replace("{{username}}", username).Replace("{{title}}", title); |
| 93 | + } |
| 94 | + |
| 95 | + public static bool ContainsLink(string message) |
| 96 | + { |
| 97 | + return UrlRegex.IsMatch(message); |
| 98 | + } |
| 99 | + |
| 100 | + public bool CanExecute(string userName, string fullCommandText) |
| 101 | + { |
| 102 | + |
| 103 | + if (FritzBot._OtherBots.Contains(userName.ToLowerInvariant())) return false; |
| 104 | + |
| 105 | + return HttpPageTitleCommand.ContainsLink(fullCommandText); |
| 106 | + } |
| 107 | + |
| 108 | + public Task Execute(IChatService chatService, string userName, string fullCommandText) |
| 109 | + { |
| 110 | + this.ChatService = chatService; |
| 111 | + return Execute(userName, fullCommandText); |
| 112 | + } |
| 113 | + } |
111 | 114 | }
|
0 commit comments