Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

hipchat 2 google drive export worker #53

Merged
merged 7 commits into from
Commits on Jul 3, 2012
  1. @rkononov

    hipchat export worker

    rkononov authored
Commits on Jul 6, 2012
  1. @treeder
Commits on Jul 12, 2012
  1. @treeder
Commits on Jul 13, 2012
  1. @treeder

    Added todo for readme.

    treeder authored
  2. @treeder
Commits on Jul 15, 2012
  1. @treeder
Commits on Jul 16, 2012
  1. @treeder
This page is out of date. Refresh to see the latest.
View
2  README.md
@@ -19,6 +19,8 @@ You can get your token from: https://hud.iron.io/tokens . **Be sure to keep your
You can get your project_id from your project list in https://hud.iron.io/tq/projects
+For full details on configuration, see [Configuration article](http://dev.iron.io/articles/configuration/) in [Dev Center](http://dev.iron.io/).
+
## Now Run the Examples and Learn from Them
See the README's in each of the examples directories to run and review the code to learn how it works.
View
2  go/hello_worker/README.md
@@ -1 +1 @@
-DO NOT USE THIS YET
+TODO: README
View
18 ruby_ng/hipchat_export/README.md
@@ -0,0 +1,18 @@
+# Hipchat to GoogleDrive export
+
+## Overview
+
+This worker export history from your hipchat account to your GoogleDrive spreadsheets (one spreadsheet per month).
+
+## Quick Start
+
+Required gems:
+
+* gem install uber_config hipchat-api google_drive
+
+1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
+1. Copy config_example.yml to config.yml and modify to your liking.
+1. Run `iron_worker upload hipchat_export` to upload the worker code package to IronWorker.
+1. Queue up a task:
+ 1. Run `ruby run_hipchat_export.rb` to queue up a task.
+1. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.
View
13 ruby_ng/hipchat_export/config_example.yml
@@ -0,0 +1,13 @@
+hipchat_token: "TOKEN"
+# An array of rooms to skip:
+exclude_rooms: ["AlwaysBeTesting", "Test Room", "Performance"]
+
+# Use either google username and password:
+google_password: "YOURPASSWORD"
+google_username: "YOURUSERNAME"
+
+# OR use oauth2. See get_oauth2_token.rb
+#google_client_id: "CLIENT_ID"
+#google_secret: "SECRET"
+# You'll get the refresh token after running get_oauth2_token.rb script
+#google_refresh_token: "TOKEN"
View
50 ruby_ng/hipchat_export/get_oauth2_token.rb
@@ -0,0 +1,50 @@
+require 'google_drive'
+require 'oauth2'
+require 'launchy'
+require 'yaml'
+
+
+# Get client_id and secret from Google: https://code.google.com/apis/console
+# Be sure to create an application the Google API Console that is an "installed application"
+# Update config.yml with thes values
+config = YAML.load_file("config.yml")
+google_client_id = config["google_client_id"]
+google_secret = config["google_secret"]
+redirect_url = "urn:ietf:wg:oauth:2.0:oob" # This is used for desktop apps
+
+client = OAuth2::Client.new(
+ google_client_id,
+ google_secret,
+ :site => "https://accounts.google.com",
+ :token_url => "/o/oauth2/token",
+ :authorize_url => "/o/oauth2/auth")
+auth_url = client.auth_code.authorize_url(
+ :redirect_uri => redirect_url,
+ :scope => "https://docs.google.com/feeds/ " +
+ "https://docs.googleusercontent.com/ " +
+ "https://spreadsheets.google.com/feeds/")
+
+Launchy.open(auth_url)
+
+sleep 2
+
+puts "Enter the auth code you get in your browser: "
+authorization_code = gets
+puts "Got auth code: #{authorization_code}"
+puts "Getting token..."
+
+# Redirect the user to auth_url and get authorization code from redirect URL.
+auth_token = client.auth_code.get_token(
+ authorization_code,
+ :redirect_uri => redirect_url)
+
+puts "Verifying token..."
+
+session = GoogleDrive.login_with_oauth(auth_token)
+
+for file in session.files
+ p file.title
+end
+
+puts "Your auth_token is: #{auth_token.token}"
+puts "Your refresh token is: #{auth_token.refresh_token}"
View
122 ruby_ng/hipchat_export/hipchat_export.rb
@@ -0,0 +1,122 @@
+require 'active_support/core_ext'
+require 'hipchat-api'
+require 'google_drive'
+require 'oauth2'
+require 'time'
+
+####################
+#PARAMS
+puts params.inspect
+hipchat_token = params["hipchat_token"]
+exclude_rooms = params["exclude_rooms"] || []
+# Need either google username and pass or an oauth2 token
+google_user = params["google_username"]
+google_pass = params["google_password"]
+# OR
+google_client_id = params["google_client_id"]
+google_secret = params["google_secret"]
+google_refresh_token = params["google_refresh_token"]
+range_ago = params["range_ago"]
+###################
+
+
+range_ago||=1
+if google_refresh_token
+ client = OAuth2::Client.new(
+ google_client_id,
+ google_secret,
+ :site => "https://accounts.google.com",
+ :token_url => "/o/oauth2/token",
+ :authorize_url => "/o/oauth2/auth")
+ access_token = OAuth2::AccessToken.from_hash(client, {:refresh_token => google_refresh_token})
+ access_token = access_token.refresh!
+ session = GoogleDrive.login_with_oauth(access_token)
+else
+ session = GoogleDrive.login(google_user, google_pass)
+end
+hipchat = HipChat::API.new(hipchat_token)
+end_date = Time.now
+start_date = end_date - range_ago.month
+rooms = hipchat.rooms_list
+#p rooms
+puts "Found #{rooms.size} rooms"
+open_rooms = rooms.to_hash["rooms"].select { |r| !r["is_private"] }
+rooms_history = {}
+
+filename = "HipChat Archive - #{Time.now.strftime('%Y-%m')}"
+file = session.files("title" => filename)
+
+puts "Looking for file: #{filename}"
+if file.empty?
+ puts "File not found, creating new one!"
+ file = session.create_spreadsheet(filename)
+else
+ puts "File found, let's use it"
+ file = file.first
+end
+
+
+open_rooms.each do |room|
+ room_name = room["name"]
+ puts "Room: #{room_name} - private? #{room["is_private"]}"
+ puts "Looking for a worksheet: #{room_name}"
+ if exclude_rooms.include?(room_name)
+ puts "Skipping room."
+ next
+ end
+ ws = file.worksheet_by_title(room_name)
+ import_from = Time.at(room["created"])
+ if ws
+ puts "Worksheet found!"
+ last_entry = ws.list.entries.last
+ if last_entry
+ last_date = last_entry["date"]
+ puts "Last date used: #{last_date}"
+ # eg: Last date used: 7/3/2012 0:19:22
+ if last_date.length < 12
+ import_from = Date.strptime(last_date, "%m/%d/%Y").to_time
+ else
+ import_from = DateTime.strptime(last_date, "%m/%d/%Y %H:%M:%S")
+ end
+ end
+ puts "import_from: #{import_from}"
+ else
+ puts "No such worksheet, creating new"
+ ws = file.add_worksheet(room_name)
+ ws.list.keys = ["date", "from", "message"]
+ ws.save
+ end
+ import_from = start_date if start_date > import_from
+ puts "Latest timestamp :#{import_from}"
+ days_back = ((end_date - import_from)/60/60/24).ceil
+ puts "Should look for #{days_back} days back"
+ days_back.downto(0) do |i|
+ date_to_get = (end_date - i.days)
+ formatted_date = date_to_get.strftime('%Y-%m-%d')
+ puts "Getting messages #{i} days ago - #{formatted_date}"
+ resp = hipchat.rooms_history(room_name, formatted_date, 'UTC').parsed_response
+ messages = rooms_history[room_name]= resp["messages"]
+ #puts "Found messages:#{messages.inspect}"
+ if !messages
+ # something is wrong
+ puts "NO MESSAGES!"
+ p resp
+ break
+ elsif messages.size > 0
+ messages.each_with_index do |m, i|
+ d = DateTime.parse(m["date"])
+ next if d <= import_from
+ puts "Pushing message :#{m.inspect}"
+ ws.list.push({:date => d.strftime("%m/%d/%Y %H:%M:%S"), :from => m["from"]["name"], :message => m["message"]})
+ ws.save if i % 50 == 0
+ end
+ ws.save
+ else
+ ws.list.push({:date => date_to_get.strftime('%Y-%m-%d'), :from => "--", :message => "No messages"})
+ ws.save
+ end
+ end
+end
+
+temp_sheet = file.worksheet_by_title('Sheet 1')
+temp_sheet.delete if temp_sheet
View
8 ruby_ng/hipchat_export/hipchat_export.worker
@@ -0,0 +1,8 @@
+runtime 'ruby'
+exec 'hipchat_export.rb'
+name 'HipchatExport'
+gem 'hipchat-api'
+gem 'google_drive'
+gem 'oauth2'
+
+
View
22 ruby_ng/hipchat_export/run_hipchat_export.rb
@@ -0,0 +1,22 @@
+require 'iron_worker_ng'
+require 'yaml'
+require 'uber_config'
+
+#IronCore::Logger.logger.level = ::Logger::DEBUG
+
+# Create IronWorker client
+client = IronWorkerNG::Client.new
+
+params = UberConfig.load()
+puts 'CONFIG: ' + params.inspect
+raise "No params!" unless params
+
+task = client.tasks.create('HipchatExport', params)
+
+puts "Your task has been queued up, check https://hud.iron.io to see your task status and log or wait for it below..."
+
+result = client.tasks.wait_for(task.id)
+p result
+
+log = client.tasks.log(task.id)
+puts log
Something went wrong with that request. Please try again.