Class: Faceoff::Photo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, url, options = {}) ⇒ Photo

Returns a new instance of Photo.



106
107
108
109
110
111
# File 'lib/faceoff/photo.rb', line 106

def initialize id, url, options={}
  @fid     = id
  @url     = url
  @caption = options[:caption]
  @album   = options[:album] || "Photos"
end

Instance Attribute Details

#albumObject

Album name (context) of the photo.



103
104
105
# File 'lib/faceoff/photo.rb', line 103

def album
  @album
end

#captionObject

Caption of the photo.



100
101
102
# File 'lib/faceoff/photo.rb', line 100

def caption
  @caption
end

#fidObject

Facebook photo id.



94
95
96
# File 'lib/faceoff/photo.rb', line 94

def fid
  @fid
end

#urlObject

Url of the photo.



97
98
99
# File 'lib/faceoff/photo.rb', line 97

def url
  @url
end

Class Method Details

.photos_of_album(faceoff, album_id, options = {}, &block) ⇒ Object

Retrieve all photos for a given album id. To retrieve the profile album pass :profile as the album_id.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/faceoff/photo.rb', line 35

def self.photos_of_album faceoff, album_id, options={}, &block
  agent = faceoff.agent
  start = options[:start] || 0

  options[:album] = "Profile pictures" if album_id == :profile

  param = album_id == :profile ? "profile=1" : "aid=#{album_id}"
  param = "#{param}&id=#{faceoff.profile_id}&s=#{start}"

  page = agent.get "/album.php?#{param}"
  photo_link = page.link_with(:href => %r{/photo.php}).href
  photo_id = $1 if photo_link =~ /pid=(\d+)/

  return unless photo_id

  retrieve_all faceoff, photo_id, options, &block
end

.photos_of_me(faceoff, options = {}, &block) ⇒ Object

Retrieve all ‘Photos of Me’.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/faceoff/photo.rb', line 9

def self.photos_of_me faceoff, options={}, &block
  agent = faceoff.agent
  start = options[:start] || 0

  index = start % 15

  param = "id=#{faceoff.profile_id}&v=photos&so=#{start}&ref=sgm"

  page = agent.get "/profile.php?#{param}"

  photo_id, options[:user_id] =
    page.body.scan(%r{/photo.php\?pid=(\d+)[^\\]+;id=(\d+)})[index]

  return unless photo_id

  retrieve_all faceoff,
               photo_id,
               options.merge(:album => "Photos of me"),
               &block
end

.retrieve_all(faceoff, photo_id, options = {}, &block) ⇒ Object

Retrieve all photos in an album, starting at a given photo id. Setting the ‘global’ argument to true will attempt to retrieve all ‘Photos of Me’.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/faceoff/photo.rb', line 59

def self.retrieve_all faceoff, photo_id, options={}, &block
  agent  = faceoff.agent
  user_id = options[:user_id]
  limit  = options[:limit]

  param = "pid=#{photo_id}&id=#{user_id || faceoff.profile_id}"
  param = "#{param}&view=global&subj=#{faceoff.profile_id}" if user_id

  page = agent.get "/photo.php?#{param}"

  photo_count = ($1.to_i - 1) if page.body =~ /Photo \d+ of (\d+)/m
  photo_count = limit if limit && photo_count > limit

  photos = []

  photo_count.times do |i|
    url = page.search("img[@id='myphoto']").first['src']
    photo_id = $1 if page.uri.query =~ %r{pid=(\d+)}

    caption =
      page.search("div[@class='photocaption_text']").first.text rescue nil

    photos << new(photo_id, url, :caption => caption,
                                 :album => options[:album])

    yield photos.last if block_given?

    page = page.link_with(:text => 'Next').click
  end

  photos
end

Instance Method Details

#save!(target = nil) ⇒ Object

Saves the photo to the provided file path.



117
118
119
120
121
122
123
124
125
126
# File 'lib/faceoff/photo.rb', line 117

def save! target=nil
  target ||= File.join ".", @album
  filename = File.join(target, "#{@fid}#{File.extname(@url)}")

  data = Faceoff.download(@url)

  Faceoff.safe_save(filename) do |file|
    file.write data
  end
end