1 | #Region "Microsoft.VisualBasic::f4e6823b9373fdd84311f2b08fb328ff, Microsoft.VisualBasic.Core\Net\HTTP\sitemap.xml.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class sitemap |
35 | ' |
36 | ' Properties: urls |
37 | ' |
38 | ' Function: ToString |
39 | ' Structure url |
40 | ' |
41 | ' Properties: changefreq, lastmod, loc, priority |
42 | ' |
43 | ' Function: ToString |
44 | ' |
45 | ' Enum changefreqs |
46 | ' |
47 | ' always, daily, hourly, monthly, never |
48 | ' weekly, yearly |
49 | ' |
50 | ' |
51 | ' |
52 | ' |
53 | ' |
54 | ' Function: (+2 Overloads) Save, ScanAllFiles |
55 | ' |
56 | ' |
57 | ' /********************************************************************************/ |
58 | |
59 | #End Region |
60 | |
61 | Imports System.Text |
62 | Imports System.Text.RegularExpressions |
63 | Imports System.Xml.Serialization |
64 | Imports Microsoft.VisualBasic.ComponentModel |
65 | Imports Microsoft.VisualBasic.Language |
66 | Imports Microsoft.VisualBasic.Language.UnixBash |
67 | Imports Microsoft.VisualBasic.Serialization.JSON |
68 | Imports Microsoft.VisualBasic.Text |
69 | Imports Microsoft.VisualBasic.ValueTypes |
70 | |
71 | Namespace Net.Http |
72 | |
73 | ''' <summary> |
74 | ''' |
75 | ''' </summary> |
76 | ''' <remarks> |
77 | ''' ``` |
78 | ''' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" |
79 | ''' ``` |
80 | ''' </remarks> |
81 | <XmlRoot("urlset")> Public Class sitemap : Implements ISaveHandle |
82 | |
83 | <XmlElement("url")> Public Property urls As url() |
84 | |
85 | Public Structure url |
86 | |
87 | Public Property loc As String |
88 | Public Property priority As Double |
89 | Public Property lastmod As String |
90 | Public Property changefreq As String |
91 | |
92 | Public Overrides Function ToString() As String |
93 | Return Me.GetJson |
94 | End Function |
95 | End Structure |
96 | |
97 | Public Overrides Function ToString() As String |
98 | Return Me.GetJson |
99 | End Function |
100 | |
101 | Public Enum changefreqs |
102 | always |
103 | hourly |
104 | daily |
105 | weekly |
106 | monthly |
107 | yearly |
108 | never |
109 | End Enum |
110 | |
111 | ''' <summary> |
112 | ''' ```xml |
113 | ''' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> |
114 | ''' ``` |
115 | ''' </summary> |
116 | Const xmlns$ = "<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"">" |
117 | |
118 | Public Function Save(Optional path As String = "", Optional encoding As Encoding = Nothing) As Boolean Implements ISaveHandle.Save |
119 | Dim xml As String = (Me.GetXml) |
120 | xml = Regex.Replace(xml, "<urlset .*?>", xmlns) |
121 | Return xml.SaveTo(path, encoding) |
122 | End Function |
123 | |
124 | Public Function Save(Optional path As String = "", Optional encoding As Encodings = Encodings.UTF8) As Boolean Implements ISaveHandle.Save |
125 | Return Save(path, encoding.CodePage) |
126 | End Function |
127 | |
128 | Public Shared Function ScanAllFiles(wwwroot$, host$, Optional changefreq As changefreqs = changefreqs.monthly) As sitemap |
129 | Dim url As New List(Of url) |
130 | Dim freq$ = changefreq.ToString |
131 | Dim lastmod$ = $"{Now.Year}-{FillDateZero(Now.Month)}-{FillDateZero(Now.Day)}" |
132 | |
133 | wwwroot = wwwroot.GetDirectoryFullPath |
134 | host = host.TrimDIR |
135 | |
136 | For Each file$ In ls - l - r - "*.*" <= wwwroot |
137 | file = file.Replace("\", "/").Replace(wwwroot, "") |
138 | file = host & file |
139 | url += New url With { |
140 | .changefreq = freq, |
141 | .lastmod = lastmod, |
142 | .priority = 1, |
143 | .loc = file |
144 | } |
145 | Next |
146 | |
147 | Return New sitemap With { |
148 | .urls = url |
149 | } |
150 | End Function |
151 | End Class |
152 | End Namespace |