├── Gemfile ├── Gemfile.lock ├── README.md └── photo_friend_finder.rb /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'nokogiri' 4 | gem 'koala' -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | addressable (2.4.0) 5 | faraday (0.9.2) 6 | multipart-post (>= 1.2, < 3) 7 | koala (2.3.0) 8 | addressable 9 | faraday 10 | multi_json 11 | mini_portile2 (2.0.0) 12 | multi_json (1.12.1) 13 | multipart-post (2.0.0) 14 | nokogiri (1.6.7.2) 15 | mini_portile2 (~> 2.0.0.rc2) 16 | 17 | PLATFORMS 18 | ruby 19 | 20 | DEPENDENCIES 21 | koala 22 | nokogiri 23 | 24 | BUNDLED WITH 25 | 1.10.6 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | To use the script: 2 | 3 | ``` 4 | git clone https://github.com/bknarendra/fb_photo_friend_finder.git 5 | cd fb_photo_friend_finder 6 | bundle install 7 | ``` 8 | 9 | You need to get the FB access token to allow uploading temporary photos to FB. Use the [Graph API Explorer](https://developers.facebook.com/tools/explorer#_=_) to get a token that has the permissions `publish_actions` and `user_photos`. Copy and paste the access token in the script. 10 | 11 | Next you need the FB cookies. 12 | - Open facebook.com in Chrome and login. 13 | - Then open Developer Tools and switch to Network Tab. 14 | - Reload the page. 15 | - Right click on any request which goes to facebook.com and select Copy as cURL option. It will copy that request as a cURL command. 16 | - Paste it in some editor. It will look something like this. 17 | curl 'https://www.facebook.com/photo.php?fbid=&set=' -H 'accept-encoding: gzip, deflate, sdch, br' -H 'accept-language: en-US,en;q=0.8' -H 'authority: www.facebook.com' -H 'cookie: cookies' --compressed 18 | - Copy the cookie block (everything in '' which has cookie written) 19 | - Paste that in the script and save. 20 | 21 | To run the script. 22 | 23 | ``` 24 | ruby photo_friend_finder.rb "path to the photo" 25 | ``` 26 | -------------------------------------------------------------------------------- /photo_friend_finder.rb: -------------------------------------------------------------------------------- 1 | require 'koala' 2 | require 'json' 3 | require 'nokogiri' 4 | 5 | def flatten_nested_hash(content) 6 | content.flat_map{|k, v| [k, *(v.respond_to?(:flat_map) ? flatten_nested_hash(v): v)]} 7 | end 8 | 9 | 10 | ACCESS_TOKEN="your access token goes here" 11 | COOKIES = "your FB cookies go here" 12 | 13 | 14 | @graph = Koala::Facebook::API.new(ACCESS_TOKEN) 15 | 16 | file = ARGV[0] 17 | puts "uploading file: #{file}" 18 | photo = @graph.put_picture(file, {:published => false, :temporary=> true}) 19 | puts "upload response: #{photo}" 20 | puts "waiting for photo to get processed" 21 | found = false 22 | 23 | 3.times do 24 | sleep(3) 25 | puts "calling api to get friends in photo" 26 | cmd = "curl -s 'https://www.facebook.com/ajax/pagelet/generic.php/PhotoViewerInitPagelet?dpr=2&data=%7B%22fbid%22%3A%22#{photo['id']}%22%2C%22data_ft%22%3A%7B%7D%7D&__a=1&__req=jsonp_4&__pc=PHASED%3ADEFAULT' -H '#{COOKIES}'" 27 | response = %x(#{cmd}) 28 | content = JSON.parse(response.sub(/^[^{]*/,'')) 29 | content = flatten_nested_hash(content) 30 | content = content.select{|x| x if x.respond_to?(:index) && x.index("fbPhotosPhotoTagboxes")}[0] 31 | content = Nokogiri::HTML(content) 32 | people_in_photo = content.css(".fbPhotosPhotoTagboxBase.faceBox").length 33 | identified_friends_in_photo = content.css(".faceboxSuggestion").map{|x| x.attributes["data-text"].value} 34 | if people_in_photo > 0 35 | puts "Found #{people_in_photo} people in the photo" 36 | puts "identified people in the photo: #{identified_friends_in_photo * ", "}" 37 | found = true 38 | break 39 | end 40 | end 41 | 42 | puts "tried 3 times couldn't find any friends in photo. Some problem." if !found 43 | puts "deleting photo. response : #{@graph.delete_object(photo["id"])}" --------------------------------------------------------------------------------