-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContentService.cs
More file actions
157 lines (142 loc) · 4.4 KB
/
ContentService.cs
File metadata and controls
157 lines (142 loc) · 4.4 KB
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.Graph.Models.ExternalConnectors;
using System.Xml.Serialization;
[XmlRoot("rss")]
public class RssFeed
{
[XmlElement("channel")]
public RssChannel? Channel { get; set; }
}
public class RssChannel
{
[XmlElement("title")]
public string? Title { get; set; }
[XmlElement("link")]
public string? Link { get; set; }
[XmlElement("description")]
public string? Description { get; set; }
public DateTime LastBuildDate { get; set; }
[XmlElement("item")]
public List<RssItem>? Items { get; set; }
}
public class RssItem
{
[XmlElement("guid")]
public string? Guid { get; set; }
[XmlElement("link")]
public string? Link { get; set; }
[XmlElement("category")]
public List<string>? Categories { get; set; }
[XmlElement("title")]
public string? Title { get; set; }
[XmlElement("description")]
public string? Description { get; set; }
public DateTime PublishedDate { get; set; }
[XmlElement("updated", Namespace = "http://www.w3.org/2005/Atom")]
public DateTime? Updated { get; set; }
}
static class ContentService
{
/// <summary>
/// Load the Rss Feed channel's items and extract information from them.
/// </summary>
/// <returns></returns>
static async Task<List<RssItem>> Extract()
{
var rssFeedRequest = await new HttpClient().GetAsync($"{Configuration.FeedBaseUrl}/RoadmapFeatureRSS");
string xml = await rssFeedRequest.Content.ReadAsStringAsync();
var rssItems = new List<RssItem>();
XmlSerializer serializer = new XmlSerializer(typeof(RssFeed));
using (TextReader reader = new StringReader(xml))
{
RssFeed? feed = (RssFeed?)serializer?.Deserialize(reader);
if (feed != null && feed.Channel != null && feed.Channel.Items != null)
{
foreach (var item in feed.Channel.Items)
{
try
{
rssItems.Add(item);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
return rssItems;
}
/// <summary>
/// Converts Rss Feed Items into external items from Microsoft Graph.
/// </summary>
/// <param name="content"></param>
/// <returns></returns> <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
static IEnumerable<ExternalItem> Transform(List<RssItem> rssListItems)
{
return rssListItems.Select(a =>
{
return new ExternalItem
{
Id = a.Guid,
Properties = new()
{
AdditionalData = new Dictionary<string, object> {
{ "title", a.Title ?? "" },
{ "description", a.Description ?? "" },
{ "url", a.Link ?? "" },
{ "iconUrl", a.Link ?? "" }
}
},
Content = new()
{
Value = a.Description ?? "",
Type = ExternalItemContentType.Text
},
Acl = new()
{
new()
{
Type = AclType.Everyone,
Value = "everyone",
AccessType = AccessType.Grant
}
}
};
});
}
/// <summary>
/// load the transformed external items into Microsoft 365.
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
static async Task Load(IEnumerable<ExternalItem> items)
{
foreach (var item in items)
{
Console.Write(string.Format("Loading item {0}...", item.Id));
try
{
await GraphService.Client.External
.Connections[Uri.EscapeDataString(ConnectionConfiguration.ExternalConnection.Id!)]
.Items[item.Id]
.PutAsync(item);
Console.WriteLine("DONE");
}
catch (Exception ex)
{
Console.WriteLine("ERROR");
Console.WriteLine(ex.Message);
}
}
}
public static async Task LoadContent()
{
var rssListItems = await Extract();
var transformed = Transform(rssListItems);
await Load(transformed);
}
}