private static AdDocumentPayload CreateDocument(IClipAdPayload mediaSource)
        {
            var adDocument     = new AdDocumentPayload();
            var adPod          = new AdPod();
            var ad             = new Ad();
            var linearCreative = new CreativeLinear();

            linearCreative.ClickThrough = mediaSource.ClickThrough;
            linearCreative.MediaFiles.Add(new MediaFile()
            {
                Type = mediaSource.MimeType, Value = mediaSource.MediaSource
            });
            ad.Creatives.Add(linearCreative);
            adPod.Ads.Add(ad);
            adDocument.AdPods.Add(adPod);
            return(adDocument);
        }
 private static AdDocumentPayload CreateDocument(IClipAdPayload mediaSource)
 {
     var adDocument = new AdDocumentPayload();
     var adPod = new AdPod();
     var ad = new Ad();
     var linearCreative = new CreativeLinear();
     linearCreative.ClickThrough = mediaSource.ClickThrough;
     linearCreative.MediaFiles.Add(new MediaFile() { Type = mediaSource.MimeType, Value = mediaSource.MediaSource });
     ad.Creatives.Add(linearCreative);
     adPod.Ads.Add(ad);
     adDocument.AdPods.Add(adPod);
     return adDocument;
 }
        internal static async Task<AdDocumentPayload> CreateFromVast(Stream stream, int? maxRedirectDepth, bool allowMultipleAds)
#endif
        {
            XDocument xDoc = XDocument.Load(stream);
            XElement vastRoot = xDoc.Element("VAST");
            if (vastRoot == null)
            {
                vastRoot = xDoc.Element("VideoAdServingTemplate");
                if (vastRoot == null) throw new NotImplementedException();
                return await CreateFromVast1(vastRoot, maxRedirectDepth, allowMultipleAds);
            }
            else
            {
                var result = new AdDocumentPayload();
                result.Version = (string)vastRoot.Attribute("version");
                result.Error = (string)vastRoot.Element("Error");

                var eligableAds = vastRoot.Elements("Ad");
                if (!allowMultipleAds)
                {
                    eligableAds = eligableAds.Where(va => string.IsNullOrEmpty((string)va.Attribute("sequence")));
                }
                foreach (var vastAdPod in eligableAds.GroupBy(va => va.Attribute("sequence") != null ? 1 : int.MaxValue).OrderBy(vap => vap.Key))
                {
                    var adPod = new AdPod();
                    foreach (var vastAd in vastAdPod.OrderBy(va => ToNullableInt((string)va.Attribute("sequence")).GetValueOrDefault(0)))
                    {
                        var ad = new Ad();
                        ad.Id = (string)vastAd.Attribute("id");

                        if (vastAd.Elements("InLine").Any())
                        {
                            var vastAdInline = vastAd.Element("InLine");

                            ad.AdSystem = GetAdSystem(vastAdInline.Element("AdSystem"));

                            ad.Advertiser = (string)vastAdInline.Element("Advertiser");
                            ad.Description = (string)vastAdInline.Element("Description");
                            var error = (string)vastAdInline.Element("Error");
                            if (error != null) ad.Errors.Add(error);
                            ad.Title = (string)vastAdInline.Element("AdTitle");
                            ad.Survey = GetUriValue(vastAdInline.Element("Survey"));

                            ad.Pricing = new Pricing();
                            var pricing = vastAdInline.Element("Pricing");
                            if (pricing != null)
                            {
                                ad.Pricing.Currency = (string)pricing.Attribute("currency");
                                ad.Pricing.Model = (PricingModel)Enum.Parse(typeof(PricingModel), (string)pricing.Attribute("model"), true);
                                ad.Pricing.Value = Convert.ToDouble((string)pricing);
                            }

                            foreach (var vastImpression in vastAdInline.Elements("Impression"))
                            {
                                ad.Impressions.Add((string)vastImpression);
                            }

                            if (vastAdInline.Elements("Extensions").Any())
                            {
                                foreach (var vastExtension in vastAdInline.Element("Extensions").Elements("Extension"))
                                {
                                    ad.Extensions.Add(new Extension()); // TODO
                                }
                            }

                            LoadCreatives(vastAdInline, ad);

                            adPod.Ads.Add(ad);
                        }
                        else if (vastAd.Elements("Wrapper").Any())
                        {
                            Ad wrapper = new Ad();
                            var vastAdWrapper = vastAd.Element("Wrapper");

                            // parse the wrapper itself
                            wrapper.AdSystem = GetAdSystem(vastAdWrapper.Element("AdSystem"));
                            var error = (string)vastAdWrapper.Element("Error");
                            if (error != null) wrapper.Errors.Add(error);

                            foreach (var vastImpression in vastAdWrapper.Elements("Impression"))
                            {
                                wrapper.Impressions.Add((string)vastImpression);
                            }

                            LoadCreatives(vastAdWrapper, wrapper);

                            if (vastAdWrapper.Elements("Extensions").Any())
                            {
                                foreach (var vastExtension in vastAdWrapper.Element("Extensions").Elements("Extension"))
                                {
                                    ad.Extensions.Add(new Extension()); // TODO
                                }
                            }

                            AdDocumentPayload wrappedVastDoc = null;
                            var vastAdUri = GetUriValue(vastAdWrapper.Element("VASTAdTagURI"));
                            if (vastAdUri != null && (!maxRedirectDepth.HasValue || maxRedirectDepth.Value > 0))
                            {
                                try
                                {
                                    // load the stream from the web
                                    using (var s = await Extensions.LoadStreamAsync(vastAdUri))
                                    {
                                        var newAllowMultipleAds = vastAdWrapper.GetBoolAttribute("allowMultipleAds", allowMultipleAds);
                                        var followAdditionalWrappers = vastAdWrapper.GetBoolAttribute("followAdditionalWrappers", true);
                                        int? nextMaxRedirectDepth = followAdditionalWrappers ? (maxRedirectDepth.HasValue ? maxRedirectDepth.Value - 1 : maxRedirectDepth) : 0;
                                        wrappedVastDoc = await CreateFromVast(s, nextMaxRedirectDepth, newAllowMultipleAds);
                                    }
                                }
                                catch { /* swallow */ }
                            }

                            AdPod wrappedAdPod = null;
                            if (wrappedVastDoc != null)
                            {
                                wrappedAdPod = wrappedVastDoc.AdPods.FirstOrDefault();
                            }

                            if (wrappedAdPod == null || !wrappedAdPod.Ads.Any())
                            {
                                // no ads were returned
                                var fallbackOnNoAd = vastAdWrapper.GetBoolAttribute("fallbackOnNoAd", true);
                                if (fallbackOnNoAd)
                                {
                                    wrappedAdPod = FallbackAdPod;
                                }
                            }

                            if (wrappedAdPod != null)
                            {
                                // merge tracking info from this wrapper to every ad in the first adpod in the child
                                foreach (Ad inlineAd in wrappedAdPod.Ads)
                                    MergeWrappedAd(wrapper, inlineAd);

                                // add each ad from the first adpod in the child to the current adpod
                                foreach (Ad inlineAd in wrappedAdPod.Ads)
                                    adPod.Ads.Add(inlineAd);
                            }
                        }
                    }
                    result.AdPods.Add(adPod);
                }
                return result;
            }
        }
        internal static async Task <AdDocumentPayload> CreateFromVast(Stream stream, int?maxRedirectDepth, bool allowMultipleAds)
#endif
        {
            XDocument xDoc     = XDocument.Load(stream);
            XElement  vastRoot = xDoc.Element("VAST");

            if (vastRoot == null)
            {
                vastRoot = xDoc.Element("VideoAdServingTemplate");
                if (vastRoot == null)
                {
                    throw new NotImplementedException();
                }
                return(await CreateFromVast1(vastRoot, maxRedirectDepth, allowMultipleAds));
            }
            else
            {
                var result = new AdDocumentPayload();
                result.Version = (string)vastRoot.Attribute("version");
                result.Error   = (string)vastRoot.Element("Error");

                var eligableAds = vastRoot.Elements("Ad");
                if (!allowMultipleAds)
                {
                    eligableAds = eligableAds.Where(va => string.IsNullOrEmpty((string)va.Attribute("sequence")));
                }
                foreach (var vastAdPod in eligableAds.GroupBy(va => va.Attribute("sequence") != null ? 1 : int.MaxValue).OrderBy(vap => vap.Key))
                {
                    var adPod = new AdPod();
                    foreach (var vastAd in vastAdPod.OrderBy(va => ToNullableInt((string)va.Attribute("sequence")).GetValueOrDefault(0)))
                    {
                        var ad = new Ad();
                        ad.Id = (string)vastAd.Attribute("id");

                        if (vastAd.Elements("InLine").Any())
                        {
                            var vastAdInline = vastAd.Element("InLine");

                            ad.AdSystem = GetAdSystem(vastAdInline.Element("AdSystem"));

                            ad.Advertiser  = (string)vastAdInline.Element("Advertiser");
                            ad.Description = (string)vastAdInline.Element("Description");
                            var error = (string)vastAdInline.Element("Error");
                            if (error != null)
                            {
                                ad.Errors.Add(error);
                            }
                            ad.Title  = (string)vastAdInline.Element("AdTitle");
                            ad.Survey = GetUriValue(vastAdInline.Element("Survey"));

                            ad.Pricing = new Pricing();
                            var pricing = vastAdInline.Element("Pricing");
                            if (pricing != null)
                            {
                                ad.Pricing.Currency = (string)pricing.Attribute("currency");
                                ad.Pricing.Model    = (PricingModel)Enum.Parse(typeof(PricingModel), (string)pricing.Attribute("model"), true);
                                ad.Pricing.Value    = Convert.ToDouble((string)pricing);
                            }

                            foreach (var vastImpression in vastAdInline.Elements("Impression"))
                            {
                                ad.Impressions.Add((string)vastImpression);
                            }

                            if (vastAdInline.Elements("Extensions").Any())
                            {
                                foreach (var vastExtension in vastAdInline.Element("Extensions").Elements("Extension"))
                                {
                                    ad.Extensions.Add(new Extension()); // TODO
                                }
                            }

                            LoadCreatives(vastAdInline, ad);

                            adPod.Ads.Add(ad);
                        }
                        else if (vastAd.Elements("Wrapper").Any())
                        {
                            Ad  wrapper       = new Ad();
                            var vastAdWrapper = vastAd.Element("Wrapper");

                            // parse the wrapper itself
                            wrapper.AdSystem = GetAdSystem(vastAdWrapper.Element("AdSystem"));
                            var error = (string)vastAdWrapper.Element("Error");
                            if (error != null)
                            {
                                wrapper.Errors.Add(error);
                            }

                            foreach (var vastImpression in vastAdWrapper.Elements("Impression"))
                            {
                                wrapper.Impressions.Add((string)vastImpression);
                            }

                            LoadCreatives(vastAdWrapper, wrapper);

                            if (vastAdWrapper.Elements("Extensions").Any())
                            {
                                foreach (var vastExtension in vastAdWrapper.Element("Extensions").Elements("Extension"))
                                {
                                    ad.Extensions.Add(new Extension()); // TODO
                                }
                            }

                            AdDocumentPayload wrappedVastDoc = null;
                            var vastAdUri = GetUriValue(vastAdWrapper.Element("VASTAdTagURI"));
                            if (vastAdUri != null && (!maxRedirectDepth.HasValue || maxRedirectDepth.Value > 0))
                            {
                                try
                                {
                                    // load the stream from the web
                                    using (var s = await Extensions.LoadStreamAsync(vastAdUri))
                                    {
                                        var newAllowMultipleAds      = vastAdWrapper.GetBoolAttribute("allowMultipleAds", allowMultipleAds);
                                        var followAdditionalWrappers = vastAdWrapper.GetBoolAttribute("followAdditionalWrappers", true);
                                        int?nextMaxRedirectDepth     = followAdditionalWrappers ? (maxRedirectDepth.HasValue ? maxRedirectDepth.Value - 1 : maxRedirectDepth) : 0;
                                        wrappedVastDoc = await CreateFromVast(s, nextMaxRedirectDepth, newAllowMultipleAds);
                                    }
                                }
                                catch { /* swallow */ }
                            }

                            AdPod wrappedAdPod = null;
                            if (wrappedVastDoc != null)
                            {
                                wrappedAdPod = wrappedVastDoc.AdPods.FirstOrDefault();
                            }

                            if (wrappedAdPod == null || !wrappedAdPod.Ads.Any())
                            {
                                // no ads were returned
                                var fallbackOnNoAd = vastAdWrapper.GetBoolAttribute("fallbackOnNoAd", true);
                                if (fallbackOnNoAd)
                                {
                                    wrappedAdPod = FallbackAdPod;
                                }
                            }

                            if (wrappedAdPod != null)
                            {
                                // merge tracking info from this wrapper to every ad in the first adpod in the child
                                foreach (Ad inlineAd in wrappedAdPod.Ads)
                                {
                                    MergeWrappedAd(wrapper, inlineAd);
                                }

                                // add each ad from the first adpod in the child to the current adpod
                                foreach (Ad inlineAd in wrappedAdPod.Ads)
                                {
                                    adPod.Ads.Add(inlineAd);
                                }
                            }
                        }
                    }
                    result.AdPods.Add(adPod);
                }
                return(result);
            }
        }
        internal static async Task<AdDocumentPayload> CreateFromVast1(XElement vastRoot, int? maxRedirectDepth, bool allowMultipleAds)
        {
            var result = new AdDocumentPayload();
            result.Version = (string)vastRoot.Attribute("version");

            foreach (var vastAdPod in vastRoot.Elements("Ad").GroupBy(va => va.Attribute("sequence") != null ? 1 : int.MaxValue).OrderBy(vap => vap.Key))
            {
                var adPod = new AdPod();
                foreach (var vastAd in vastAdPod.OrderBy(va => ToNullableInt((string)va.Attribute("sequence")).GetValueOrDefault(0)))
                {
                    var ad = new Ad();
                    ad.Id = (string)vastAd.Attribute("id");

                    if (vastAd.Elements("InLine").Any())
                    {
                        throw new NotImplementedException();
                    }
                    else if (vastAd.Elements("Wrapper").Any())
                    {
                        Ad wrapper = new Ad();
                        var vastAdWrapper = vastAd.Element("Wrapper");

                        // parse the wrapper itself
                        wrapper.AdSystem = GetAdSystem(vastAdWrapper.Element("AdSystem"));
                        var error = (string)vastAdWrapper.Element("Error");
                        if (error == null) wrapper.Errors.Add(error);

                        var linearCreative = new CreativeLinear();

                        foreach (var trackingEvent in GetTrackingEvents(vastAdWrapper))
                            linearCreative.TrackingEvents.Add(trackingEvent);

                        LoadVideoClicks(vastAdWrapper, linearCreative);

                        wrapper.Creatives.Add(linearCreative);

                        var vastAdUri = GetUriValue(vastAdWrapper.Element("VASTAdTagURL"));
                        if (vastAdUri != null)
                        {
                            // load the stream from the web
                            using (var s = await Extensions.LoadStreamAsync(vastAdUri))
                            {
                                int? nextMaxRedirectDepth = maxRedirectDepth.HasValue ? maxRedirectDepth.Value - 1 : maxRedirectDepth;
                                var vastDoc = await CreateFromVast(s, nextMaxRedirectDepth, allowMultipleAds);

                                var firstAdPodInChild = vastDoc.AdPods.FirstOrDefault();

                                if (firstAdPodInChild != null)
                                {
                                    // merge tracking info from this wrapper to every ad in the first adpod in the child
                                    foreach (Ad inlineAd in firstAdPodInChild.Ads)
                                        MergeWrappedAd(wrapper, inlineAd);

                                    // add each ad from the first adpod in the child to the current adpod
                                    foreach (Ad inlineAd in firstAdPodInChild.Ads)
                                        adPod.Ads.Add(inlineAd);
                                }
                            }
                        }
                    }
                }
                result.AdPods.Add(adPod);
            }
            return result;
        }
        public static async Task<AdDocumentPayload> GetAdDocumentPayload(FWTemporalAdSlot adSlot, FWAdResponse adResponse, CancellationToken c)
#endif
        {
            var payload = new AdDocumentPayload();
            var adPod = new AdPod();
            payload.AdPods.Add(adPod);
            foreach (var adReference in adSlot.SelectedAds)
            {
                var ad = await CreateAd(adResponse, adReference);
                adPod.Ads.Add(ad);

                foreach (var fallbackAdReference in adReference.FallbackAds)
                {
                    var fallbackAd = await CreateAd(adResponse, fallbackAdReference);
                    ad.FallbackAds.Add(fallbackAd);
                }
            }
            return payload;
        }
Exemple #7
0
        internal static async Task <AdDocumentPayload> CreateFromVast1(XElement vastRoot, int?maxRedirectDepth, bool allowMultipleAds)
        {
            var result = new AdDocumentPayload();

            result.Version = (string)vastRoot.Attribute("version");

            foreach (var vastAdPod in vastRoot.Elements("Ad").GroupBy(va => va.Attribute("sequence") != null ? 1 : int.MaxValue).OrderBy(vap => vap.Key))
            {
                var adPod = new AdPod();
                foreach (var vastAd in vastAdPod.OrderBy(va => ToNullableInt((string)va.Attribute("sequence")).GetValueOrDefault(0)))
                {
                    var ad = new Ad();
                    ad.Id = (string)vastAd.Attribute("id");

                    if (vastAd.Elements("InLine").Any())
                    {
                        throw new NotImplementedException();
                    }
                    else if (vastAd.Elements("Wrapper").Any())
                    {
                        Ad  wrapper       = new Ad();
                        var vastAdWrapper = vastAd.Element("Wrapper");

                        // parse the wrapper itself
                        wrapper.AdSystem = GetAdSystem(vastAdWrapper.Element("AdSystem"));
                        var error = (string)vastAdWrapper.Element("Error");
                        if (error == null)
                        {
                            wrapper.Errors.Add(error);
                        }

                        var linearCreative = new CreativeLinear();

                        foreach (var trackingEvent in GetTrackingEvents(vastAdWrapper))
                        {
                            linearCreative.TrackingEvents.Add(trackingEvent);
                        }

                        LoadVideoClicks(vastAdWrapper, linearCreative);

                        wrapper.Creatives.Add(linearCreative);

                        var vastAdUri = GetUriValue(vastAdWrapper.Element("VASTAdTagURL"));
                        if (vastAdUri != null)
                        {
                            // load the stream from the web
                            using (var s = await Extensions.LoadStreamAsync(vastAdUri))
                            {
                                int?nextMaxRedirectDepth = maxRedirectDepth.HasValue ? maxRedirectDepth.Value - 1 : maxRedirectDepth;
                                var vastDoc = await CreateFromVast(s, nextMaxRedirectDepth, allowMultipleAds);

                                var firstAdPodInChild = vastDoc.AdPods.FirstOrDefault();

                                if (firstAdPodInChild != null)
                                {
                                    // merge tracking info from this wrapper to every ad in the first adpod in the child
                                    foreach (Ad inlineAd in firstAdPodInChild.Ads)
                                    {
                                        MergeWrappedAd(wrapper, inlineAd);
                                    }

                                    // add each ad from the first adpod in the child to the current adpod
                                    foreach (Ad inlineAd in firstAdPodInChild.Ads)
                                    {
                                        adPod.Ads.Add(inlineAd);
                                    }
                                }
                            }
                        }
                    }
                }
                result.AdPods.Add(adPod);
            }
            return(result);
        }