Skip to content

Instantly share code, notes, and snippets.

@kkxlkkxllb
Created November 1, 2012 03:39
Show Gist options
  • Select an option

  • Save kkxlkkxllb/3991532 to your computer and use it in GitHub Desktop.

Select an option

Save kkxlkkxllb/3991532 to your computer and use it in GitHub Desktop.
class RecordingEpg
RSID = "SCSP"
TSID = "ISPACE DS"
def self.get_epg(args={})
args = {
:spid => $rca_config[:kuanyun_spid],
:appid => $rca_config[:kuanyun_appid],
:time_stamp => Date.today.to_time.strftime('%Y-%m-%d %H:%M:%S'),
:correlate_id => "#{Time.current.to_i}",
:uuid => "1104003091800024C12A36C5"
}.update args
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<message version="1.0">
<header CorrelateID='#{args[:correlate_id]}' RequestSystemID='#{RSID}' TargetSystemID='#{TSID}' Action='REQUEST' command='ISPACE_CHANNEL_SCHEDULE' Timestamp='#{args[:time_stamp]}'/>
<body>
<request UUID='#{args[:uuid]}' SPID='#{args[:spid]}' AppID='#{args[:appid]}' AccessToken=''></request>
</body>
</message>
XML
p xml
result = YunMedia.interface_request(xml)
p result
if result && result["result"]["code"] == 0
@data = result["channels"].inject([]){|a,x|
programs = x["assetPrograms"].inject([]){|b,y|
b << {
:asset_id => y["assetID"],
:start_time => Time.parse(y["startDateTime"]),
:end_time => Time.parse(y["endDateTime"]),
:program_name => y["programName"],
:description => y["description"],
:duration => y["duration"].to_i,
:channelID => y["channelID"],
:category => y["category"],
:status => y["status"]
}
}
physicals = x["physicalChannels"].inject([]){|c,z|
c << {
:asset_id => z["assetID"],
:encoding_profile => z["encodingProfile"],
:video_codec => z["videoCodec"],
:audio_codec => z["audioCodec"],
:hd_flag => z["hDFlag"],
:channelID => z["channelID"],
:bit_rate => z["bitRate"],
:mime_type => z["mimeType"]
}
}
a << {
:channel => {
:title_full => x["titleFull"],
:code => x["channelCode"],
:ctype => x["type"],
:status => x["status"]
},
:programs => programs,
:physicals => physicals
}
}
else
puts "server error"
end
end
# rake em job
# epg数据更新方案
# channel 信息基本不会变更
# 请求参数只有timstamp,为当天起始时间,所以只拉1天内的epg
# 以天为单位,查program asset_id,对应信息有无变化
def self.sync
# get_epg
# store epg data to recording_channel & recording_program
data = RecordingEpg.get_epg
data.each do |d|
c = RecordingChannel.where(:code => d[:channel][:code]).first
# channel不存在则新建
if c
c.update_attributes(d[:channel])
else
c = RecordingChannel.create(d[:channel])
end
# 一天的节目
d[:programs].each do |p|
# 同channel 下program start_time 唯一,asset_id 唯一索引
prog = c.recording_programs.where(:asset_id => p[:asset_id]).first
if prog
# 如果已存在则更新字段
prog.update_attributes(p)
else
# 不存在则新建
c.recording_programs.create(p)
end
end
# 可能存在多个物理频道,画面质量不同
d[:physicals].each do |h|
# asset_id 唯一索引
phy_c = c.physical_channels.where(:asset_id => h[:asset_id]).first
unless phy_c
c.physical_channels.create(h)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment