Skip to content

Instantly share code, notes, and snippets.

@nozzlegear
Last active January 21, 2024 01:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nozzlegear/0d42673f64e5c9a9862a to your computer and use it in GitHub Desktop.
Save nozzlegear/0d42673f64e5c9a9862a to your computer and use it in GitHub Desktop.
Converting a .ics file to JSON, parsing it and using the data
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace YourNamespace
{
public class YourClass
{
public async Task<string> YourMethod()
{
//Prepare url
string calUrl = "http://spacexstats.com/calendar.php?launch=all";
string conversionUrl = String.Format("http://ical-to-json.herokuapp.com/convert.json?url={0}", calUrl);
//Prepare web request
HttpWebRequest req = WebRequest.Create(conversionUrl) as HttpWebRequest;
req.Method = "GET";
try
{
//Get request
using (StreamReader reader = new StreamReader((await req.GetResponseAsync()).GetResponseStream()))
{
//Get data
string data = await reader.ReadToEndAsync();
//Deserialize data into our ical class
List<iCalendar> calendars = JObject.Parse(data).GetValue("calendars").ToObject<List<iCalendar>>();
//Get first calendar
var calendar = calendars.FirstOrDefault();
//Ensure calendar is not null
if (calendar != null)
{
//Get the first event ordered by start date
var calEvent = calendar.Events.OrderBy(x => x.Start.Value).FirstOrDefault();
//Ensure event is not null
if (calEvent != null)
{
// TODO: Check if stamp, start and end have values ( if(calEvent.Start.HasValue) { doSomething() } )
//"Event found. Summary: AsiaSat 6 Launch. Description: AsiaSat 6 is a communications satellite being launched for Asia Satellite Telecommunications Company Ltd. Once in orbit, it will be renamed AsiaSat 6 / Thaicom 7.. Start Date: 8/26/2014 11:50:00 PM. End Date: 8/27/2014 12:50:00 AM."
return String.Format("Event found. Summary: {0}. Description: {1}. Start Date: {2}. End Date: {3}.", calEvent.Summary, calEvent.Description, calEvent.Start.Value, calEvent.End);
}
else
{
return "No events found.";
}
}
else
{
return "No calendars found.";
}
}
}
catch (WebException webex)
{
// TODO: Handle exception
return webex.Message;
}
catch (Exception ex)
{
// TODO: Handle exception
return ex.Message;
}
}
private class iCalendar
{
public string Version { get; set; }
public string ProdId { get; set; }
public List<iCalendarEvent> Events { get; set; }
}
private class iCalendarEvent
{
private string _dtStamp { get; set; }
private string _dtStart { get; set; }
private string _dtEnd { get; set; }
[JsonProperty("uid")]
public string UniqueId { get; set; }
public string DTStamp
{
get
{
return _dtStamp;
}
set
{
_dtStamp = value;
Stamp = this.ParseStringToDate(value);
}
}
public string DTStart
{
get
{
return _dtStart;
}
set
{
_dtStart = value;
Start = this.ParseStringToDate(value);
}
}
public string DTEnd
{
get
{
return _dtEnd;
}
set
{
_dtEnd = value;
End = this.ParseStringToDate(value);
}
}
public DateTime? Stamp { get; set; }
public DateTime? Start { get; set; }
public DateTime? End { get; set; }
public string Summary { get; set; }
public string Description { get; set; }
public string Location { get; set; }
private DateTime? ParseStringToDate(string value)
{
//Ensure value exists
if (!String.IsNullOrEmpty(value))
{
var date = new DateTime();
//This is a simple datetime parser. According to the iCal standard, a DateTime string ending in 'Z' indicates that the datetime is in UTC rather than local time. This code assumes the date is in UTC.
if (DateTime.TryParseExact(value.ToLower().Replace("t", " ").Replace("z", ""), "yyyyMMdd HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out date))
{
return date;
}
else
{
return null;
}
}
else
{
return null;
}
}
}
}
}
@h586620
Copy link

h586620 commented Apr 6, 2022

Is it possible to use a ical url instead of a ics file? And write it in a json file or js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment