Created
July 15, 2020 12:59
-
-
Save jakoss/544888019d9f440ab780990f60d4dcf5 to your computer and use it in GitHub Desktop.
Automated todoist tasks creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
using var client = new HttpClient(); | |
client.DefaultRequestHeaders.Add("Authorization", "Bearer <integration_token_here>"); | |
var dates = new List<DateTime>(); | |
for (int year = 2020; year <= 2023; year++) | |
{ | |
for (int i = 1; i <= 12; i++) | |
{ | |
for (int j = 1; j <= 7; j++) | |
{ | |
var day = new DateTime(year, i, j); | |
if (day.DayOfWeek == DayOfWeek.Thursday && day > DateTime.Now) | |
{ | |
dates.Add(day); | |
break; | |
} | |
} | |
} | |
} | |
// let's make sure we don't want to make more than 50 requests per minut (todoist api limitation) | |
Debug.Assert(dates.Count <= 50); | |
var months = new string[] { | |
"Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień" | |
}; | |
foreach (var date in dates) | |
{ | |
// set reminder for 5 hours earlier | |
var relativeReminderDate = date.AddHours(-5); | |
// this is very important for timezones handling | |
var utcReminderDate = relativeReminderDate.ToUniversalTime(); | |
var taskName = $"Odpady segregowane ({months[date.Month - 1]}/{date.Year})"; | |
var dateAsRfc3339String = XmlConvert.ToString(utcReminderDate, XmlDateTimeSerializationMode.RoundtripKind); | |
var json = JsonSerializer.Serialize(new Task(taskName, <project_id_here>, dateAsRfc3339String)); | |
var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); | |
var response = client.PostAsync("https://api.todoist.com/rest/v1/tasks", httpContent).Result; | |
response.EnsureSuccessStatusCode(); | |
} | |
} | |
class Task | |
{ | |
public Task(string content, uint projectId, string dueDateTime) | |
{ | |
Content = content; | |
ProjectId = projectId; | |
DueDateTime = dueDateTime; | |
} | |
[JsonPropertyName("content")] | |
public string Content { get; } | |
[JsonPropertyName("project_id")] | |
public uint ProjectId { get; } | |
[JsonPropertyName("due_datetime")] | |
public string DueDateTime { get; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment