Example #1
0
        private static bool ParseFacebookEventListing(string URL, string EventContents, out EventInformation EventInfo)
        {
            string StartDate;
            string EndDate;

            EventInfo          = new EventInformation();
            EventInfo.Location = GetEventLocation(EventContents);

            EventInfo.Title = GetEventTitle(EventContents);
            if (string.IsNullOrEmpty(EventInfo.Title))
            {
                return(false);
            }

            EventInfo.Description = "Source: " + URL + "\n";

            if (!GetEventDates(EventContents, out StartDate, out EndDate))
            {
                return(false);
            }

            EventInfo.SetStartDate(StartDate);
            if (string.IsNullOrEmpty(EndDate))
            {
                //
                // If we don't have an explicit end date time just add an hour.
                //
                EventInfo.SetEndDate((DateTime.Parse(StartDate)).Add(new TimeSpan(1, 0, 0)).ToString());
            }
            else
            {
                EventInfo.SetEndDate(EndDate);
            }

            return(true);
        }
Example #2
0
        static void Main(string[] args)
        {
            UserCredential   credential;
            string           CalendarID      = "primary";
            EventInformation EventInfo       = new EventInformation();
            string           EventUrl        = null;
            bool             ParseOnly       = false; // If this is true we parse the event URL and display its info but don't add it to the calendar.
            List <string>    NakedParameters = null;
            bool             FlagsPassed     = false; // If we don't see any arguments then we act like the classic "cal" command

            // TODO -- https://www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-console-app.html
            using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) // TODO -- Make this a resource
            {
                string credPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json"); // TODO

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
            }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            // TODO -- Check for required arguments
            // TODO -- Do some sanity checking (end >= start, end not specified if all-day, ...)
            // TODO -- A date string like "2018-01-28" will get round-tripped (through DateTime.Parse()) as having an explicit time of 12AM
            OptionSet options = new OptionSet();

            options.Add("?|h|help", value => { PrintUsage(); });
            options.Add("c|calendar=", value => { CalendarID = FindCalendarByName(service, value); if (CalendarID == null)
                                                  {
                                                      PrintUsage("Couldn't find specified calendar!");
                                                  }
                                                  FlagsPassed = true; });
            options.Add("d|description=", value => { EventInfo.Description = value; FlagsPassed = true; });
            options.Add("e|end=", value => { EventInfo.SetEndDate(value); FlagsPassed = true; });
            options.Add("p|parse-only", value => { ParseOnly = true; FlagsPassed = true; });
            options.Add("r|reminder=", value => { EventInfo.AddReminder(value); FlagsPassed = true; });
            options.Add("s|start=", value => { EventInfo.SetStartDate(value); FlagsPassed = true; });
            options.Add("t|title=", value => { EventInfo.Title = value; FlagsPassed = true; });
            options.Add("u|url=", value => { EventUrl = value; FlagsPassed = true; });
            options.Add("w|where=", value => { EventInfo.Location = value; FlagsPassed = true; });

            NakedParameters = options.Parse(args);

            if (FlagsPassed)
            {
                if (!String.IsNullOrEmpty(EventUrl))
                {
                    HandleEventURL(service, CalendarID, EventUrl, ParseOnly);
                }
                else
                {
                    //
                    // The event start and title are required, everything else is optional.
                    //
                    if (EventInfo.GetEventTimes().Count == 0)
                    {
                        PrintUsage("Start date is required!");
                    }
                    else if (String.IsNullOrEmpty(EventInfo.Title))
                    {
                        PrintUsage("A title is required!");
                    }

                    AddCalendarEvent(service, CalendarID, EventInfo);
                }
            }
            else
            {
                if (NakedParameters.Count == 0)
                {
                    ShowCalendar();
                }
                else if (NakedParameters.Count == 1)
                {
                    ShowCalendar(Int32.Parse(NakedParameters[0]));
                }
                else if (NakedParameters.Count == 2)
                {
                    ShowCalendar(Int32.Parse(NakedParameters[0]), Int32.Parse(NakedParameters[1]));
                }
                else
                {
                    PrintUsage();
                }
            }
        }