Sha256: 929fc56b5e0f6132801b4855de90b709042d183c59c8dc890e680033f09e0651

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

#!/usr/bin/env ruby

require 'optparse'
require 'methadone'
require 'mailchimp' 
require 'erubis'
require 'json'
require 'mailchimp_exporter'

class App
  include Methadone::Main
  include Methadone::CLILogging

  main do |mailchimp_api_key|

    mailchimp_api = Mailchimp::API.new(mailchimp_api_key)
    export_api = Mailchimp::Export.new(mailchimp_api_key)
    list_template_file = File.read('./lib/mailchimp_exporter/list_template.erb')
    list_template = Erubis::Eruby.new(list_template_file)

    mailchimp_api.lists['data'].each do |list|
        puts "\t Extracting members from list: #{list['name']}"
        filename = options['output_folder'] + friendly_filename("#{list['id']}-#{list['name']}") + ".rdfa.html"
        members = export_api.list(id: list['id']).map {|member| JSON.parse(member)}
        File.open(filename, 'w') {|f| f.write(list_template.result(:list=>list, :members => members)) } 
        puts "\t\t exported #{members.count} members to #{filename}"
    end
  end

  def self.friendly_filename(filename)
    filename.gsub(/[^\w\s_-]+/, '')
            .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
            .gsub(/\s+/, '_')
  end

  # Declare command-line interface here

  description "Export your MailChimp data (lists, campaigns) into standard formats like RDFa"
  
  options['output_folder'] = "./"

  on("--output_folder VAL","Folder to write exported data to")

  arg :mailchimp_api_key, "Your MailChimp API key (including datacenter, in format: '{key}-{dc}')"
  
  version MailchimpExporter::VERSION

  use_log_level_option

  go!
end




Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailchimp_exporter-0.0.1 bin/mailchimp_exporter