Class: Picker::Facebook

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

Constant Summary collapse

APP_ID =
"238077826328712"
SECRET =
"17859f155d2d7734cabb3e1b78d3306b"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFacebook

Returns a new instance of Facebook.



8
9
10
11
12
13
14
15
16
17
# File 'lib/picker/facebook.rb', line 8

def initialize
  @app = FbGraph::Application.new(APP_ID, :secret => SECRET)

  data = read_yaml
  unless data[:fb_long_token] && @app.debug_token(data[:fb_long_token]).is_valid
    save_access_token
  end

  token
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/picker/facebook.rb', line 6

def app
  @app
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/picker/facebook.rb', line 6

def user
  @user
end

Instance Method Details

#download_albums_via_fqlObject



71
72
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
# File 'lib/picker/facebook.rb', line 71

def download_albums_via_fql
  # create a directory for this user
  path = File.join(ENV['HOME'], "picker", @user.name)
  FileUtils.mkdir_p path

  # change to this directory
  Dir.chdir(path) do
    @all_photos = []

    albums = query "SELECT aid, name, photo_count FROM album WHERE owner = #{@user.identifier}"
    albums.each do |album|
      FileUtils.mkdir_p album["name"]
      progress = ProgressBar.create(
        :title => album["name"],
        :total => album["photo_count"],
        :format => '%t ( %c/%C ) |%b>>%i| %f'
      )
      photos = query "SELECT pid, caption, images FROM photo WHERE aid = \"#{album["aid"]}\""
      Dir.chdir(album["name"]) do
        photos.each do |photo|
          unless @all_photos.include?(photo["pid"])
            @all_photos.push photo["pid"]
            # select the largest image for download
            size = 0; source = "";
            photo["images"].each do |img|
              dim = img["height"] * img["width"]
              if size < dim
                size = dim
                source = img["source"]
              end
            end
            system("wget -qct 3 #{source}") unless File.exists?(File.basename(source))
          end
          progress.increment
        end
      end
    end
  end
end

#download_tagged_via_fqlObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/picker/facebook.rb', line 111

def download_tagged_via_fql
  # create a directory for this user
  path = File.join(ENV['HOME'], "picker", @user.name)
  FileUtils.mkdir_p path

  # change to this directory
  Dir.chdir(path) do
    # download all tagged images for this user
    album_name = "Tagged Photos"
    FileUtils.mkdir_p album_name
    photos = query "SELECT pid, caption, images FROM photo WHERE pid IN
                    (SELECT pid FROM photo_tag WHERE subject = #{@user.identifier})"
    progress = ProgressBar.create(
      :title => album_name,
      :total => photos.count,
      :format => '%t ( %c/%C ) |%b>>%i| %f'
    )
    Dir.chdir(album_name) do
      photos.each do |photo|
        unless @all_photos.include?(photo["pid"])
          @all_photos.push photo["pid"]
          # select the largest image for download
          size = 0; source = "";
          photo["images"].each do |img|
            dim = img["height"] * img["width"]
            if size < dim
              size = dim
              source = img["source"]
            end
          end
          system("wget -qct 3 #{source}") unless File.exists?(File.basename(source))
          progress.increment
        end
      end
    end

  end
end

#find_female_friendsObject



61
62
63
64
# File 'lib/picker/facebook.rb', line 61

def find_female_friends
  response = query "SELECT uid, name, sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
  response.reject{|x| x["sex"] == "male"}
end

#get_long_tokenObject



35
36
37
38
39
40
41
# File 'lib/picker/facebook.rb', line 35

def get_long_token
  uri   = "https://graph.facebook.com/oauth/access_token"
  data  = "client_id=#{APP_ID}&client_secret=#{SECRET}&"
  data += "grant_type=fb_exchange_token&fb_exchange_token=#{@access_token}"
  response = `curl -s -d "#{data}" "#{uri}"`
  @long_token = response.split("&").first.split("=").last
end

#get_oauth_tokenObject



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

def get_oauth_token
  scope      = ["user_photos", "friends_photos", "user_about_me", "friends_about_me"]
  oauth_uri  = "https://www.facebook.com/dialog/oauth?client_id=#{APP_ID}"
  oauth_uri += "&redirect_uri=https://www.facebook.com/connect/login_success.html"
  oauth_uri += "&scope=" + scope.join(",")
  oauth_uri += "&response_type=token"

  say "Copy the following URI and open it in browser."
  say "Then, paste the URI for the page with \"success\" message here.."
  say oauth_uri
  puts

  success_uri = ask("Success URI?  ").strip
  @access_token = URI.parse(success_uri).fragment.split("&").first.split("=").last
end

#query(query) ⇒ Object



57
58
59
# File 'lib/picker/facebook.rb', line 57

def query(query)
  FbGraph::Query.new(query).fetch(@token)
end

#save_access_tokenObject



43
44
45
46
47
48
49
50
# File 'lib/picker/facebook.rb', line 43

def save_access_token
  data = read_yaml
  data = data.merge({
    fb_access_token: get_oauth_token,
    fb_long_token:   get_long_token
  })
  write_yaml(data)
end

#tokenObject



52
53
54
55
# File 'lib/picker/facebook.rb', line 52

def token
  data = read_yaml
  @token = data[:fb_long_token]
end