Created
July 5, 2014 15:30
-
-
Save barkerd427/ff9e4a3e9a6f53a6923c 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
require "instagram" | |
require "restclient" | |
require "twitter" | |
require 'koala' | |
hashtag = "danpluslaura" | |
INSTAGRAM_ACCESS_TOKEN = "############" | |
INSTAGRAM_CLIENT_ID = "###########" | |
INSTAGRAM_CLIENT_SECRET = "###########" | |
Instagram.configure do |config| | |
config.client_id = INSTAGRAM_CLIENT_ID | |
config.client_secret = INSTAGRAM_CLIENT_SECRET | |
end | |
instagram_client = Instagram.client(:access_token => INSTAGRAM_ACCESS_TOKEN) | |
tags = instagram_client.tag_search(hashtag) | |
urls = Array.new | |
if (!tags.empty?) | |
for media_item in instagram_client.tag_recent_media(tags[0].name) | |
urls << media_item.images.standard_resolution.url | |
end | |
end | |
twitter_client = Twitter::REST::Client.new do |config| | |
config.consumer_key = "############" | |
config.consumer_secret = "###############" | |
config.access_token = "##################" | |
config.access_token_secret = "######################" | |
end | |
for tweet in twitter_client.search("#" + hashtag + " -rt", {include_entities: "1"}) | |
if tweet.entities? | |
if !tweet.attrs[:entities][:media].nil? | |
if tweet.attrs[:entities][:media][0][:type].eql? "photo" | |
urls << tweet.attrs[:entities][:media][0][:media_url] | |
end | |
end | |
end | |
end | |
facebook_urls = Hash.new | |
oauth_access_token = "############################" | |
facebook_client = Koala::Facebook::API.new(oauth_access_token) | |
for post in facebook_client.search(hashtag) | |
if post.has_key?("picture") | |
facebook_urls["#{post["id"]}"] = post["picture"] | |
end | |
end | |
facebook_urls.each do |id, url| | |
if (File.exists?(Dir.pwd + "/#{id}")) | |
puts "file #{id} already exists from facebook" | |
next | |
end | |
puts "getting file #{id} from facebook" | |
image = RestClient.get(url) | |
path = Dir.pwd + "/#{id}" | |
File.open(path, 'w') {|f| f.write(image)} | |
end | |
pattern = /^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/ | |
urls.each_with_index do |url, idx| | |
split_url = url.match(pattern) | |
if (File.exists?(Dir.pwd + "/#{split_url[6]}")) | |
puts "file #{split_url[6]} already exists" | |
next | |
end | |
puts "getting file #{split_url[6]}" | |
image = RestClient.get(url) | |
path = Dir.pwd + "/#{split_url[6]}" | |
File.open(path, 'w') {|f| f.write(image) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment