]> fortfriendship.online Git - gnargle.github.io.git/blob - RSSGen/Program.cs
a574f9a89c45e00a43bae23480ddbf81569b2984
[gnargle.github.io.git] / RSSGen / Program.cs
1 using OpenGraphNet;
2 using RssFeedGenerator;
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Runtime.InteropServices;
7
8 // See https://aka.ms/new-console-template for more information
9 Console.WriteLine("Scanning entries folder for latest files");
10
11 var folder = String.Empty;
12
13 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
14 {
15 folder = Path.Combine(Directory.GetCurrentDirectory(), "../entries");
16 }
17 else
18 {
19 folder = Path.Combine(Directory.GetCurrentDirectory(), "../../../../entries");
20 }
21 var filePaths = Directory.EnumerateFiles(folder);
22 var fileInfos = new List<FileInfo>();
23
24 if (filePaths.Any())
25 {
26 foreach (var path in filePaths)
27 {
28 if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase))
29 continue;
30 var fInfo = new FileInfo(path);
31 fileInfos.Add(fInfo);
32 }
33 }
34
35 Console.WriteLine("Scanning projects folder for latest files");
36
37 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
38 {
39 folder = Path.Combine(Directory.GetCurrentDirectory(), "../projects");
40 }
41 else
42 {
43 folder = Path.Combine(Directory.GetCurrentDirectory(), "../../../../projects");
44 }
45
46 filePaths = Directory.EnumerateFiles(folder);
47
48 if (filePaths.Any())
49 {
50 foreach (var path in filePaths)
51 {
52 var fInfo = new FileInfo(path);
53 fileInfos.Add(fInfo);
54 }
55 }
56
57 fileInfos = fileInfos.OrderByDescending(f => f.CreationTimeUtc).Take(10).ToList();
58
59 var myRSS = new rss();
60
61 myRSS.version = 2.0m;
62
63 myRSS.channel = new rssChannel
64 {
65 title = "athene.gay entries",
66 description = "blog entries for athene.gay",
67 language = "en-GB",
68 link = "https://athene.gay",
69 item = new List<rssChannelItem>(),
70 link1 = new link
71 {
72 href = "https://athene.gay/feed.xml",
73 rel = "self",
74 type = "application/rss+xml",
75 }
76 };
77
78 foreach (var file in fileInfos)
79 {
80 var htmlString = File.ReadAllText(file.FullName);
81 OpenGraph graph = OpenGraph.ParseHtml(htmlString);
82 var item = new rssChannelItem()
83 {
84 title = graph.Title,
85 description = graph.Metadata["og:description"].First(),
86 pubDate = file.CreationTimeUtc.ToString("r"),
87 };
88 if (file.FullName.Contains("entries"))
89 {
90 item.link = "https://athene.gay/entries/" + Path.GetFileName(file.Name);
91 }
92 else if (file.FullName.Contains("projects"))
93 {
94 item.link = "https://athene.gay/projects/" + Path.GetFileName(file.Name);
95 }
96 item.guid = new rssChannelItemGuid()
97 {
98 isPermaLink = true,
99 Value = item.link
100 };
101 myRSS.channel.item.Add(item);
102 }
103
104 var output = Generator.SerializeRSS(myRSS);
105
106 var rssPath = String.Empty;
107
108 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
109 {
110 rssPath = Path.Combine(Directory.GetCurrentDirectory(), "../feed.xml");
111 }
112 else
113 {
114 rssPath = Path.Combine(Directory.GetCurrentDirectory(), "../../../../feed.xml");
115 }
116
117 if (File.Exists(rssPath))
118 {
119 File.Delete(rssPath);
120 }
121
122 Console.WriteLine("RSS generated, outputting to console and file");
123 Console.WriteLine(output);
124
125 File.WriteAllText(rssPath, output);