-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.cs
100 lines (91 loc) · 3.44 KB
/
Extension.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using DiscordBot.Models;
using Ical.Net;
using Microsoft.Extensions.Configuration;
namespace DiscordBot
{
public static class Extension
{
/// <summary>
/// Async method to load ical calendar from feed
/// </summary>
/// <param name="calendar"></param>
/// <param name="uri">the url</param>
/// <returns><seealso cref="Calendar"/>Calendar</returns>
public static async Task<Calendar> LoadFromUriAsync(this Calendar calendar, Uri uri)
{
using (var client = new HttpClient())
using (var response = await client.GetAsync(uri))
{
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return Calendar.Load(result);
}
}
/// <summary>
/// method to load ical calendar from feed
/// </summary>
/// <param name="calendar"></param>
/// <param name="uri">the url</param>
/// <returns><seealso cref="Calendar"/>Calendar</returns>
public static Calendar LoadFromUri(this Calendar calendar, Uri uri)
{
using (var client = new HttpClient())
using (var response = client.GetAsync(uri).Result)
{
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsStringAsync().Result;
return Calendar.Load(result);
}
}
/// <summary>
/// Method to create a list of tasks
/// </summary>
/// <param name="taskList"></param>
/// <returns></returns>
public static string ShowAll(this List<Tasks> taskList)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (Tasks task in taskList)
{
stringBuilder.Append($"- [{task.Course}] {task.Title} tenlaatste {task.EndTime:dd/MM/yyy HH:mm}\r\n");
}
if (!taskList.Any()) stringBuilder.Append("No tasks");
return stringBuilder.ToString();
}
public static Tasks GetLatestTask(this List<Tasks> newList, List<Tasks> oldList)
{
return newList.First(tasks => !oldList.Contains(tasks));
}
public static List<Tasks> ApplyBlacklist(this List<Tasks> taskList)
{
List<Tasks> retList = new List<Tasks>();
string[] excludeList = Program.ThisProgram.GetConfig().GetSection("exclude").AsEnumerable().Where(p => p.Value != null).Select(p => p.Value).ToArray();
foreach (Tasks task in taskList)
{
bool isValid = excludeList.All(bl => !task.Course.ToLower().Contains(bl));
if (isValid)
{
retList.Add(task);
}
}
return retList;
}
/// <summary>
/// Discord AddField message cannot be longer than 1024 characters
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string LimitMessage(this string message)
{
if (message.Length > 1000)
return message.Substring(0, 1000)+"...";
return message;
}
}
}