Module: Forki

Extended by:
Configuration
Defined in:
lib/forki/scrapers/user_scraper.rb,
lib/forki.rb,
lib/forki/post.rb,
lib/forki/user.rb,
lib/forki/version.rb,
lib/forki/scrapers/scraper.rb,
lib/forki/scrapers/post_scraper.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: BlockedCredentialsError, ContentUnavailableError, Error, InvalidUrlError, MissingCredentialsError, Post, PostScraper, RetryableError, Scraper, UnhandledContentError, User, UserScraper, VideoSieveFailedError

Constant Summary collapse

VERSION =
"0.2.20"
@@forki_logger =
Logger.new(STDOUT)

Class Method Summary collapse

Methods included from Configuration

configuration, define_setting

Class Method Details

.create_temp_storage_locationObject



95
96
97
98
99
# File 'lib/forki.rb', line 95

def self.create_temp_storage_location
  return if File.exist?(Forki.temp_storage_location) && File.directory?(Forki.temp_storage_location)

  FileUtils.mkdir_p Forki.temp_storage_location
end

.extract_file_extension_from_url(url) ⇒ Object

Extract the file extension from a media URL E.g. “.png” from scontent-atl3-2.xx.fbcdn.net/v/t39.30808-1.png?stp=dst-png_p148x148



62
63
64
65
66
67
68
69
70
71
# File 'lib/forki.rb', line 62

def self.extract_file_extension_from_url(url)
  return nil if url.nil?

  stripped_url = url.split("?").first  # remove URL query params
  extension = stripped_url.split(".").last

  extension = nil unless /^[a-zA-Z0-9]{3}$/.match?(extension)  # extension must be a 3-character alphanumeric string
  extension = ".#{extension}" unless extension.nil?
  extension
end

.retrieve_media(url) ⇒ Object

Get an image from a URL and save to a temp folder set in the configuration under temp_storage_location



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/forki.rb', line 75

def self.retrieve_media(url)
  @@forki_logger.debug("Forki download started: #{url}")
  start_time = Time.now

  response = Typhoeus.get(url)

  extension = Forki.extract_file_extension_from_url(url)
  temp_file = "#{Forki.temp_storage_location}/facebook_media_#{SecureRandom.uuid}#{extension}"

  # We do this in case the folder isn't created yet, since it's a temp folder we'll just do so
  create_temp_storage_location
  File.binwrite(temp_file, response.body)

  @@forki_logger.debug("Forki download finished")
  @@forki_logger.debug("Save Location: #{temp_file}")
  @@forki_logger.debug("Size: #{(File.size("./#{temp_file}").to_f / 1024 / 1024).round(4)} MB")
  @@forki_logger.debug("Time to Download: #{(Time.now - start_time).round(3)} seconds")
  temp_file
end

.set_logger_levelObject



101
102
103
104
105
106
107
# File 'lib/forki.rb', line 101

def self.set_logger_level
  if ENV["RAILS_ENV"] == "test" || ENV["RAILS_ENV"] == "development"
    @@forki_logger.level = Logger::INFO
  else
    @@forki_logger.level = Logger::WARN
  end
end