Last active
August 29, 2015 14:04
-
-
Save davidbalbert/f970defd9f53a2e10da7 to your computer and use it in GitHub Desktop.
Google Groups -> Community import script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This will be pasted into the REPL | |
require 'csv' | |
# these were extracted from the first line of the CSV | |
GoogleGroupContact = Struct.new(:email_address, :nickname, :group_status, :email_status, :email_preference, :posting_permissions, :join_year, :join_month, :join_day, :join_hour, :join_minute, :join_second, :time_zone) | |
class InteractiveContactFinder | |
def self.find_all(contacts) | |
@@done = false | |
catch :quit do | |
contacts.map do |c| | |
user_from_contact(c) | |
end | |
end | |
end | |
def self.user_from_contact(c) | |
user = User.where(email: c.email_address).first | |
return user if user | |
names = c.nickname.try(:split) | |
return c if names.nil? | |
if names.size == 2 | |
first, last = names | |
elsif names.size == 3 | |
first, _, last = names | |
end | |
user = User.where(first_name: first, last_name: last).first | |
return user if user | |
unless @@done | |
catch :done do | |
new(c).prompt_for_contact | |
end | |
end | |
end | |
attr_reader :contact | |
def initialize(contact) | |
@contact = contact | |
end | |
def prompt_for_contact | |
puts | |
puts %{>>> Couldn't find user "#{name}" <#{email}>. Please do it manually.} | |
puts ">>> Control methods `pick`, `dunno`, `quit`, and `done`" | |
puts ">>> Attributes available: `name`, `email`, `contact`" | |
puts | |
self.pry | |
end | |
private | |
def email | |
contact.email_address | |
end | |
def name | |
contact.nickname | |
end | |
def pick(user) | |
puts "Picking #{user.name}" | |
# pry catches :breakout. I'm relying on pry's implementation here. Not great, but this will only get run once. | |
throw :breakout, user | |
end | |
def dunno | |
throw :breakout, email | |
end | |
def quit | |
throw :quit | |
end | |
def done | |
@@done = true | |
throw :done, nil | |
end | |
end | |
class GoogleGroup | |
attr_reader :name, :members | |
def initialize(name, csv_text) | |
@name = name | |
# the first line is a title "Members for group hackerschool-nyc" | |
# the second line is the column headers which I've encoded in GoogleGroupContact | |
@members = CSV.parse(csv_text)[2..-1].map { |row| GoogleGroupContact.new(*row) } | |
end | |
end | |
def subscribe(google_group, subforum) | |
results = InteractiveContactFinder.find_all(google_group.members).group_by(&:class) | |
results[User].each do |u| | |
s = subforum.subscription_for(u) | |
if s.new_record? | |
s.subscribed = true | |
s.reason = "You are receiving emails because you were subscribed to #{google_group.name}." | |
s.save | |
end | |
end | |
puts "Here are the email addresses you were unable to find. You should email them." | |
puts results[String] | |
end | |
# This is how to run the code: | |
# paste in CSV data here | |
s = <<END | |
... | |
END | |
subforum = Subforum.find(5) # New York | |
google_group = GoogleGroup.new("hackerschool-nyc", s) | |
subscribe(google_group, subforum) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment