Class: GranicusMigrateHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/granicus_migrate_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GranicusMigrateHelper

Returns a new instance of GranicusMigrateHelper.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/granicus_migrate_helper.rb', line 9

def initialize(args)
  # Handle options
  options = {}
  @option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: granicus_migrate [options]"
    opts.separator ""
    opts.separator "Specific options:"

    # host
    opts.on("-h","--host HOST","The Granicus platform host to load files into.") do |host|
      @host = host
    end

    # username
    opts.on("-u","--username USERNAME","The username to use to communicate with the Granicus host.") do |user|
      @username = user
    end

    # password
    opts.on("-p","--password PASSWORD","The password to use to communicate with the Granicus host.") do |pass|
      @password = pass
    end

    # directory
    opts.on("-d","--directory DIRECTORY", "The directory containing the HTML files to migrate.") do |dir|
      @directory = dir
    end
    
    # folder id
    opts.on("-f","--folder FOLDER_ID", "The folder id in Granicus that contains the destination clips.") do |id|
      @folder = id.to_i
    end

    # No argument, shows at tail.  This will print an options summary.
    opts.on_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  # parse options
  @option_parser.parse!(ARGV)
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



7
8
9
# File 'lib/granicus_migrate_helper.rb', line 7

def directory
  @directory
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/granicus_migrate_helper.rb', line 7

def host
  @host
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/granicus_migrate_helper.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/granicus_migrate_helper.rb', line 7

def username
  @username
end

Instance Method Details

#extract_text(tag) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/granicus_migrate_helper.rb', line 53

def extract_text(tag)
  if tag.class == Nokogiri::XML::Text
    tag.text.gsub(' ',' ')
  else
    text = ''
    tag.children.each {|child| text += extract_text child }
    text
  end
end

#missing_arguments?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/granicus_migrate_helper.rb', line 67

def missing_arguments?
  return (@host.nil? or @username.nil? or @password.nil? or @directory.nil?)
end

#process_filesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/granicus_migrate_helper.rb', line 71

def process_files
  granicus = GranicusPlatformAPI::Client.new @host,@username,@password
  clips = granicus.get_clips @folder.nil? ? 1 : @folder
  clips.each do |clip|
    # skip the file if we don't have a matching html document
    fname = @directory + clip.Name + '.html'
    unless File.exists? fname
      puts "Couldn't find file: #{fname} (skipping)"
      next
    end

    f = File.open fname
    sushi = Nokogiri::HTML f
    f.close
    puts "Processing #{clip.Name}..."
    paragraphs = sushi.xpath('//p')
    meta_array = []
    paragraphs.count.times do |index|
      tag = paragraphs[index]
      # process all of the text and see if we have an item
      text = extract_text tag
      text.gsub!(/\u00a0/,' ')
      next if not text =~ /\w\w/

      meta = GranicusPlatformAPI::MetaDataData.new 
      meta.Payload = GranicusPlatformAPI::AgendaItem.new
      meta.Name = text

      # grab any links in the tag
      link_tag = tag.xpath('.//a')[0]
      unless link_tag.nil?
        unless link_tag['href'].nil?
          meta.TimeStamp = link_tag['href'].gsub(/.*?(\d+\.\d\d\d).*/,'\\1').to_i
          link_tag['href'] = 'javascript:void(0);'
          link_tag['onclick'] = "top.SetPlayerPosition('0:#{meta.TimeStamp}',null);"
          link_tag.remove_attribute 'target'
        else
          puts 'Unabled to find href in link: ' + link_tag.to_s
        end
      end

      meta_array << meta
    end

    # write the html file and upload it to the platform
    File.open('agenda_temp.html', 'w') {|f| f.write(sushi.to_html) }

    options = {
      :headers => {
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Host' => @host,
        'Cookie' => "SESS1=#{granicus.impersonation_token}"},

        :ssl => {:verify => false}
      }

    connection = Faraday::Connection.new(options) do |builder|
      builder.use Faraday::Request::Multipart
      builder.adapter(Faraday.default_adapter)
    end

    payload = {
      :form_panel_agendaPublishing => 'uploaded',
      :form_submit => 'Save Settings',
      :form_panel_agendaFile => Faraday::UploadIO.new('agenda_temp.html', 'text/html')
    }
    response = connection.post "http://#{@host}/panes/EditFileAgenda.php?clip_id=#{clip.ID}", payload  
    
    File.delete('agenda_temp.html')
    
    # upload all of the metadata
    puts "Writing #{meta_array.count} items to Granicus Clip ID #{clip.ID}"
    keymap = granicus. clip.ID, meta_array

    # make the clip public
    clip.Status = "Public"
    granicus.update_clip clip
  end
end

#show_helpObject



63
64
65
# File 'lib/granicus_migrate_helper.rb', line 63

def show_help
  puts @option_parser
end