-
-
Save jhbabon/5894578 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# Modified from https://gist.github.com/jpr5/4260656 | |
# Usage: | |
# HIPCHAT_USER="[email protected]" HIPCHAT_PASS="secret" bundle exec ./clear_hipchat.rb | |
require 'mechanize' | |
require 'chronic' | |
account = { | |
email: ENV['HIPCHAT_USER'], | |
password: ENV['HIPCHAT_PASS'], | |
} | |
agent = Mechanize.new | |
site = agent.post("https://www.hipchat.com/sign_in", account).uri.host | |
# Get our user id | |
results = agent.get("https://#{site}/account").search('img[alt="Your photo"]') | |
if results && !results.empty? | |
member_id = results.first["src"].match(%r{photos/(\d+)})[1] | |
else | |
$stderr.puts "ERROR: You need to put a photo in your profile in order to get your member's ID." | |
exit 1 | |
end | |
$stdout.puts "==> Your HIPCHAT member ID: #{member_id}" | |
# Determine our Signup date and the actual date | |
registered_around = agent. | |
get("https://#{site}/people/show/#{member_id}"). | |
search('li:contains("Signed up")'). | |
text. | |
sub(/Signed up: /, '') | |
registered_on = Chronic.parse(registered_around).to_date | |
intervals = (registered_on..Date.today).map { |d| d.strftime("/%Y/%m/%d") } | |
# Identify all users ids | |
members = agent. | |
get("https://#{site}/people"). | |
search("ul.members"). | |
children. | |
search("a"). | |
map { |m| m.attributes["href"].value }. | |
reject { |m| m =~ /#{member_id}/ } | |
# Get and delete all messages between the users and you | |
members.each do |member| | |
real_member_id = member.match('/[0-9]*/')[0] | |
total = 0 | |
$stdout.puts '' | |
$stdout.puts "==> Deleting messages for member: #{member}" | |
intervals.each do |interval| | |
messages = [] | |
messages += agent. | |
get("https://#{site}/history/member#{real_member_id}#{interval}"). | |
search("input[name=message_id]"). | |
map { |m| m["value"] } | |
messages.each do |msg_id| | |
agent.post( | |
"https://#{site}/history/member#{real_member_id}", | |
{ action: "delete", message_id: msg_id } | |
) | |
$stdout.write '.' | |
total += 1 | |
end | |
end | |
$stdout.puts "--> Deleted #{total} messages" | |
end | |
$stdout.puts "==> All messages deleted." |
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
# A sample Gemfile | |
source "https://rubygems.org" | |
gem 'mechanize' | |
gem 'chronic' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment