Class: TwitPhoto::TwitPhoto

Inherits:
Object
  • Object
show all
Defined in:
lib/twitphoto.rb

Class Method Summary collapse

Class Method Details

.getPhotoUrlFromUrl(url) ⇒ Object

extracts the media url from a shortened url (if possible)

Parameters:

url

The URL as a string (not URI)

Returns:

return

A media URL as a string, or nil

Example:

TwitPhoto::TwitPhoto.getPhotoUrlFromUrl "http://yfrog.com/kfx6gdj"


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/twitphoto.rb', line 47

def self.getPhotoUrlFromUrl url
  adaptors = [Adaptors::YFrogAdaptor, Adaptors::TwitPicAdaptor, Adaptors::LockerzAdaptor, Adaptors::InstagramAdaptor]

  adaptors.each do |adaptor|
     imageUrl = adaptor.getImageUrl url
     if !imageUrl.nil?
       return imageUrl
     end
  end

  return nil
end

.getPhotoUrlsFromText(text) ⇒ Object

gets all of the photo URLs from a blog of text

Parameters:

text

the text you want to extract media from

Returns:

return

an array of URLs as strings (not URI objects)

Example:

Get photo URLs from a block of text

TwitPhoto::TwitPhoto.getPhotoUrlsFromText "Hello http://yfrog.com/kfx6gdj http://yfrog.com/klmjxyej"


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twitphoto.rb', line 21

def self.getPhotoUrlsFromText(text)
  urlStrings = URI.extract text
  results = []

  urlStrings.each do |url|
    imageUrl = TwitPhoto.getPhotoUrlFromUrl url
    if !imageUrl.nil?
      results << imageUrl
    end 
  end

  return results
end

.getPhotoUrlsFromTweet(tweet) ⇒ Object

returns all of the photo URLs from a tweet object from the “twitter” gem (twitter.rubyforge.org/)

IMPORTANT: this expects detailed entities from the twitter API, so request with :include_entities => ‘t’

IMPORTANT: this will yield better results than the other methods, since Twitter now has native media support

Paramaters

tweet

the tweet object from the twitter gem

Returns:

returns

the array of media URLs in this tweet



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/twitphoto.rb', line 73

def self.getPhotoUrlsFromTweet tweet
  results = []

  if !tweet.retweeted_status.nil?
    tweet = tweet.retweeted_status
  end

  # ensure the user included entites
  if !defined? tweet.entites || tweet.entities.nil? 
    raise ArgumentError, 'not a valid tweet object, make sure you :include_entities => \'t\' in the request'
  end

  # process URLs from third party photos
  urls = tweet.entities.urls || []
  urls.each do |url|
    # sometimes its in expanded, sometimes URL
    actualUrl = url.expanded_url
    if actualUrl.nil?
      actualUrl = url.url
    end
    if !actualUrl.nil?
      photoUrl = TwitPhoto.getPhotoUrlFromUrl actualUrl
      if !photoUrl.nil?
        results << photoUrl
      end
    end
  end

  # find any media from twitter itself (photobucket)
  medias = tweet.entities.media || []
  medias.each do |media|
    #only photos
    if 'photo'.eql? media[:type]
      results << media.media_url
    end
  end

  return results
end