Created
February 12, 2015 16:07
-
-
Save sha1sum/c50f330d5a1b0e42a28c to your computer and use it in GitHub Desktop.
Back Up a GroupMe Group's Images to a Folder
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
require 'uri' | |
require 'open-uri' | |
require 'fileutils' | |
require 'httparty' | |
# You will need to have HTTParty installed: | |
# https://github.com/jnunemaker/httparty | |
# | |
# Initialize with your access token: | |
# GroupMe.new('abcdefghijklmnopqrstuvwxyz') | |
# | |
# get_groups is a convenience method that will get 100 groups at a time, | |
# starting with a particular page | |
# | |
# To backup an entire group's images as far back as is possible, use | |
# the #back_up_images method, passing it a group ID and a group name | |
# (used for creating the proper directories). | |
# | |
# Images will be saved to /tmp/GroupMe Image Backup/ | |
class GroupMe | |
include HTTParty | |
base_uri 'https://api.groupme.com/v3' | |
format :json | |
def initialize(access_token) | |
@access_token = access_token | |
end | |
def back_up_images(group_id, group_name) | |
messages = get_messages_before group_id, group_name | |
return if messages.nil? | |
until messages.count == 0 | |
urls = Array.new | |
for message in messages do | |
url = message.image_url | |
urls << url if url | |
end | |
urls.uniq! | |
for url in urls do | |
puts "Downloading #{url}" | |
download_and_save_image(group_name_to_directory_name(group_name), url) | |
end | |
messages = get_messages_before group_id, group_name, messages.last.message_id | |
return if messages.nil? | |
end | |
end | |
def get_groups(page = 1) | |
response = doGet '/groups', { per_page: 100, page: page } | |
response['response'] | |
end | |
def get_messages_before(group_id, group_name, before_id = nil) | |
messages = Array.new | |
query = { limit: 100 } | |
query[:before_id] = before_id if before_id | |
response = doGet "/groups/#{group_id}/messages", query | |
return nil unless response.success? | |
return nil unless response['response'] and response['response']['messages'] | |
all_messages = response['response']['messages'] | |
for message in all_messages do | |
next if message['system'] == true | |
group_me_message = GroupMeMessage.new group_name, | |
message['attachments'], | |
message['id'] | |
messages << group_me_message | |
end | |
messages | |
end | |
def group_name_to_directory_name(group_name) | |
encoding_options = { | |
:invalid => :replace, # Replace invalid byte sequences | |
:undef => :replace, # Replace anything not defined in ASCII | |
:replace => '' # Use a blank for those replacements | |
} | |
group_name.encode(Encoding.find('ASCII'), encoding_options) | |
end | |
def download_and_save_image(directory, url) | |
begin | |
dir_string = "/tmp/GroupMe Image Backup/#{directory}" | |
FileUtils.mkdir_p(dir_string) | |
filename = File.basename(URI.parse(url).path) | |
parts = filename.split '.' | |
parts.insert(parts.length - 2) | |
filename = parts.insert(parts.length - 2, parts.delete_at(parts.length - 1)). | |
join('.') | |
open("#{dir_string}/#{filename}", 'wb') do |file| | |
open(url) do |uri| | |
file.write uri.read | |
end | |
end | |
rescue | |
return | |
end | |
end | |
def doGet(path, query = nil) | |
query_hash = { token: @access_token } | |
query_hash = query_hash.merge query if query | |
self.class.get path, { query: query_hash, headers: { 'Content-Type' => 'application/json', | |
'Accept' => 'application/json' } } | |
end | |
end | |
class GroupMeMessage | |
attr_reader :group_name, :attachments, :message_id | |
def initialize(group_name, attachments, message_id) | |
@group_name = group_name | |
@attachments = attachments | |
@message_id = message_id | |
end | |
def has_image? | |
has_image = false | |
for attachment in @attachments do | |
has_image = true if attachment['type'] == 'image' | |
end | |
has_image | |
end | |
def image_url | |
return nil unless has_image? | |
for attachment in @attachments do | |
return attachment['url'] if attachment['type'] == 'image' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment