namespace :video do desc "get video play data" task :data => :environment do start_times = ['2019-08-19 00:00:00', '2019-11-10 00:00:00'] end_times = ['2019-11-09 23:59:59', '2020-02-06 00:00:00'] Video.all.each do |video| vv = 0 play_duration = 0 start_times.each_with_index do |time, index| start_time = time.to_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ') end_time = end_times[index].to_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ') data = AliyunVod::Service.video_data(video.uuid, start_time, end_time) puts data if data[:VideoPlayStatisDetails].present? sum_duration = data[:VideoPlayStatisDetails][:VideoPlayStatisDetail].map(&:PlayDuration).sum sum_vv = data[:VideoPlayStatisDetails][:VideoPlayStatisDetail].map(&:vv).sum vv += sum_vv play_duration += sum_duration end end video.update!(vv: vv, play_duration: play_duration) puts video.id end end # 执行示例 bundle exec rake video:sample args=2933,'2019-08-19 00:00:00','2019-11-09 23:59:59' task :sample => :environment do if ENV['args'] video_id = ENV['args'].split(",")[0] start_time = ENV['args'].split(",")[1].to_s.to_time.utc.iso8601 # 表示开始时间 end_time = ENV['args'].split(",")[2].to_s.to_time.utc.iso8601 # 表示结束时间 video=Video.find video_id data = AliyunVod::Service.video_data(video.uuid, start_time, end_time) puts data end end task :get_play_url => :environment do Video.all.each do |video| result = AliyunVod::Service.get_play_info(video.uuid) rescue nil if result.present? && result["PlayInfoList"]["PlayInfo"].present? puts result play_url = result["PlayInfoList"]["PlayInfo"].first["PlayURL"] puts play_url # play_url = (result.dig('PlayInfoList', 'PlayInfo') || []).first&.[]('PlayURL') video.update!(play_url: play_url) if play_url.present? end end end end