static void SampleMetadataOperations()
        {
            // The metadata field we'll add/update
            string metaDataFieldName = "SubtitleFormat";
            string fieldValue = "VobSub";

            // The Schema file for the field
            // Currently, you must build the xsd yourself. There is no utility provided.
            string xsdFile = "MetadataSchema.xsd";

            KalturaClient client = new KalturaClient(GetConfig());

            // start new session (client session is enough when we do operations in a users scope)
            string ks = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");
            client.KS = ks;

            // Setup a pager and search to use
            KalturaFilterPager pager = new KalturaFilterPager();
            KalturaMediaEntryFilter search = new KalturaMediaEntryFilter();
            search.OrderBy = KalturaMediaEntryOrderBy.CREATED_AT_ASC;
            search.MediaTypeEqual = KalturaMediaType.VIDEO;  // Video only
            pager.PageSize = 10;
            pager.PageIndex = 1;

            Console.WriteLine("List videos, get the first one...");

            // Get 10 video entries, but we'll just use the first one returned
            IList<KalturaMediaEntry> entries = client.MediaService.List(search, pager).Objects;
            // Check if there are any custom fields defined in the KMC (Settings -> Custom Data)
            // for the first item returned by the previous listaction
            KalturaMetadataProfileFilter filter = new KalturaMetadataProfileFilter();
            IList<KalturaMetadataProfile> metadata = client.MetadataProfileService.List(filter, pager).Objects;
            int profileId = 0;
            string name = "";
            string id = "";

            if (metadata.Count > 0)
            {
                profileId = metadata[0].Id;
                name = entries[0].Name;
                id = entries[0].Id;
                Console.WriteLine("1. There are custom fields for video: " + name + ", entryid: " + id);
            }
            else
            {
                Console.WriteLine("1. This publisher account doesn't have any custom metadata profiles enabled.");
                Console.WriteLine("Exiting the metadata test (enable customer metadata in Admin Console and create a profile in KMC first).");
                return;
            }

            // Add a custom data entry in the KMC  (Settings -> Custom Data)
            KalturaMetadataProfile profile = new KalturaMetadataProfile();
            profile.MetadataObjectType = KalturaMetadataObjectType.ENTRY;
            profile.Name = metadata[0].Name;
            string viewsData = "";

            StreamReader fileStream = File.OpenText(xsdFile);
            string xsd = fileStream.ReadToEnd();
            KalturaMetadataProfile metadataResult = client.MetadataProfileService.Update(profileId, profile, xsd, viewsData);

            if (metadataResult.Xsd != null)
            {
                Console.WriteLine("2. Successfully created the custom data field " + metaDataFieldName + ".");
            } else {
                Console.WriteLine("2. Failed to create the custom data field.");
            }

            // Add the custom metadata value to the first video
            KalturaMetadataFilter filter2 = new KalturaMetadataFilter();
            filter2.ObjectIdEqual = entries[0].Id;
            string xmlData = "<metadata><SubtitleFormat>" + fieldValue + "</SubtitleFormat></metadata>";
            KalturaMetadata metadata2 = client.MetadataService.Add(profileId, profile.MetadataObjectType, entries[0].Id, xmlData);

            if (metadata2.Xml != null) {
                Console.WriteLine("3. Successfully added the custom data field for video: "+name+", entryid: "+id);
                string xmlStr = metadata2.Xml;
                Console.WriteLine("XML used: " + xmlStr);
            } else {
                Console.WriteLine("3. Failed to add the custom data field.");
            }

            // Now lets change the value (update) of the custom field
            // Get the metadata for the video
            KalturaMetadataFilter filter3 = new KalturaMetadataFilter();
            filter3.ObjectIdEqual = entries[0].Id;
            IList<KalturaMetadata> metadataList = client.MetadataService.List(filter3).Objects;
            if (metadataList[0].Xml != null) {
                Console.WriteLine("4. Current metadata for video: " + name + ", entryid: " + id);
                string xmlquoted = metadataList[0].Xml;
                Console.WriteLine("XML: " + xmlquoted);
                string xml = metadataList[0].Xml;
                // Make sure we find the old value in the current metadata
                int pos = xml.IndexOf("<" + metaDataFieldName + ">" + fieldValue + "</" + metaDataFieldName + ">");
                if (pos == -1) {
                    Console.WriteLine("4. Failed to find metadata STRING for video: " + name + ", entryid: " + id);
                } else {
                    System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex ("@<" + metaDataFieldName + ">(.+)</" + metaDataFieldName + ">@");
                    xml = pattern.Replace(xml, "<" + metaDataFieldName + ">Ogg Writ</" + metaDataFieldName + ">");
                    KalturaMetadata rc = client.MetadataService.Update(metadataList[0].Id, xml);
                    Console.WriteLine("5. Updated metadata for video: " + name + ", entryid: " + id);
                    xmlquoted = rc.Xml;
                    Console.WriteLine("XML: " + xmlquoted);
                }
            } else {
                Console.WriteLine("4. Failed to find metadata for video: " + name + ", entryid: " + id);
            }
        }
        static void SampleMetadataOperations()
        {
            // The Schema file for the field
            // Currently, you must build the xsd yourself. There is no utility provided.
            string xsdFile = "MetadataSchema.xsd";
            StreamReader fileStream = File.OpenText(xsdFile);
            string xsd = fileStream.ReadToEnd();

            string fieldValue = "VobSub";
            string xmlData = "<metadata><SubtitleFormat>" + fieldValue + "</SubtitleFormat></metadata>";

            KalturaClient client = new KalturaClient(GetConfig());

            // start new session (client session is enough when we do operations in a users scope)
            client.KS = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID);

            // Setup a pager and search to use
            KalturaMediaEntryFilter mediaEntryFilter = new KalturaMediaEntryFilter();
            mediaEntryFilter.OrderBy = KalturaMediaEntryOrderBy.CREATED_AT_ASC;
            mediaEntryFilter.MediaTypeEqual = KalturaMediaType.VIDEO;

            KalturaFilterPager pager = new KalturaFilterPager();
            pager.PageSize = 1;
            pager.PageIndex = 1;

            KalturaMetadataProfile newMetadataProfile = new KalturaMetadataProfile();
            newMetadataProfile.MetadataObjectType = KalturaMetadataObjectType.ENTRY;
            newMetadataProfile.Name = "Test";

            Console.WriteLine("List videos, get the first one...");
            IList<KalturaMediaEntry> entries = client.MediaService.List(mediaEntryFilter, pager).Objects;
            KalturaMediaEntry entry = entries[0];

            KalturaMetadataProfile metadataProfile = client.MetadataProfileService.Add(newMetadataProfile, xsd);
            Console.WriteLine("1. Successfully created the custom metadata profile " + metadataProfile.Name + ".");

            KalturaMetadata metadata = client.MetadataService.Add(metadataProfile.Id, metadataProfile.MetadataObjectType, entry.Id, xmlData);
            Console.WriteLine("2. Successfully added the custom data field for entryid: " + entry.Id);

            KalturaMetadataFilter metadataFilter = new KalturaMetadataFilter();
            metadataFilter.ObjectIdEqual = entry.Id;
            metadataFilter.MetadataProfileIdEqual = metadataProfile.Id;
            IList<KalturaMetadata> metadataList = client.MetadataService.List(metadataFilter).Objects;
            if (metadataList.Count == 0) {
                throw new Exception("Failed to find metadata for entryid: " + entry.Id);
            }
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        public static void SampleMetadataOperations()
        {
            var client = new Kaltura.KalturaClient(GetConfig());

            // start new session (client session is enough when we do operations in a users scope)
            string ks = client.SessionService.Start(KalturaConfigManager.Kulturaadminsecret, KalturaConfigManager.Kulturauserid, KalturaSessionType.ADMIN, KalturaConfigManager.Kulturapartnerid, 86400, "");

            client.KS = ks;

            // Setup a pager and search to use
            var pager  = new KalturaFilterPager();
            var search = new KalturaMediaEntryFilter
            {
                OrderBy        = KalturaMediaEntryOrderBy.CREATED_AT_ASC,
                MediaTypeEqual = KalturaMediaType.VIDEO
            };

            pager.PageSize  = 10;
            pager.PageIndex = 1;

            Console.WriteLine("List videos, get the first one...");

            // Get 10 video entries, but we'll just use the first one returned
            var entries = client.MediaService.List(search, pager).Objects;
            // Check if there are any custom fields defined in the KMC (Settings -> Custom Data)
            // for the first item returned by the previous listaction
            var filter    = new KalturaMetadataProfileFilter();
            var metadata  = client.MetadataProfileService.List(filter, pager).Objects;
            var profileId = metadata[0].Id;
            var name      = entries[0].Name;
            var id        = entries[0].Id;

            if (metadata[0].Xsd != null)
            {
                Console.WriteLine("1. There are custom fields for video: " + name + ", entryid: " + id);
            }
            else
            {
                Console.WriteLine("1. There are no custom fields for video: " + name + ", entryid: " + id);
            }
            // Add a custom data entry in the KMC  (Settings -> Custom Data)
            var profile = new KalturaMetadataProfile {
                MetadataObjectType = KalturaMetadataObjectType.ENTRY
            };
            const string viewsData = "";

            var fileStream     = File.OpenText(KalturaConfigManager.XsdFile);
            var xsd            = fileStream.ReadToEnd();
            var metadataResult = client.MetadataProfileService.Update(profileId, profile, xsd, viewsData);

            if (metadataResult.Xsd != null)
            {
                Console.WriteLine("2. Successfully created the custom data field " + KalturaConfigManager.MetaDataFieldName + ".");
            }
            else
            {
                Console.WriteLine("2. Failed to create the custom data field.");
            }

            // Add the custom metadata value to the first video
            var filter2 = new KalturaMetadataFilter();

            filter2.ObjectIdEqual = entries[0].Id;
            const string xmlData   = "<metadata><SubtitleFormat>" + KalturaConfigManager.FieldValue + "</SubtitleFormat></metadata>";
            var          metadata2 = client.MetadataService.Add(profileId, profile.MetadataObjectType, entries[0].Id, xmlData);

            if (metadata2.Xml != null)
            {
                Console.WriteLine("3. Successfully added the custom data field for video: " + name + ", entryid: " + id);
                var xmlStr = metadata2.Xml;
                Console.WriteLine("XML used: " + xmlStr);
            }
            else
            {
                Console.WriteLine("3. Failed to add the custom data field.");
            }

            // Now lets change the value (update) of the custom field
            // Get the metadata for the video
            var filter3 = new KalturaMetadataFilter {
                ObjectIdEqual = entries[0].Id
            };
            var metadataList = client.MetadataService.List(filter3).Objects;

            if (metadataList[0].Xml != null)
            {
                Console.WriteLine("4. Current metadata for video: " + name + ", entryid: " + id);
                var xmlquoted = metadataList[0].Xml;
                Console.WriteLine("XML: " + xmlquoted);
                var xml = metadataList[0].Xml;
                // Make sure we find the old value in the current metadata
                var pos = xml.IndexOf("<" + KalturaConfigManager.MetaDataFieldName + ">" + KalturaConfigManager.FieldValue + "</" + KalturaConfigManager.MetaDataFieldName + ">", StringComparison.Ordinal);
                if (pos == -1)
                {
                    Console.WriteLine("4. Failed to find metadata STRING for video: " + name + ", entryid: " + id);
                }
                else
                {
                    var pattern = new System.Text.RegularExpressions.Regex("@<" + KalturaConfigManager.MetaDataFieldName + ">(.+)</" + KalturaConfigManager.MetaDataFieldName + ">@");
                    xml = pattern.Replace(xml, "<" + KalturaConfigManager.MetaDataFieldName + ">Ogg Writ</" + KalturaConfigManager.MetaDataFieldName + ">");
                    var rc = client.MetadataService.Update(metadataList[0].Id, xml);
                    Console.WriteLine("5. Updated metadata for video: " + name + ", entryid: " + id);
                    xmlquoted = rc.Xml;
                    Console.WriteLine("XML: " + xmlquoted);
                }
            }
            else
            {
                Console.WriteLine("4. Failed to find metadata for video: " + name + ", entryid: " + id);
            }
        }
        private static KalturaMetadataProfile createMetadataProfile(KalturaMetadataObjectType objectType, string xsdData)
        {
            KalturaClient client = new KalturaClient(GetConfig());
            client.KS = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");

            KalturaMetadataProfile metadataProfile = new KalturaMetadataProfile();
            metadataProfile.MetadataObjectType = objectType;
            metadataProfile.Name = "test_" + Guid.NewGuid().ToString();

            return client.MetadataProfileService.Add(metadataProfile, xsdData);
        }