From 02fa5526b3592da154da1d4f05f6d90d2ba718e2 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Tue, 1 Jan 2019 16:28:54 -0800 Subject: [PATCH] Remove API usage from Facebook download endpoint.. it didn't seem to work anymore anyway. --- app.rb | 101 ++++++++++++--------------------------------------------- 1 file changed, 21 insertions(+), 80 deletions(-) diff --git a/app.rb b/app.rb index 3c6d20e..5ddf063 100644 --- a/app.rb +++ b/app.rb @@ -424,8 +424,6 @@ end get "/facebook/download" do - return [404, "Facebook credentials not configured"] if ENV["FACEBOOK_APP_ID"].empty? || ENV["FACEBOOK_APP_SECRET"].empty? - if /\/(?\d+)/ =~ params[:url] # https://www.facebook.com/infectedmushroom/videos/10153430677732261/ # https://www.facebook.com/infectedmushroom/videos/vb.8811047260/10153371214897261/?type=2&theater @@ -436,88 +434,31 @@ id = params[:url] end - response = Facebook.get("/", query: { id: id, metadata: "1" }) + response = HTTP.get("https://www.facebook.com/#{id}") + response = HTTP.get(response.redirect_url) if response.redirect_same_origin? if response.success? - type = response.json["metadata"]["type"] - if type == "video" - response = Facebook.get("/", query: { id: id, fields: "source,created_time,title,description,live_status,from" }) - data = response.json - fn = "#{data["created_time"].to_date} - #{data["title"] || data["description"] || data["from"]["name"]}#{" (live)" if data["live_status"]}.mp4".to_filename - url = if data["live_status"] == "LIVE" - "https://www.facebook.com/video/playback/playlist.m3u8?v=#{data["id"]}" - else - data["source"] - end - - if env["HTTP_ACCEPT"] == "application/json" - content_type :json - return { - url: url, - filename: fn, - live: (data["live_status"] == "LIVE"), - }.to_json - end - - if data["live_status"] == "LIVE" - "ffmpeg -i \"#{url}\" \"#{fn}\"" - else - redirect url - end - elsif type == "photo" - response = Facebook.get("/", query: { id: id, fields: "images,created_time,name,from" }) - data = response.json - image = data["images"][0] - url = image["source"] - fn = "#{data["created_time"].to_date} - #{data["name"] || data["from"]["name"]}.jpg".to_filename - - if env["HTTP_ACCEPT"] == "application/json" - content_type :json - return { - url: url, - filename: fn, - }.to_json - end - - redirect url - else - return [404, "Unknown type (#{type})."] + if /hd_src(?:_no_ratelimit)?:"(?[^"]+)"/ =~ response.body + elsif /https:\/\/[^"]+_#{id}_[^"]+\.jpg[^"]+/o =~ response.body + # This is not the best quality of the picture, but it will have to do + url = CGI.unescapeHTML($&) end - else - if response.json["error"]["code"] == 100 - # The video/photo is probably uploaded by a regular Facebook user (i.e. not uploaded to a page), which we can't get info from via the API. - # Video example: https://www.facebook.com/ofer.dikovsky/videos/10154633221533413/ - # Photo example: 1401133169914577 - response = Facebook.get("https://www.facebook.com/#{id}") - response = Facebook.get(response.redirect_url) if response.redirect_same_origin? - if response.success? - if /hd_src_no_ratelimit:"(?[^"]+)"/ =~ response.body - elsif /https:\/\/[^"]+_#{id}_[^"]+\.jpg[^"]+/o =~ response.body - # This is not the best quality of the picture, but it will have to do - url = CGI.unescapeHTML($&) - end - if /]*>(?[^<]+)<\/title>/ =~ response.body && /data-utime="(?<utime>\d+)"/ =~ response.body - title = title.force_encoding("UTF-8").gsub(" | Facebook", "") - created_time = Time.at(utime.to_i) - fn = "#{created_time.to_date} - #{title}.#{url.url_ext}".to_filename - end - if url - if env["HTTP_ACCEPT"] == "application/json" - content_type :json - return { - url: url, - filename: fn, - }.to_json - end - redirect url - else - return [404, "Video/photo not found."] - end - else - return [response.code, "https://www.facebook.com/#{id} returned #{response.code}"] + if !url + return [404, "Video/photo not found."] + end + if env["HTTP_ACCEPT"] == "application/json" + content_type :json + fn = nil + if /<title[^>]*>(?<title>[^<]+)<\/title>/ =~ response.body && /data-utime="(?<utime>\d+)"/ =~ response.body + title = title.force_encoding("UTF-8").gsub(" | Facebook", "") + created_time = Time.at(utime.to_i) + fn = "#{created_time.to_date} - #{title}.#{url.url_ext}".to_filename end - else - return [response.code, response.json.to_json] + return { + url: url, + filename: fn, + }.to_json end + redirect url end end