-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_media_box.rb
151 lines (134 loc) · 4.73 KB
/
make_media_box.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'sony_ci_api'
require 'active_support/core_ext/time'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/deep_merge'
# unset the object so it gets completely overwritten below; for console testing.
Object.send(:remove_const, :MediaBoxMaker) if defined? MediaBoxMaker
class MediaBoxMaker
MAX_SIZE = 1000
attr_reader :guids, :params, :errors, :responses
def initialize(guids, params={})
@guids = Array(guids).uniq
@params = default_params.deep_merge(params.deep_stringify_keys)
# assetIds param is constructed from GUID lookups, so ensure it's not part
# of the passed in params.
@params.delete('assetIds')
@errors = []
end
# Makes one or more mediaboxes from the Sony Ci IDs retreived by looking up
# the GUIDs in the Sony Ci API.
# NOTE: When making large media boxes, we were getting "Asset Not Found" errors
# even though all Assets exist. I think the assetIds param was getting cut off
# due to large size, and resulting an a malformed ID, which was resulting in
# the "Asset Not Found" error. As a workaround, we set a MAX_SIZE and create
# however many mediaboxes we need, where none exceed MAX_SIZE. Currently, having
# a MAX_SIZE of 1000 seems to work.
def make!
@responses ||= sony_ci_ids.each_slice(MAX_SIZE).map.with_index do |sony_ci_ids_slice, i|
params_for_slice = params.merge({ "assetIds" => sony_ci_ids_slice })
if sony_ci_ids.count > MAX_SIZE
starts_at = i * MAX_SIZE + 1
ends_at = i * MAX_SIZE + sony_ci_ids_slice.count
slice_details = " (records #{starts_at}-#{ends_at} of #{sony_ci_ids.count})"
params_for_slice['name'] += slice_details
params_for_slice['message'] += slice_details
end
client.post '/mediaboxes', params: params_for_slice
end
rescue => e
@errors << e
nil
end
def default_params
{
"name" => "Test media box",
"type" => "Protected",
"allowSourceDownload" => true,
"allowPreviewDownload" => false,
"allowElementDownload" => false,
"recipients" => [ "[email protected]" ],
"message" => "Test Media Box",
"password" => 'aapb',
"expirationDays" => 1,
"sendNotifications" => true,
"notifyOnOpen" => true,
"notifyOnChange" => true,
"filters" => {
"elements" => {
"types" => [
"Video"
]
}
}
}
end
def client
@client ||= SonyCiApi::Client.new('ci.yml')
end
# Returns a hash of Sony Ci search results keyed by GUID.
# If the GUID search returned results, the value is the first result found.
# If the GUID search returned no results, the value is nil.
# If the GUID search errored, the value is nil and the error is added to
# @errors array.
# Results can take a long time for long lists of GUIDs, to the result is
# memoized for subsequent calls.
# Pass refresh: true to reset the memoized results and redo the search.
def search_results(refresh: false)
@search_results if refresh
@search_results ||= {}.tap do |results|
Array(guids).each do |guid|
search_str = guid[-20..-1]
search_result = begin
client.workspace_search(query: search_str).first
rescue => e
@errors << e
nil
end
results[guid] = search_result
end
end
end
def sony_ci_records
# Any non-nil search result can expected to be single JSON Sony Ci result.
search_results.values.compact
end
# Maps all Sony Ci records to their Sony Ci ID, which is used in the POST
# request to make the media box.
def sony_ci_ids
sony_ci_records.map { |r| r['id'] }
end
def guids_not_found
search_results.select { |guid, result| result.nil? }.keys
end
end
# GUIDs should be listed one per line in a plain text file.
@guids_filename = 'path/to/list_of_guids.txt'
@guids = File.readlines(@guids_filename).map(&:chomp)
# See documentation for all parameter options. These values are just examples.
# https://developers.cimediacloud.com/#mediaboxes-create-mediabox-post
@params = {
"name" => "My Media Box",
"type" => "Protected",
"allowSourceDownload" => true,
"allowPreviewDownload" => true,
"allowElementDownload" => true,
"recipients" => [ "[email protected]" ],
"message" => "",
"password" => '',
"expirationDays" => 365,
"sendNotifications" => true,
"notifyOnOpen" => false,
"notifyOnChange" => true,
"filters" => {
"elements" => {
"types" => [
"Video"
]
}
}
}
@mb = MediaBoxMaker.new(@guids, @params)
# This command does the actual making. Uncomment to make the media box,
# or you can load this script in a ruby console and inspect the object
# to see what GUIDs were not found, or to see errors, etc.
# @mb.make!