-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from ernado-x/33-bug-nullref-in-savetodirectory
- Loading branch information
Showing
22 changed files
with
276 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace X.Web.Sitemap.Example; | ||
|
||
public interface IExample | ||
{ | ||
void Run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using X.Web.Sitemap.Example; | ||
|
||
Console.WriteLine("OK"); | ||
|
||
IExample example1 = new SitemapGenerationWithSitemapIndexExample(); | ||
example1.Run(); | ||
|
||
|
||
IExample example2 = new SimpleSitemapGenerationExample(); | ||
example2.Run(); |
20 changes: 20 additions & 0 deletions
20
src/X.Web.Sitemap.Example/SimpleSitemapGenerationExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace X.Web.Sitemap.Example; | ||
|
||
public class SimpleSitemapGenerationExample : IExample | ||
{ | ||
public void Run() | ||
{ | ||
// Pick a place where you would like to write the sitemap files in that folder will get overwritten by new ones | ||
var directory = Path.Combine(Path.GetTempPath(), "XWebsiteExample"); | ||
|
||
var urlGenerator = new UrlGenerator(); | ||
|
||
// Get list of website urls | ||
var allUrls = urlGenerator.GetUrls("mywebsite.com"); | ||
|
||
var sitemap = new Sitemap(allUrls); | ||
|
||
sitemap.SaveToDirectory(directory); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/X.Web.Sitemap.Example/SitemapGenerationWithSitemapIndexExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace X.Web.Sitemap.Example; | ||
|
||
/// <summary> | ||
/// This is an example showing how you might take a large list of URLs of different kinds of resources and build | ||
/// both a bunch of sitemaps (depending on how many URls you have) as well as a sitemap index file to go with it | ||
/// </summary> | ||
public class SitemapGenerationWithSitemapIndexExample : IExample | ||
{ | ||
public void Run() | ||
{ | ||
// Pick a place where you would like to write the sitemap files in that folder will get overwritten by new ones | ||
var targetSitemapDirectory = Path.Combine(Path.GetTempPath(), "XWebsiteExample"); | ||
|
||
// Pick a place where sitemaps will be accessible from internet | ||
var sitemapRootUrl = "https://www.mywebsite.com/sitemaps/"; | ||
|
||
var sitemapGenerator = new SitemapGenerator(); | ||
var sitemapIndexGenerator = new SitemapIndexGenerator(); | ||
var urlGenerator = new UrlGenerator(); | ||
|
||
// Get list of website urls | ||
var allUrls = urlGenerator.GetUrls("mywebsite.com"); | ||
|
||
|
||
// generate one or more sitemaps (depending on the number of URLs) in the designated location. | ||
var fileInfoForGeneratedSitemaps = sitemapGenerator.GenerateSitemaps(allUrls, targetSitemapDirectory); | ||
|
||
var sitemapInfos = new List<SitemapInfo>(); | ||
var dateSitemapWasUpdated = DateTime.UtcNow.Date; | ||
|
||
foreach (var fileInfo in fileInfoForGeneratedSitemaps) | ||
{ | ||
// It's up to you to figure out what the URI is to the sitemap you wrote to the file sytsem. | ||
// In this case we are assuming that the directory above has files exposed | ||
// via the /sitemaps/ subfolder of www.mywebsite.com | ||
|
||
var uriToSitemap = new Uri($"{sitemapRootUrl}{fileInfo.Name}"); | ||
|
||
sitemapInfos.Add(new SitemapInfo(uriToSitemap, dateSitemapWasUpdated)); | ||
} | ||
|
||
// Now generate the sitemap index file which has a reference to all of the sitemaps that were generated. | ||
sitemapIndexGenerator.GenerateSitemapIndex(sitemapInfos, targetSitemapDirectory, "sitemap-index.xml"); | ||
|
||
// After this runs you'll want to make sure your robots.txt has a reference to the sitemap index (at the bottom of robots.txt) like this: | ||
// "Sitemap: https://www.mywebsite.com/sitemaps/sitemap-index.xml" | ||
// You could do this manually (since this may never change) or if you are ultra-fancy, you could dynamically update your robots.txt with the names of the sitemap index | ||
// file(s) you generated | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
namespace X.Web.Sitemap.Example; | ||
|
||
public class UrlGenerator | ||
{ | ||
public List<Url> GetUrls(string domain) | ||
{ | ||
var productPageUrlStrings = GetHighPriorityProductPageUrls(domain); | ||
|
||
//--build a list of X.Web.Sitemap.Url objects and determine what is the appropriate ChangeFrequency, TimeStamp (aka "LastMod" or date that the resource last had changes), | ||
// and the a priority for the page. If you can build in some logic to prioritize your pages then you are more sophisticated than most! :) | ||
var allUrls = productPageUrlStrings.Select(url => new Url | ||
{ | ||
//--assign the location of the HTTP request -- e.g.: https://www.somesite.com/some-resource | ||
Location = url, | ||
//--let's instruct crawlers to crawl these pages monthly since the content doesn't change that much | ||
ChangeFrequency = ChangeFrequency.Monthly, | ||
//--in this case we don't know when the page was last modified so we wouldn't really set this. Only assigning here to demonstrate that the property exists. | ||
// if your system is smart enough to know when a page was last modified then that is the best case scenario | ||
TimeStamp = DateTime.UtcNow, | ||
//--set this to between 0 and 1. This should only be used as a relative ranking of other pages in your site so that search engines know which result to prioritize | ||
// in SERPS if multiple pages look pertinent from your site. Since product pages are really important to us, we'll make them a .9 | ||
Priority = .9 | ||
}).ToList(); | ||
|
||
var miscellaneousLowPriorityUrlStrings = GetMiscellaneousLowPriorityUrls(domain); | ||
|
||
var miscellaneousLowPriorityUrls = miscellaneousLowPriorityUrlStrings.Select(url => new Url | ||
{ | ||
Location = url, | ||
//--let's instruct crawlers to crawl these pages yearly since the content almost never changes | ||
ChangeFrequency = ChangeFrequency.Yearly, | ||
//--let's pretend this content was changed a year ago | ||
TimeStamp = DateTime.UtcNow.AddYears(-1), | ||
//--these pages are super low priority | ||
Priority = .1 | ||
}).ToList(); | ||
|
||
//--combine the urls into one big list. These could of course bet kept seperate and two different sitemap index files could be generated if we wanted | ||
allUrls.AddRange(miscellaneousLowPriorityUrls); | ||
|
||
return allUrls; | ||
} | ||
|
||
private IReadOnlyCollection<string> GetMiscellaneousLowPriorityUrls(string domain) | ||
{ | ||
var result = new List<string>(); | ||
|
||
for (int i = 0; i < 40000; i++) | ||
{ | ||
result.Add($"https://{domain}/page/{i}.html"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private IReadOnlyCollection<string> GetHighPriorityProductPageUrls(string domain) | ||
{ | ||
var result = new List<string>(); | ||
|
||
for (int i = 0; i < 10000; i++) | ||
{ | ||
result.Add($"https://{domain}/priority-page/{i}.html"); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\X.Web.Sitemap\X.Web.Sitemap.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
101 changes: 0 additions & 101 deletions
101
src/X.Web.Sitemap.Examples/SitemapGenerationWithSitemapIndexExample.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace X.Web.Sitemap; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.