Skip to content

Instantly share code, notes, and snippets.

@whoisjake
Created August 31, 2009 02:53
Show Gist options
  • Save whoisjake/178266 to your computer and use it in GitHub Desktop.
Save whoisjake/178266 to your computer and use it in GitHub Desktop.
Simple module that allows you to easily search for images in tweets relative to a keyword
require 'rubygems'
require 'httparty'
require 'uri'
module PictureSearch
class TwitterSearch
include HTTParty
base_uri 'search.twitter.com'
default_params :output => 'json'
format :json
def self.find_pics(q, page = 1, per_page = 100)
get('/search.json', :query => {:q => q, :page => page, :rpp => per_page})["results"]
end
end
def self.thumb_for(url)
thumb = nil
if url =~ /twitpic/
pid = url.match(/http:\/\/twitpic\.com\/([a-zA-Z0-9]+)/).captures[0]
thumb = "http://twitpic.com/show/thumb/#{pid}"
elsif url =~ /yfrog/
pid = url.match(/http:\/\/yfrog\.[a-z.]+\/([a-zA-Z0-9]+)/).captures[0]
thumb = "http://yfrog.com/#{pid}.th.jpg"
else
return nil
end
end
class Scraper
def self.grab(keyword, page = 1, per_page = 100)
q = "#{keyword} twitpic.com OR yfrog."
tweets = TwitterSearch.find_pics(q,page,per_page)
tweets = tweets.collect do |r|
h = {"text"=> r["text"], "from_user"=> r["from_user"], "id"=> r["id"], "profile_image_url"=> r["profile_image_url"]}
h["pics"] = URI.extract(r["text"],"http")
h["pics"] = h["pics"].select{|p| (p =~ /twitpic/) || (p =~ /yfrog/)}
h
end
tweets
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment