Skip to content

Instantly share code, notes, and snippets.

@NickLaMuro
Last active December 19, 2023 05:02
Show Gist options
  • Save NickLaMuro/efbd1bcb5781e50d4546008eb3544c26 to your computer and use it in GitHub Desktop.
Save NickLaMuro/efbd1bcb5781e50d4546008eb3544c26 to your computer and use it in GitHub Desktop.
ruby script to search gitter archives (requires `oga` rubygem)
#!/usr/bin/env ruby
#
# gitter-search [--date YYYYMMDD] [--user @USERNAME] ORG/ROOM [PATTERN]
#
require 'oga'
require 'optparse'
require 'net/http'
options = {}
OptionParser.new do |opt|
opt.banner = "Usage: #{File.basename $0} [--date YYYY/MM/DD] [--user @USERNAME] ORG/ROOM [PATTERN]"
opt.separator ""
opt.on("--date=DATE", "Search only a specific date") { |date| options[:date] = date }
opt.on("--user=USER", "Limit search to specific user") { |user| options[:user] = user }
opt.separator ""
opt.separator "Examples"
opt.separator ""
opt.separator " Search for all messages in ManageIQ/miq_bot by @NickLaMuro"
opt.separator ""
opt.separator " $ gitter --user NickLaMuro ManageIQ/miq_bot"
opt.separator ""
opt.separator " Search for all messages in ManageIQ/miq_bot on 2019/07/02"
opt.separator ""
opt.separator " $ gitter --date 2019/07/02 ManageIQ/miq_bot"
opt.separator ""
opt.separator " ... now with messages containing the word 'fun'"
opt.separator ""
opt.separator " $ gitter --date 2019/07/02 ManageIQ/miq_bot fun"
opt.separator ""
opt.separator ""
end.parse!
host = "https://gitter.im/"
room = ARGV.shift
path = "/#{room}/archives"
user = "@#{options[:user]}" if options[:user]
date = options[:date]
pattern = ARGV.shift
link_regexp = /href="(#{path}[^"]*)"/
room_format = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}\/[a-z\d\-_\/]*$/i
raise "Invalid ORG/ROOM format" unless room =~ room_format
archives_list = Net::HTTP.get URI("#{host}#{path}")
archive_paths = archives_list.scan(link_regexp).map(&:first)
archive_paths.each do |archive|
next if date && !archive.include?(date)
printed = false
document = Oga.parse_html Net::HTTP.get(URI("#{host}#{archive}"))
document.css('.chat-item__content').each do |element|
found_user = element.at_css('.chat-item__username')
@current_user = found_user.text if found_user
chat_msg = element.at_css('.chat-item__text')
next if user && @current_user != user
next if pattern && !chat_msg.text.include?(pattern)
unless printed == true
puts
puts "Archive: #{host}#{archive}"
puts
printed = true
end
print "#{@current_user}: "
puts chat_msg.text.strip
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment