Skip to content

Commit

Permalink
Fix up some structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cguess committed Nov 8, 2024
1 parent cd866dc commit 9fde40d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/forki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize(msg = "Forki does not know how to handle the post")
class VideoSieveFailedError < StandardError
def initialize(msg = "Video sieve failed to find a video", sieve_class: VideoSieve)
self.msg = "#{sieve_class} failed to find a video" if msg.nil?
super
# super(self.msg)
end
end

Expand Down
14 changes: 10 additions & 4 deletions lib/forki/scrapers/post_scraper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,16 @@ def extract_video_post_data(graphql_strings)
reshare_warning = feedback_object["comet_ufi_summary_and_actions_renderer"]["feedback"]["should_show_reshare_warning"]
end

video_object_url_subsearch = video_object
video_object_url_subsearch = video_object_url_subsearch["videoDeliveryLegacyFields"] if video_object_url_subsearch.has_key?("videoDeliveryLegacyFields")
# if video_object.key?("browser_native_hd_url") || video_object.key?("browser_native_sd_url")
video_url = video_object_url_subsearch["browser_native_hd_url"] || video_object_url_subsearch["browser_native_sd_url"]
if video_object.has_key?("videoDeliveryResponseFragment")
progressive_urls_wrapper = video_object["videoDeliveryResponseFragment"]["videoDeliveryResponseResult"]
video_url = progressive_urls_wrapper["progressive_urls"].find_all { |object| !object["progressive_url"].nil? }.last["progressive_url"]
else
video_object_url_subsearch = video_object
video_object_url_subsearch = video_object_url_subsearch["videoDeliveryLegacyFields"] if video_object_url_subsearch.has_key?("videoDeliveryLegacyFields")
video_url = video_object_url_subsearch["browser_native_hd_url"] || video_object_url_subsearch["browser_native_sd_url"]
end

video_url = "" if video_url.nil?

post_details = {
id: video_object["id"],
Expand Down
6 changes: 6 additions & 0 deletions lib/forki/scrapers/sieves/video_sieves/video_sieve_reel_2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def self.sieve(graphql_objects)
video_preview_image_url = video_object["short_form_video_context"]["playback_video"]["preferred_thumbnail"]["image"]["uri"]
video_url = video_object["short_form_video_context"]["playback_video"]["browser_native_hd_url"] || video_object["short_form_video_context"]["playback_video"]["browser_native_sd_url"]

if !video_object.dig("short_form_video_context", "playback_video").nil?
video_object_url_subsearch = video_object["short_form_video_context"]["playback_video"]
progressive_urls_wrapper = video_object_url_subsearch["videoDeliveryResponseFragment"]["videoDeliveryResponseResult"]
video_url = progressive_urls_wrapper["progressive_urls"].find_all { |object| !object["progressive_url"].nil? }.last["progressive_url"]
end

if video_url.nil? && video_object["short_form_video_context"]["playback_video"].has_key?("videoDeliveryLegacyFields")
video_url = video_object["short_form_video_context"]["playback_video"]["videoDeliveryLegacyFields"]["browser_native_hd_url"]
video_url = video_object["short_form_video_context"]["playback_video"]["videoDeliveryLegacyFields"]["browser_native_sd_url"] if video_url.nil?
Expand Down
15 changes: 13 additions & 2 deletions lib/forki/scrapers/sieves/video_sieves/video_sieve_watch_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ def self.sieve(graphql_objects)
video_url = video_object["attachments"]&.first.dig("media", "videoDeliveryLegacyFields", "browser_native_hd_url") if video_url.nil?
video_url = video_object["attachments"]&.first.dig("media", "videoDeliveryLegacyFields", "browser_native_sd_url") if video_url.nil?

raise VideoSieveFailedError.new(sieve_class: "VideoSieveWatchTab") if video_url.nil?
if video_url.nil? && video_object["attachments"].first["media"].has_key?("videoDeliveryResponseFragment")
progressive_urls_wrapper = video_object["attachments"].first["media"]["videoDeliveryResponseFragment"]["videoDeliveryResponseResult"]
video_url = progressive_urls_wrapper["progressive_urls"].find_all { |object| !object["progressive_url"].nil? }.last["progressive_url"]
end
# else
# video_object_url_subsearch = video_object
# video_object_url_subsearch = video_object_url_subsearch["videoDeliveryLegacyFields"] if video_object_url_subsearch.has_key?("videoDeliveryLegacyFields")
# video_url = video_object_url_subsearch["browser_native_hd_url"] || video_object_url_subsearch["browser_native_sd_url"]
# end


raise Forki::VideoSieveFailedError.new(sieve_class: "VideoSieveWatchTab") if video_url.nil?

video_preview_image_url = video_object["attachments"]&.first.dig("media", "preferred_thumbnail", "image", "uri")
video_preview_image_url = video_object["short_form_video_context"]["video"]["first_frame_thumbnail"] if video_preview_image_url.nil?

raise VideoSieveFailedError.new(sieve_class: "VideoSieveWatchTab") if video_preview_image_url.nil?
raise Forki::VideoSieveFailedError.new(sieve_class: "VideoSieveWatchTab") if video_preview_image_url.nil?

if !video_object["feedback_context"].nil?
feedback_object = video_object["feedback_context"]["feedback_target_with_context"]
Expand Down
2 changes: 1 addition & 1 deletion lib/forki/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Forki
VERSION = "0.2.21"
VERSION = "0.2.22"
end
6 changes: 3 additions & 3 deletions test/post_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_a_video_post_by_a_user_returns_properly_when_scraped
end
end

def test_a_video_post_by_a_page_retuns_properly_when_scraped
def test_a_video_post_by_a_page_retuns_properly_when_scraped # 777777777777777
urls = %w[https://www.facebook.com/161453087348302/videos/684374025476745/
https://www.facebook.com/AmericaFirstAction/videos/323018088749144/
https://www.facebook.com/Meta/videos/264436895517475]
Expand All @@ -138,7 +138,7 @@ def test_a_video_post_by_a_page_retuns_properly_when_scraped
end
end

def test_a_video_in_the_watch_tab_returns_properly_when_scraped
def test_a_video_in_the_watch_tab_returns_properly_when_scraped # 777777777777777777777
urls = %w[https://www.facebook.com/watch/?v=2707731869527520
https://www.facebook.com/watch/?v=3743756062349219]
posts = Forki::Post.lookup(urls)
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_a_url_that_seems_to_fail_2
# end

def test_a_video_without_text_works
post = Forki::Post.lookup("https://www.facebook.com/100000568872011/videos/2240868886282175")
post = Forki::Post.lookup("https://www.facebook.com/watch/live/?ref=watch_permalink&v=535737772684504")
assert_not_nil(post)

assert File.size(post.first.video_file) > 1000
Expand Down

0 comments on commit 9fde40d

Please sign in to comment.