Skip to content

Commit

Permalink
Fix up video processing
Browse files Browse the repository at this point in the history
  • Loading branch information
cguess committed Dec 14, 2023
1 parent f0da324 commit 39d1cf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
20 changes: 9 additions & 11 deletions lib/mosquito.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,23 @@ def self.tweet_fields

# Get media from a URL and save to a temp folder set in the configuration under
# temp_storage_location
def self.retrieve_media(url)
def self.retrieve_media(url, extension: nil)
return "" if url.nil?
return "" if !Mosquito.save_media

response = Typhoeus.get(url)

# Get the file extension if it's in the file
begin
if extension.nil?
extension = url.split(".").last
rescue StandardError => e
debugger
end

# Do some basic checks so we just empty out if there's something weird in the file extension
# that could do some harm.
if extension.length.positive?
extension = extension[0...extension.index("?")]
extension = nil unless /^[a-zA-Z0-9]+$/.match?(extension)
extension = ".#{extension}" unless extension.nil?
# Do some basic checks so we just empty out if there's something weird in the file extension
# that could do some harm.
if extension.length.positive?
extension = extension[0...extension.index("?")]
extension = nil unless /^[a-zA-Z0-9]+$/.match?(extension)
extension = ".#{extension}" unless extension.nil?
end
end

temp_file_name = "#{Mosquito.temp_storage_location}/#{SecureRandom.uuid}#{extension}"
Expand Down
6 changes: 3 additions & 3 deletions lib/mosquito/scrapers/tweet_scraper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def parse(id)
# Video
nodes = doc.xpath("//div[contains(@class, 'main-tweet')]/div/div/div[contains(@class, 'attachments')]/div[contains(@class, 'gallery-video')]/div/video")
unless nodes.empty?
video_preview_image = nodes.first["poster"]
videos.concat(nodes.map { |node| "#{Capybara.app_host}#{node.xpath("//source").first["src"]}" })
video_preview_image = Mosquito.retrieve_media("#{Capybara.app_host}#{nodes.first["poster"]}", extension: ".jpg")
videos.concat(nodes.map { |node| Mosquito.retrieve_media(node.xpath("//source").first["src"]) })
video_file_type = "video" # This is always video now, sing a gif isn't displayed differently
end

# GIF
nodes = doc.xpath("//div[contains(@class, 'main-tweet')]/div/div/div[contains(@class, 'attachments')]/div[contains(@class, 'gallery-gif')]/div/video")
unless nodes.empty?
video_preview_image = nodes.first["poster"]
video_preview_image = Mosquito.retrieve_media(nodes.first["poster"], extension: ".jpg")
videos.concat(nodes.map { |node| Mosquito.retrieve_media("#{Capybara.app_host}#{node.xpath("//source[1]/source/@src").first&.content}") })
video_file_type = "gif"
end
Expand Down

0 comments on commit 39d1cf6

Please sign in to comment.