Code · 162 lines · 3341 bytes
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
158
159
160
161
162//
// (c) 2022,2023 Alphons van der Heijden
//
// https://opendata.tweedekamer.nl/documentatie/
// https://opendata.tweedekamer.nl/documentatie/syncfeed-api
//

using MongoDB.MvcCore;
using System.Xml.Linq;

namespace OpenKamer.LogicControllers;

public class FeedController
{
	private static readonly string FEED = "https://gegevensmagazijn.tweedekamer.nl/SyncFeed/2.0/";

	private string FilesDirectory;

	public FeedController(string FilesDirectory, string Token)
	{
		this.FilesDirectory = FilesDirectory;
		this.Token = Token;
		this.Status = string.Empty;
	}

	public event EventHandler? OnLog;

	public string Status { get; set; }

	public string Token { get; set; }

	public int FilesTotal { get; set; }

	public int FilesIndex { get; set; }

	private void Log(string message)
	{
		OnLog?.Invoke(message, new EventArgs());
	}

	async public Task SyncFeedAsync(CancellationToken cancellationToken)
	{
		Log($"MongoDB.MvcCore.BsonJsonSerializer: {typeof(BsonJsonSerializer).Assembly.GetName().Version}");

		Log("SyncFeedAsync");

		HttpClient httpclient = new();

		XDocument xDoc;

		XNamespace ns = "http://www.w3.org/2005/Atom";

		string? urlFeed = FEED;

		if (string.IsNullOrWhiteSpace(Token) == false)
			urlFeed += "?skiptoken=" + this.Token;

		if (string.IsNullOrWhiteSpace(this.Token))
			this.Token = "0";

		var list = Directory
			.GetFiles(this.FilesDirectory, "*.xml")
			.Select(x => long.Parse(Path.GetFileNameWithoutExtension(x)))
			.OrderBy(x => x)
			.Select(x => x.ToString())
			.ToList();

		this.FilesIndex = 0;
		this.FilesTotal = list.Count - list.IndexOf(this.Token);

		list.Clear();
		GC.Collect();

		do
		{
			var FileName = FilesDirectory + @"\" + this.Token + ".xml";

			if (File.Exists(FileName))
			{
				Status = "cached";
			}
			else
			{
				var xml = await httpclient.GetStringAsync(urlFeed, cancellationToken);

				await File.WriteAllTextAsync(FileName, xml, cancellationToken);

				Status = "saved";
			}

			var textReader = File.OpenText(FileName);

			xDoc = await XDocument.LoadAsync(textReader, LoadOptions.None, cancellationToken);

			textReader.Dispose();

			var links = xDoc.Descendants(ns + "link");

			var nodeSelf = links.FirstOrDefault(x => x.Attribute("rel")?.Value == "self");

			var urlSelf = nodeSelf?.Attribute("href")?.Value;

			var nodeResume = links.FirstOrDefault(x => x.Attribute("rel")?.Value == "resume");

			var nodeNext = links.FirstOrDefault(x => x.Attribute("rel")?.Value == "next");

			urlFeed = nodeNext?.Attribute("href")?.Value;

			if(urlSelf == null)
			{
				Log("Self error, exit");
				return;
			}

			if(this.Token!= "0" && !urlSelf.EndsWith(this.Token))
			{
				Log("Incomplete, exit");
				return;
			}

			if (nodeResume != null)
			{
				if (Status == "cached")
				{
					File.Delete(FileName);
					continue;
				}
				if (Status == "saved")
				{
					Log("Resume exit");
					return;
				}
			}

			if (nodeNext == null)
			{
				Log("Normal exit");
				return;
			}

			if (urlFeed == null)
			{
				Log("Abnormal feed exit");
				return;
			}

			var intI = urlFeed.IndexOf("skiptoken=");
			if (intI < 0)
			{
				Log("Abnormal skiptoken exit");
				return;
			}

			this.Token = urlFeed[(intI + 10)..];

			this.FilesIndex++;

		} while (!string.IsNullOrWhiteSpace(this.Token));

		Log("Abnormal empty skiptoken exit");
	}
}