This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
/
GetBlogFeeds.cs
157 lines (145 loc) · 7.59 KB
/
GetBlogFeeds.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.SyndicationFeed;
using Microsoft.SyndicationFeed.Atom;
using Microsoft.SyndicationFeed.Rss;
using Statiq.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml;
namespace DotnetFoundationWeb
{
public class GetBlogFeeds : ParallelModule
{
private DateTimeOffset _recent;
protected override void BeforeExecution(IExecutionContext context)
{
_recent = new DateTimeOffset(
DateTime.Today.AddHours(context.ApplicationState.IsCommand("preview") || context.Settings.GetBool("dev") ? -168 : -48));
}
protected override async Task<IEnumerable<IDocument>> ExecuteInputAsync(IDocument input, IExecutionContext context)
{
string feed = input.GetString("Feed");
if (!string.IsNullOrEmpty(feed))
{
try
{
// Download the feed
context.LogInformation($"Getting feed for {feed}");
Uri website = null;
string title = null;
string author = null;
string description = null;
string image = null;
List<ISyndicationItem> items = new List<ISyndicationItem>();
using (HttpClient httpClient = context.CreateHttpClient())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", "Wyam");
var response = await httpClient.GetAsync(feed);
if (response.StatusCode == HttpStatusCode.Redirect ||
response.StatusCode == HttpStatusCode.MovedPermanently)
{
context.LogWarning($"Attempting to follow redirect for {feed}");
feed = response.Headers.Location.OriginalString;
response = await httpClient.GetAsync(feed);
}
if (response.IsSuccessStatusCode)
{
using (Stream stream = await response.Content.ReadAsStreamAsync())
using (StreamReader streamReader = new XmlStreamReader(stream))
using (XmlReader xmlReader = XmlReader.Create(streamReader, new XmlReaderSettings { Async = true, DtdProcessing = DtdProcessing.Ignore }))
{
xmlReader.MoveToContent();
bool atom = xmlReader.Name == "feed";
context.LogInformation($"Reading {feed} as " + (atom ? "Atom" : "RSS"));
XmlFeedReader feedReader = atom
? (XmlFeedReader)new AtomFeedReader(xmlReader, new FixedAtomParser())
: new RssFeedReader(xmlReader);
while (await feedReader.Read())
{
try
{
switch (feedReader.ElementType)
{
case SyndicationElementType.Person:
ISyndicationPerson person = await feedReader.ReadPerson();
if (person.RelationshipType == "author")
{
author = person.Name ?? person.Email;
}
break;
case SyndicationElementType.Image:
ISyndicationImage img = await feedReader.ReadImage();
image = img.Url.ToString();
break;
case SyndicationElementType.Link:
ISyndicationLink link = await feedReader.ReadLink();
website = link.Uri;
break;
case SyndicationElementType.Item:
ISyndicationItem item = await feedReader.ReadItem();
items.Add(item);
break;
case SyndicationElementType.None:
break;
default:
ISyndicationContent content = await feedReader.ReadContent();
if (string.Equals(content.Name, "title", StringComparison.OrdinalIgnoreCase))
{
title = content.Value;
}
else if (string.Equals(content.Name, "description", StringComparison.OrdinalIgnoreCase)
|| string.Equals(content.Name, "subtitle", StringComparison.OrdinalIgnoreCase))
{
description = content.Value;
}
break;
}
}
catch (Exception ex)
{
context.LogWarning($"Exception while processing {feedReader.ElementType} in {feed}: {ex.Message}");
}
}
}
}
// Get a new document with feed metadata
MetadataItems metadata = new MetadataItems();
if (items.Count > 0)
{
BlogFeedItem[] feedItems = items
.Select(x => new BlogFeedItem(x, _recent, website))
.OrderByDescending(x => x.Published)
.Take(50) // Only take the 50 most recent items
.ToArray();
metadata.Add(SiteKeys.BlogFeedItems, feedItems);
return input.Clone(metadata).Yield();
}
}
}
catch (Exception ex)
{
context.LogWarning($"Error getting feed for {feed}: {ex.Message}");
}
}
else
{
var speaker = input.GetString("Title");
if (!string.IsNullOrWhiteSpace(speaker))
{
context.LogInformation($"No RSS specified for {speaker}.");
}
else
{
context.LogInformation($"No RSS specified for unknown speaker.");
}
}
return input.Yield();
}
}
}