Advertisement
developerjustin

Untitled

May 16th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.98 KB | None | 0 0
  1. using System;using System.IO;using System.Text;using System.Xml;using DDay;using DDay.iCal;using DDay.iCal.Serialization;using DDay.iCal.Serialization.iCalendar;using CsvHelper.Configuration;using CsvHelper.TypeConversion;using CsvHelper;using Newtonsoft.Json.Serialization;using Newtonsoft;using Newtonsoft.Json;

namespace CSV2iCal
{    
 class MainClass
  {
        public static void Main (string[] args)
      {
            //JSON to iCal http://pints.digitalrelativity.me/api/get_posts/?post_type=event&posts_per_page=500
           //CSV2CSV (@"/Users/justin/events.ics", @"/Users/justin/events-news.ics");
           //iCal2CSV (@"/Users/justin/Desktop/event-organiser_2013-11-20.ics", @"/Users/justin/events-new.csv", "dsgdsg");
         XML2iCal (@"/Users/justin/Desktop/calendar_export_past.xml ", @"/Users/justin/Desktop/events-new.ics");
          //JSON2iCal(@"/Users/justin/Desktop/events.json", @"/Users/justin/Desktop/events-new.ics");
      }

     public static void JSON2iCal(string oldPath, string newPath){

     }

     public static void XML2iCal(string oldPath, string newPath)
      {

         XmlDocument doc = new XmlDocument();
         doc.Load(oldPath);

            DDay.iCal.iCalendar iCal = new DDay.iCal.iCalendar();

         Console.WriteLine ("* * * * doc.DocumentElement.ChildNodes Count " + doc.DocumentElement.ChildNodes.Count);

           foreach(XmlNode node in doc.DocumentElement.ChildNodes){
             string text = node.InnerText; 

                var newNode = node;

               Event evt = iCal.Create<Event>();

             string title = "";
               string desc = "";
                string phone = "";
               string website = "";
             string websiteName = "";
             string location = "";

             title = node.SelectSingleNode ("title").InnerText;
               desc = node.SelectSingleNode ("description").InnerText;

               try{
                 phone = node.SelectSingleNode ("//phone").InnerText;
             }
                catch{
                   Console.WriteLine ("Phone was empty");
               }


              try{
                 website = node.SelectSingleNode ("website").InnerText;
               }
                catch{
                   Console.WriteLine ("Website was empty");
             }

             try{
                 websiteName = node.SelectSingleNode ("websiteName").InnerText;
               }
                catch{
                   Console.WriteLine ("websiteName was empty");
             }

             try{
                 location = node.SelectSingleNode ("location").InnerText;
             }
                catch{
                   Console.WriteLine ("Location was empty");
                }

             string start_time = node.SelectSingleNode ("start_time").InnerText;
              string end_time = node.SelectSingleNode ("end_time").InnerText;

               //evt.Name = "'Sample'";
             evt.Summary = title;
             evt.Start = new iCalDateTime(DateTime.Parse(start_time).ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
             evt.End = new iCalDateTime(DateTime.Parse(end_time).ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
             evt.Description = desc+" - "+phone+" - "+website+" - "+websiteName+" - "+location;

            }


          if (iCal.Events.Count > 1) {
             Console.WriteLine ("* * * * iCal Events Count " + iCal.Events.Count);
                iCalendarSerializer serializer = new iCalendarSerializer(iCal);
              serializer.Serialize(newPath);
           }

     }

     string DateFormat
        {
            get
          {
                return "yyyyMMddTHHmmssZ"; // 20060215T092000Z
           }
        }

     public static void iCal2CSV(string oldPath, string newPath, string csvFormat)
        {
            //Get File and contents
          IICalendarCollection calendars = iCalendar.LoadFromFile(oldPath);   
         //Load events into a list
            var occurrences = calendars.GetOccurrences(DateTime.Today.AddDays(double.Parse("-150000")), DateTime.Today.AddDays(double.Parse("150000")));

          //Create a writer for the new file 
          using (TextWriter writer = File.CreateText (newPath))
            {
                //Create a new CSV writer
                var csvWriter = new CsvWriter (writer);

               // Iterate through each occurrence and display information about it
              foreach (Occurrence occurrence in occurrences)
               {
                    DateTime occurrenceTime = occurrence.Period.StartTime.Local;
                 IRecurringComponent rc = occurrence.Source as IRecurringComponent;
                   if (rc != null)
                  {
                        //Console.WriteLine(rc.Summary + ": " + occurrenceTime.ToLongDateString());
                      //csvWriter.WriteRecord<string>(occurrenceTime.ToLongDateString());
                  }
                }
            }
        }
        public static void CSV2CSV(string oldPath, string newPath)
       {
            /*
               CSV to iCal
          */
           using (TextWriter writer = File.CreateText(newPath))
         using (TextReader reader = File.OpenText(oldPath))
           {
                //IICalendar iCal = new iCalendar();
             var csv = new CsvReader (reader);
                var csvWriter = new CsvWriter (writer);
              //var csvWriter = new CsvWriter(writer);
             int counter = 0;

              DRCompany currentCompany;
                DRCompany[] companies = new DRCompany[200];

               while (csv.Read()) 
              {   

                  currentCompany = new DRCompany();

                 Console.WriteLine ("ID: "+counter.ToString());
                   currentCompany.ID = counter.ToString ();
                 Console.WriteLine ("Name: "+csv.GetField (0));
                   currentCompany.Name = csv.GetField(0);
                   Console.WriteLine ("Website: "+csv.GetField (1));
                    currentCompany.Website = csv.GetField(1);
                    Console.WriteLine ("Tag: "+csv.GetField (2));
                    currentCompany.Tag = csv.GetField(2);
                    Console.WriteLine ("Street: "+csv.GetField (3));
                 currentCompany.Street = csv.GetField(3);
                 Console.WriteLine ("City: "+csv.GetField (4));
                   currentCompany.City = csv.GetField(4);
                   Console.WriteLine ("County: "+csv.GetField (5));
                 currentCompany.County = csv.GetField(5);
                 Console.WriteLine ("State: "+csv.GetField (6));
                  currentCompany.State = csv.GetField(6);
                  Console.WriteLine ("Zip: "+csv.GetField (7));
                    currentCompany.Zip = csv.GetField(7);
                    Console.WriteLine ("Description: "+csv.GetField (8));
                    currentCompany.Description = csv.GetField(8);

                 //Manipulating the lan/lon
                   string latLon = csv.GetField (9);
                    latLon = latLon.Replace ("GPS: ", "");
                   latLon = latLon.Replace ("N/A", "");
                 var latLonArray = latLon.Split (',');

                 if (latLonArray.Length > 1) 
                 {
                        var lat = latLonArray [0];
                       var lon = latLonArray [1];
                       lon = lon.Remove (0, 1);
                     lon = lon.Replace ("W080", "W80");
                       var gps = DMStoDecimal (lat,lon);
                        Console.WriteLine ("Lat " + gps[0]);
                     Console.WriteLine ("Lon " + gps[1]);
                     currentCompany.GPS = gps[0]+","+gps[1];
                  }  

                   Console.WriteLine ("Hours: "+csv.GetField (10));
                 currentCompany.Hours = csv.GetField (10);
                    Console.WriteLine ("Telephone: "+csv.GetField (11));
                 currentCompany.Telephone = csv.GetField (11);
                    Console.WriteLine ("Email: "+csv.GetField (12));
                 currentCompany.Email = csv.GetField (12);
                    Console.WriteLine ("Facebook: "+csv.GetField (13));
                  currentCompany.Facebook = csv.GetField (13);
                 Console.WriteLine ("Twitter: "+csv.GetField (14));
                   currentCompany.Twitter = csv.GetField (14);
                  Console.WriteLine ("G+: "+csv.GetField (15));
                    currentCompany.GooglePlus = csv.GetField (15);
                   Console.WriteLine ("Contact Email: "+csv.GetField (16));
                 currentCompany.ContactEmail = csv.GetField (16);
                 Console.WriteLine ("Contact Name: "+csv.GetField (17));
                  currentCompany.ContactName = csv.GetField (17);
                  Console.WriteLine ("\ r\r");

                  if (counter == 0) 
                   {
                        currentCompany.ID = "ID";
                        currentCompany.Name = "Name";
                        currentCompany.Website = "Website";
                      currentCompany.Tag = "Tag";
                      currentCompany.Street = "Street";
                        currentCompany.City = "City";
                        currentCompany.County = "County";
                        currentCompany.State = "State";
                      currentCompany.Zip = "Zip";
                      currentCompany.GPS = "GPS";
                      currentCompany.Description = "Description";
                      currentCompany.Hours = "Hours";
                      currentCompany.Telephone = "Telephone";
                      currentCompany.Email = "Email";
                      currentCompany.Facebook = "Facebook";
                        currentCompany.Twitter = "Twitter";
                      currentCompany.GooglePlus = "G+";
                        currentCompany.ContactEmail = "Contact Email";
                       currentCompany.ContactName = "Contact Name";
                 }

                 companies [counter] = currentCompany;

                 counter++;

                    PrintArray (companies);
              }

             // Loop over strings
             foreach (DRCompany s in companies)
               {
                    csvWriter.WriteRecord<DRCompany>(s);
             }
            }
        }

     public static void PrintArray(DRCompany[] array)
     {
            Console.WriteLine ("* * * * " + array.Length);
       }
        public static double ConvertDegreeAngleToDouble( double degrees, double minutes, double seconds )
        {
            //Decimal degrees = 
         //   whole number of degrees, 
           //   plus minutes divided by 60, 
            //   plus seconds divided by 3600

         return degrees + (minutes/60) + (seconds/3600);
      }

     public static double[] DMStoDecimal(string latDMS, string lonDMS)
        {
            //N39°5.4161’
         //Console.WriteLine ("Original Lat - "+latDMS);
          //Console.WriteLine ("Original Lon - "+lonDMS);

           //Degrees
            var latDegrees = latDMS.Substring (1, 2);
            var lonDegrees = lonDMS.Substring (1, 2);

         //Console.WriteLine ("Degrees Lat - "+latDegrees);
           //Console.WriteLine ("Degrees Lon - "+lonDegrees);

            //Minutes
            var latMinutes = latDMS.Substring (5, latDMS.Length-5);
          var lonMinutes = lonDMS.Substring (5, lonDMS.Length-5);
          latMinutes = latMinutes.Replace ("’","");
          lonMinutes = lonMinutes.Replace ("’","");

           // " ' "
         if(latMinutes.Contains("'"))
         {
                latMinutes = latMinutes.Replace ("'",string.Empty);
          }
            if(lonMinutes.Contains("'"))
         {
                lonMinutes = lonMinutes.Replace ("'",string.Empty);
          }

         // " ’ "
           if(latMinutes.Contains("’"))
           {
                latMinutes = latMinutes.Replace ("’",string.Empty);
            }
            if(lonMinutes.Contains("’"))
           {
                lonMinutes = lonMinutes.Replace ("’", string.Empty);
           }

         // " "
           if(latMinutes.Contains(" "))
         {
                latMinutes = latMinutes.Replace (" ", string.Empty);
         }
            if(lonMinutes.Contains(" "))
         {
                lonMinutes = lonMinutes.Replace (" ", string.Empty);
         }

         //Console.WriteLine ("Minutes Lat - "+latMinutes);
           //Console.WriteLine ("Minutes Lon -  "+lonMinutes);

           double decLat = new double ();
           double decLon = new double ();

            try
          {
                decLat = ConvertDegreeAngleToDouble (double.Parse (latDegrees), double.Parse (latMinutes), double.Parse ("000"));
                //Console.WriteLine ("DECIMAL LAT: " + decLat);
          }
            catch
            {
                decLat = double.Parse ("000");
           }


          try
          {
                decLon = ConvertDegreeAngleToDouble (double.Parse (lonDegrees), double.Parse (lonMinutes), double.Parse ("000"));
                //Console.WriteLine ("DECIMAL LON: " + decLon);
          }
            catch
            {
                decLon = double.Parse ("000");
           }


          return new double[] {decLat,decLon};
     }
    }
}

 



Advertisement
Add Comment
Please, Sign In to add comment
Advertisement