-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
253 lines (223 loc) · 11.1 KB
/
Copy pathProgram.cs
File metadata and controls
253 lines (223 loc) · 11.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using CommandLine;
using Kaltura.Enums;
using Kaltura.Types;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NLog;
using NLog.Extensions.Logging;
using System.Dynamic;
namespace AddZoomMetadata
{
class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private static List<MediaEntry> lstEntries = new();
private static int lastTimestamp = 0; // Keep track of the last timestamp processed. At the end, save
// it to the config file. Kaltura uses UNIX time.
public static IConfiguration config;
public static DateTime startDate = DateTime.MinValue;
// The Kaltura API does not like filtering on times significantly before the start
// date of your Kaltura service. Figure out the date of the first entry in your
// instance and add it to the config file.
public static DateTime serviceStartDate = DateTime.MinValue;
static void Main(string[] args)
{
config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false)
.AddEnvironmentVariables()
.Build();
serviceStartDate = DateTime.Parse(config["Settings:serviceStartDate"]);
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));
logger.Info("Program start");
// Kaltura API credentials are read from Environment variables.
int partnerId;
try
{
partnerId = int.Parse(config.GetValue<string>("KALTURA_PID"));
} catch
{
throw new ApplicationException("Error getting environment variable: KALTURA_PID");
}
string kalturaSecret = config.GetValue<string>("KALTURA_SECRET");
if (string.IsNullOrEmpty(kalturaSecret))
{
throw new ApplicationException("Error getting environment variable: KALTURA_SECRET");
}
string uid = Environment.UserName;
if (args.Count() > 0)
{
ParserResult<Options> opt = Parser.Default.ParseArguments<Options>(args)
.WithParsed(parseOptions)
.WithNotParsed(handleParseError);
} else
{
// Start date not specifiec on command line. Read last time saved in config file.
lastTimestamp = int.Parse(config["Settings:startTimestamp"]);
}
// See comment on serviceStartDate above.
lastTimestamp = Math.Max(lastTimestamp, KochKalturaUtilities.util.dotNetToUnixTime(serviceStartDate));
Kaltura.Client client = KochKalturaUtilities
.ClientUtilities
.createKalturaClient(partnerId, kalturaSecret, 86400, uid, logger);
// All Zoom recordings will be added to this category if they're not already there.
string targetCategory = config["Settings:targetCategory"];
int targetCategoryId = -1;
try
{
targetCategoryId = KochKalturaUtilities.CategoryUtilities.getCategoryByFullname(client, targetCategory);
}
catch (Kaltura.APIException ex)
{
logger.Error($"Failed to get the ID of the target category: {targetCategory}." +
$" Error={KochKalturaUtilities.util.formatExceptionString(ex)};");
Environment.Exit(5);
}
string metadataProfileName = config["Settings:metadataProfileName"];
int targetMetadataProfileId = -1;
try
{
targetMetadataProfileId = KochKalturaUtilities.MetadataUtilities.getProfileIdByName(client, metadataProfileName);
}
catch (ApplicationException ex)
{
logger.Error(ex, $"Failed to get target metadata profile ID: Name={metadataProfileName};" +
$"Error={KochKalturaUtilities.util.formatExceptionString(ex)};");
Environment.Exit(6);
}
FilterPager pager = new FilterPager()
{
PageIndex = 1,
PageSize = 500
};
MetadataFilter metaFilt = new MetadataFilter()
{
MetadataObjectTypeEqual = MetadataObjectType.ENTRY,
MetadataProfileIdEqual = targetMetadataProfileId
};
// Setup done. Processing starts here ...
int totalEntries = 0;
int entriesSoFar = 0;
bool donePaging = false;
logger.Info($"Processing starts at {KochKalturaUtilities.util.unixToDotNetTime(lastTimestamp)} ({lastTimestamp})");
// Process all media entries (filtered) that have the "zoomentry" admin tag.
try
{
MediaEntryFilter mediaFilter = new MediaEntryFilter();
mediaFilter.OrderBy = MediaEntryOrderBy.CREATED_AT_ASC;
mediaFilter.CreatedAtGreaterThanOrEqual = lastTimestamp;
mediaFilter.AdminTagsLike = "zoomentry";
while (!donePaging)
{
List<MediaEntry> pageOfEntries = KochKalturaUtilities.MediaUtilities.getPageOfEntries(
client,
logger,
ref mediaFilter,
ref pager,
ref totalEntries,
ref entriesSoFar,
ref lastTimestamp,
ref donePaging);
if (pageOfEntries == null)
{
donePaging = true;
continue;
}
logger.Info("================== Page of Media Entries ==================" + KochKalturaUtilities.util.unixToDotNetTime(lastTimestamp).ToString("yyyy-MM-dd HH:mm tt"));
foreach (MediaEntry entr in pageOfEntries)
{
try
{
logger.Info($"{entr.Id}:");
KochKalturaUtilities.MetadataUtilities.addZoomTag(client, logger, entr);
KochKalturaUtilities.MetadataUtilities.addCustomMetadata(client,
logger,
metaFilt,
entr,
targetMetadataProfileId);
KochKalturaUtilities.CategoryEntryUtilities.addEntryToCategory(client,
logger,
entr.Id,
targetCategoryId);
}
catch (Exception ex)
{
logger.Error(KochKalturaUtilities.util.formatExceptionString(ex));
}
lastTimestamp = entr.CreatedAt;
} // end foreach item in page of media entries.
} // while paging
}
catch (Kaltura.APIException ex)
{
logger.Error(ex, "The AddZoomTag task encountered the following exception and exited.\n{0}",
KochKalturaUtilities.util.formatExceptionString(ex));
// In case of an exception, bump the last timestamp by one. Hopefully we'll
// get past the offending entry next time the code is run.
lastTimestamp++;
} catch (Exception ex)
{
logger.Error(ex, "Unhandled exception: Exception={0};", KochKalturaUtilities.util.formatExceptionString(ex));
throw;
}
finally
{
saveSettings();
logger.Info("Program exit");
LogManager.Shutdown();
}
}
static void saveSettings()
{
var appSettingsPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "appsettings.json");
var json = File.ReadAllText(appSettingsPath);
JsonSerializerSettings? jsonSettings = new();
jsonSettings.Converters.Add(new ExpandoObjectConverter());
jsonSettings.Converters.Add(new StringEnumConverter());
dynamic newconfig = JsonConvert.DeserializeObject<ExpandoObject>(json, jsonSettings);
if (newconfig != null)
{
newconfig.Settings.LastCreatedTime = lastTimestamp;
var newJson = JsonConvert.SerializeObject(newconfig, Formatting.Indented, jsonSettings);
File.WriteAllText(appSettingsPath, newJson);
logger.Info($"last timestamp saved to {appSettingsPath}: {lastTimestamp}");
}
}
public static void parseOptions(Options opts)
{
if (string.IsNullOrEmpty(opts.strDate) == false)
{
try
{
startDate = DateTime.Parse(opts.strDate);
if (startDate < serviceStartDate)
{
logger.Error($"Dates before {serviceStartDate} not supported.");
Environment.Exit(1);
}
}
catch
{
logger.Error($"Invalid date format supplied on command line: { opts.strDate}");
Environment.Exit(2);
}
lastTimestamp = KochKalturaUtilities.util.dotNetToUnixTime(startDate);
}
else
{
// Start date not specified on command line. Read the last timestamp saved
// in the config file.
lastTimestamp = int.Parse(config["Settings:startTimestamp"]);
}
}
static void handleParseError(IEnumerable<Error> errors)
{
foreach (UnknownOptionError err in errors)
{
logger.Error($"{err.ToString()}. Unknown tag={err.Token};");
}
Environment.Exit(1);
}
}
}