Skip to content

Commit

Permalink
Use reachable flag to track video status
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubery committed Sep 30, 2024
1 parent ccf2f71 commit 761988f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 1 addition & 2 deletions app/models/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def generate_podcast(current_url, audio_url:, video_url:)
guid = media_item.url
item.link(media_item_link)

reachable_suffix = media_item.reachable ? '' : 'Unreachable'
title = media_item.title + reachable_suffix
title = media_item.title

item.title(title)

Expand Down
19 changes: 15 additions & 4 deletions app/models/media_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,37 @@ def article_url
end

def has_all_details?
[title, description, duration_seconds].all?(&:present?)
[(reachable == true), title, description, duration_seconds].all?(&:present?)
end

def fill_missing_details
return if has_all_details?
return if reachable == false # nil means unknown

# Tried within the last 30 minutes
return if updated_at && updated_at != created_at && (updated_at - Time.now) < 30.minutes

self.updated_at = Time.now
if guid.nil?
self.guid = url
end

if %r{\Ahttps://(www\.)?(youtube|vimeo)\.com/} =~ url || %r{\Ahttps://youtu\.be/} =~ url
self.mime_type = VIDEO_MIME_TYPE
info = Youtube::Video.new(url).get_information
if info.present? && !info["is_live"]

# Wait for stream to finish
return if info["is_live"]

if info.present?
success = !!info["id"]

if success && info['extractor'] == 'youtube'
video = Youtube::Video.from_id(info["id"])
self.guid = video.guid
self.url = video.url
end

if success || (created_at && created_at < 3.days.ago)
self.reachable = success
self.author = info["uploader"] || ''
Expand All @@ -58,10 +70,9 @@ def fill_missing_details
self.description = "Original Video: #{url}\nPublished At: #{published_at}\n #{info["description"]}"
self.duration_seconds = info["duration"] || 0
self.thumbnail_url = info["thumbnails"]&.last&.fetch("url", "") || ''
self.mime_type = VIDEO_MIME_TYPE
save!
end
end
save!
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeColumnDefaultMediaItems < ActiveRecord::Migration[8.0]
def change
change_column_default :media_items, :reachable, from: true, to: nil
change_column_null :media_items, :reachable, true
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/articles/two.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h1>An email</h1>
copy_url:
created_at: 1970-01-01 00:00:00 UTC
updated_at: 1970-01-01 00:00:01 UTC
reachable: true
reachable:
guid: http://localhost.local/media_items/42
sent_to:
-->
Expand Down

0 comments on commit 761988f

Please sign in to comment.