This repository was archived by the owner on Feb 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediathekClient.cs
More file actions
200 lines (185 loc) · 5.53 KB
/
MediathekClient.cs
File metadata and controls
200 lines (185 loc) · 5.53 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using QueryData;
using ResultData;
public class MediathekClient
{
public MediathekClient() => _query = new Query();
public MediathekClient searchTitle(string value)
{
_query.addFieldAndQuery("title", value);
return this;
}
public MediathekClient searchTitleOrTopic(string value)
{
_query.addFieldAndQuery("title", "topic", value);
return this;
}
public MediathekClient searchTopic(string value)
{
_query.addFieldAndQuery("topic", value);
return this;
}
public MediathekClient searchChannel(Channel channel)
{
_query.addFieldAndQuery("channel", channel == Channel.DreiSat ? "3sat" : Enum.GetName<Channel>(channel));
return this;
}
public MediathekClient searchDuration(string value)
{
_query.addFieldAndQuery("duration", value);
return this;
}
public MediathekClient searchId(string value)
{
_query.addFieldAndQuery("id", value);
return this;
}
public MediathekClient withFutureMedia(bool value)
{
_query.future = value;
return this;
}
public MediathekClient setOffset(int value)
{
_query.offset = value;
return this;
}
public MediathekClient setMaximumResults(int value)
{
_query.size = value;
return this;
}
public MediathekClient orderResultsBy(string value, SortOrder order = SortOrder.asc)
{
_query.sortBy = value;
_query.sortOrder = Enum.GetName<SortOrder>(order);
return this;
}
public string title
{
set => _query.addFieldAndQuery("title", value);
}
public string topic
{
set => _query.addFieldAndQuery("topic", value);
}
public Channel channel
{
set => _query.addFieldAndQuery("channel", value == Channel.DreiSat ? "3sat" : Enum.GetName<Channel>(value));
}
public int duration
{
set => _query.addFieldAndQuery("duration", value.ToString());
}
public string id
{
set => _query.addFieldAndQuery("id", value);
}
public bool future
{
set => _query.future = value;
}
public int offset
{
set => _query.offset = value;
}
public int size
{
set => _query.size = value;
}
public async Task<MediathekResult> sendQuery()
{
try
{
return await ProcessQuery(_query);
}
catch (Exception e)
{
return new MediathekResult { err = e.Message };
}
}
private Query _query;
private static readonly HttpClient client = new HttpClient();
private static async Task<MediathekResult> ProcessQuery(Query query)
{
JsonSerializerOptions querySerializerOptions = new JsonSerializerOptions { IgnoreNullValues = true };
var text = JsonSerializer.Serialize(query, querySerializerOptions);
var httpContent = new StringContent(text, Encoding.UTF8, "text/plain");
var streamTask = await client.PostAsync("https://mediathekviewweb.de/api/query", httpContent);
if (streamTask.IsSuccessStatusCode)
{
String stringResult = await streamTask.Content.ReadAsStringAsync();
JsonSerializerOptions resultSerializerOptions = new JsonSerializerOptions();
resultSerializerOptions.IncludeFields = true;
resultSerializerOptions.Converters.Add(new Custom.DateTimeConverter());
resultSerializerOptions.Converters.Add(new Custom.UInt32Converter());
MediathekResult res = null;
try
{
res = JsonSerializer.Deserialize<MediathekResult>(stringResult, resultSerializerOptions);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return res;
}
return new MediathekResult { err = streamTask.ReasonPhrase };
}
}
namespace Custom
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
DateTimeOffset.FromUnixTimeSeconds(reader.GetInt32()).DateTime;
public override void Write(
Utf8JsonWriter writer,
DateTime dateTimeValue,
JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class UInt32Converter : JsonConverter<UInt32?>
{
public override UInt32? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
UInt32 number;
try
{
return reader.GetUInt32();
}
catch
{
if (UInt32.TryParse(reader.GetString(), out number))
{
return number;
}
return null;
}
}
public override void Write(Utf8JsonWriter writer, uint? value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
// public override void Write(
// Utf8JsonWriter writer,
// DateTime dateTimeValue,
// JsonSerializerOptions options)
// {
// throw new NotImplementedException();
// }
}
}