Skip to content

Instantly share code, notes, and snippets.

@valachi
Created April 1, 2013 15:02
Show Gist options
  • Save valachi/5285423 to your computer and use it in GitHub Desktop.
Save valachi/5285423 to your computer and use it in GitHub Desktop.
#coding: utf-8
class Video < ActiveRecord::Base
mount_uploader :file, FileUploader
mount_uploader :subtitle, SubtitleUploader
belongs_to :show, inverse_of: :videos
has_many :user_subtitles
has_many :watched_episodes
has_many :users_who_watched, through: :watched_episodes, source: :user
scope :converted, where(progress: 100)
validates_presence_of :season, :number, :file
validates :number, uniqueness: { scope: [:season, :show_id], message: "У этого шоу уже есть такой эпизод (те же номер и сезон)" }
around_save :process_video, if: :file_changed?
before_update :unverify, if: :file_changed?
before_save :fetch_imdb_info, if: :sync_imdb?
after_save :remove_missing_episodes
before_save :edit_title, if: :double?
def next
@next ||= Video.converted.where(
number: number + 1,
season: season,
show_id: show.id
).first
end
def prev
@prev ||= Video.converted.where(
number: number - 1,
season: season,
show_id: show.id
).first
end
def show_name=(name)
self.show = Show.where('lower(name) = ?', name).first
end
def subtitle_text=(content)
self.subtitle = FileLike.new "track_#{Time.now.to_i}.srt", content
end
def self.total_video_size
Video.pluck(:file_size).sum
end
# Episodes rake task
def update_imdb_info
return if title? and plot? and airdate?
fetch_imdb_info
save!
end
private
def fetch_imdb_info
imdb_show ||= Imdb::TvShow.new(show.imdb_id)
if imdb_show && imdb_show.is_a_tvshow?
episode = imdb_show.season(season).episode(number.to_i)
self.title = episode.title unless double? and title?
self.airdate = episode.airdate.to_date + 1.day
self.plot = episode.plot
else
self.info_status = 'Error with imdb_show'
end
rescue Exception => e
self.info_status = e.message
end
def convert
path = file.current_path
convert_to :webm, path
new_path = path.sub /\.[^.]+$/, '.webm'
self.update_attributes file: File.open(new_path)
convert_to :mp4, new_path
end
def convert_to(type, path)
video = Voyeur::Media.new filename: path
video.convert(to: type) do |time|
progress_time = Voyeur::MediaTime.new(time).to_seconds
video_duration = Voyeur::MediaTime.new(video.raw_duration).to_seconds
percentage = (progress_time.to_f/video_duration.to_f) * 100
self.update_column :progress, percentage
end
end
def process_video
webm? ? self.progress = 100 : self.progress = 0
self.file_size = self.file.size.to_i / (1024*1024)
yield
delay(queue: "videos").convert unless progress == 100
end
def webm?
file.current_path.end_with? 'webm'
end
def unverify
self.verified = nil
end
def sync_imdb?
number_changed? or season_changed? or show_id_changed? or new_record?
end
def remove_missing_episode(num)
MissingEpisode.destroy_all show_id: show_id, season: season, number: num
end
def remove_missing_episodes
remove_missing_episode(number)
remove_missing_episode(number.to_i+1) if double?
end
def edit_title
self.title = title.gsub(/: Part 1$/, '') if title
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment