SSブログ

Creation of the data for ManyTime app from Google calendar

The data for ManyTime app is created from Google calendar.
Here, it is assumed that all the events of Google calendar are events
all day and all scripts runs on Mac.

1: The data in Google calendar is acquired using the ICAL function
of Google calendar.
Please refer to explanation of the following URL.
https://support.google.com/calendar/answer/37648?hl=
As a result, basic.ics is downloaded.

2: The ical2org program is downloaded and corrected.
org-mode is the mode using the Emacs environment as an outline
processor, a TODO list, etc., and org seems to be the data format
for org-mode.
Since it is transformed in the form that is near the data for
ManyTime app also by chance, ical2org is used.

With reference to explanation of the following URL, the ical2org
program (gawk script) which translates ics data into org data
should download.
http://orgmode.org/worg/org-tutorials/org-google-sync.html

Only the events for seven days can be treated in initial setting
of the ical2org program in the above-mentioned URL report.
Please change the corresponding section of the program as follows.
max_age = -1; # in days

3: The shell script is performed and translates the data of Google
calendar from basic.ics to output-7.txt.
It works in the terminal on Mac OS X 10.7.5.
The Ruby 1.8.7 is used on the script.

The ical2org program downloaded in step 2 makes enable execution
by using chmod u+x.
$ chmod u+x ical2org
Also the shell script tajikan.sh makes enable execution by using
chmod u+x.
$ chmod u+x tajikan.sh
$ ./tajikan.sh

4: The duplication line check of output-7.txt.
It sorts by the 2nd field(start time).
$ sort -k2 output-7.txt > output-7b.txt
This is confirmed whether there is any difference.
$ diff output-7.txt output-7b.txt
If there are duplicate lines by this check, these lines should
be deleted from output-7.txt.
  $ vi output-7.txt

5: The data for ManyTime app is created.
The data for ManyTime app with the event length corresponding
to the number of duplicate lines is created.
$ ruby tajikan-3.rb output-7.txt > output-8.txt

tajikan.rb:
#Start Time
def startTime(day, num)
 return day + " 00:00:00"
end

#End Time
def endTime(day, num)
  return day + " 23:59:00"
end

#Create the data for ManyTime from the date and the comment 
#that you extracted from the ics data of Google calendar.
aHash = Hash.new
File.open(ARGV[0], "r") {
  | f |
  while line = f.gets
    # Creating an array by dividing the row delimiter
    dayArray = line.split("\t")
    # If the hash value is not nil, the hash value will be added 1.
    # The hash key is the start date string.
    if (aHash[dayArray[0]] != nil)
      aHash[dayArray[0]] = aHash[dayArray[0]] + 1
    else
      aHash[dayArray[0]]  = 0
    end
    # Output
    puts(startTime(dayArray[0],aHash[dayArray[0]]) + "\t" +
      endTime(dayArray[0],aHash[dayArray[0]]) + "\t" + dayArray[1]);
  end
}

tajikan-2.rb:
require 'shell'
=begin
The start date is the second field of the first argument(ARGV[0]). 
The program searches in the second argument(ARGV[1]).
It adds the number of rows of the first field to the beginning 
of the matching row of the first argument file.
=end
File.open(ARGV[0], "r") {
  | f |
  while line = f.gets
    # Creating an array by dividing the row delimiter
    dayArray = line.split(" ")
    # The program runs a search command by sed.
    aCommand = "gsed -n ''\\/" + dayArray[1].to_s + "\\/{s\\/^\\/" + dayArray[0].to_s + "\\\t\\/p}'' " + ARGV[1]
    system(aCommand)
  end
}

tajikan.sh:
#!/bin/sh
#
# Convert basic.ics to the format org.
./ical2org < basic.ics > output-1.txt

# Example of the output line:
# END:VEVEN
# *  Happy Birthday
#  <2013-04-04 THU 00:00>--<2013-04-05 FRI 00:00>
# :PROPERTIES:
#
# Extract the above two lines from each event in the output-1.txt.
cat output-1.txt | awk '/\*  / || / output-2.txt

# Convert the line feed of the asterisk line to the tab character 
# in the output-1.txt.
cat output-2.txt | awk '{if(/\*  /) printf $2 "\t"; else print $0}'  > output-3.txt

# Extract the first date and the comment from the output-3.txt
cat output-3.txt | awk '{print $2 "\t" $1}' | sed 's/ output-4.txt

# Reverse the order of the file contents of the output-4.txt. 
# Then sort by date.
awk '1 { last = NR; line[last] = $0; } END { for (i = last; i > 0; i--) { print line[i]; } }' output-4.txt | sort > output-5.txt

# Create the data for ManyTime app.
ruby tajikan.rb output-5.txt > output-6.txt

# Count the number of lines that start time overlap.
# Then, create the count.txt from the number of lines and the start time.
cut -f 1 output-6.txt | sort | uniq -c > count.txt

# Merge the output-6.txt and count.txt
ruby tajikan-2.rb count.txt output-6.txt > output-7.txt

tajikan-3.rb:
# startTime(day:Date and time string, num:order, count:the number of lines)
# Return the time string (yyyy-MMM-dd HH:00:00) by calculating the start time.
def startTime(day, num, count)
  # Decide timeLength (event length) by the count value.
  timeLength = 0
  if count == "1"
    timeLength = 24
  elsif count == "2"
    timeLength = 12
  elsif count == "3"
    timeLength = 8
  elsif count == "4"
    timeLength = 6
  elsif count == "5"
    timeLength = 4
  elsif count == "6"
    timeLength = 4
  elsif count == "7"
    timeLength = 3
  elsif count == "8"
    timeLength = 3
  else
    timeLength = 1
  end
  # Determine the (hour part) start time (hour part).
  aHour = 0
  if num == 0
    aHour = 0
  else
    aHour = num * timeLength
  end
  # String processing of the start time (hour part)
  if aHour.to_s.length == 1
    aHourStr = " 0" + aHour.to_s
  else
    aHourStr = " " + aHour.to_s
  end
  # Extract the date portion (yyyy-MM-dd) from the start time string(day). 
  dayArray = day.split(" ")
  # Return the start time string (yyyy-MM-dd HH:00:00).
  return dayArray[0] + aHourStr + ":00:00"
end

# endTime(day:Date and time string, num:order, count:the number of lines)
# Return the time string (yyyy-MMM-dd HH:59:00) by calculating the end time.
def endTime(day, num, count)
  # Decide timeLength (event length) by the count value.
  timeLength = 0
  if count == "1"
    timeLength = 24
  elsif count == "2"
    timeLength = 12
  elsif count == "3"
    timeLength = 8
  elsif count == "4"
    timeLength = 6
  elsif count == "5"
    timeLength = 4
  elsif count == "6"
    timeLength = 4
  elsif count == "7"
    timeLength = 3
  elsif count == "8"
    timeLength = 3
  else
    timeLength = 1
  end
  # Determine the (hour part) end time (hour part).
  aHour = 0
  if num == 0
    aHour = 0
  else
    aHour = num * timeLength
  end
  # Subtract 1 hour after adding the event length to the end time(hour part).
  aHour = aHour + timeLength - 1
  # String processing of the end time (hour part)
  if aHour.to_s.length == 1
    aHourStr = " 0" + aHour.to_s
  else
    aHourStr = " " + aHour.to_s
  end
  # Extract the date portion (yyyy-MM-dd) from the end time string(day). 
  dayArray = day.split(" ")
  # Return the end time string (yyyy-MM-dd HH:59:00).
  return dayArray[0] + aHourStr + ":59:00"
end

# Create ManyTime data from the date and the comment that extracted 
# from the ics data of Google calendar.
aHash = Hash.new
File.open(ARGV[0], "r") {
  | f |
  while line = f.gets
    #Creating an array by dividing the row delimiter
    dayArray = line.split("\t")
    # If the hash value is not nil, the hash value will be added 1.
    # The hash key is the start date string.
    if (aHash[dayArray[1]] != nil)
      aHash[dayArray[1]] = aHash[dayArray[1]] + 1
    else
      aHash[dayArray[1]]  = 0
    end
    # Do a line output. 
    # Specify the same argument values ​​in the startTime method and the endTime method.
    puts(startTime(dayArray[1],aHash[dayArray[1]],dayArray[0]) + "\t" +
      endTime(dayArray[1],aHash[dayArray[1]],dayArray[0]) + "\t" + dayArray[3]);
  end
}

Use of a spreadsheetCreation of a garbag.. ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。