Exemplo n.º 1
0
        private static CreativeCompanions LoadCompanions(XElement vastCompanions)
        {
            var companionCreative = new CreativeCompanions();

            companionCreative.Required = (vastCompanions.Attribute("required") != null) ? (CompanionAdsRequired)Enum.Parse(typeof(CompanionAdsRequired), (string)vastCompanions.Attribute("required"), true) : CompanionAdsRequired.None;

            foreach (var vastCompanion in vastCompanions.Elements("Companion"))
            {
                var companion = new Companion();

                foreach (var trackingEvent in GetTrackingEvents(vastCompanion).Where(t => t.Type == TrackingType.CreativeView))
                {
                    companion.ViewTracking.Add(trackingEvent.Value);
                }

                if (vastCompanion.Elements("AdParameters").Any())
                {
                    var xmlEncoded = (bool?)vastCompanion.Element("AdParameters").Attribute("xmlEncoded") ?? false;
                    companion.AdParameters = xmlEncoded ? XmlDecode((string)vastCompanion.Element("AdParameters")) : (string)vastCompanion.Element("AdParameters");
                }
                companion.AdSlotId     = (string)vastCompanion.Attribute("adSlotId") ?? string.Empty;
                companion.AltText      = (string)vastCompanion.Element("AltText") ?? string.Empty;
                companion.ApiFramework = (string)vastCompanion.Attribute("apiFramework") ?? string.Empty;
                companion.AssetHeight  = (int?)vastCompanion.Attribute("assetHeight");
                companion.AssetWidth   = (int?)vastCompanion.Attribute("assetWidth");
                companion.ClickThrough = GetUriValue(vastCompanion.Element("CompanionClickThrough"));
                // TODO: this is not in the schema
                //foreach (var vastTracking in vastCompanion.CompanionClickTracking)
                //{
                //    companion.ClickTracking.Add(new Uri(vastTracking));
                //}
                if (vastCompanion.Elements("CreativeExtensions").Any())
                {
                    foreach (var vastExtension in vastCompanion.Element("CreativeExtensions").Elements("CreativeExtension"))
                    {
                        companion.Extensions.Add(new Extension()); // TODO
                    }
                }
                companion.ExpandedHeight = (int?)vastCompanion.Attribute("expandedHeight");
                companion.ExpandedWidth  = (int?)vastCompanion.Attribute("expandedWidth");
                companion.Height         = (int?)vastCompanion.Attribute("height") ?? 0;
                companion.Width          = (int?)vastCompanion.Attribute("width") ?? 0;
                companion.Item           = GetResources(vastCompanion).FirstOrDefault();
                companionCreative.Companions.Add(companion);
            }
            return(companionCreative);
        }
        private static CreativeCompanions LoadCompanions(XElement vastCompanions)
        {
            var companionCreative = new CreativeCompanions();
            companionCreative.Required = (vastCompanions.Attribute("required") != null) ? (CompanionAdsRequired)Enum.Parse(typeof(CompanionAdsRequired), (string)vastCompanions.Attribute("required"), true) : CompanionAdsRequired.None;

            foreach (var vastCompanion in vastCompanions.Elements("Companion"))
            {
                var companion = new Companion();

                foreach (var trackingEvent in GetTrackingEvents(vastCompanion).Where(t => t.Type == TrackingType.CreativeView))
                    companion.ViewTracking.Add(trackingEvent.Value);

                if (vastCompanion.Elements("AdParameters").Any())
                {
                    var xmlEncoded = (bool?)vastCompanion.Element("AdParameters").Attribute("xmlEncoded") ?? false;
                    companion.AdParameters = xmlEncoded ? XmlDecode((string)vastCompanion.Element("AdParameters")) : (string)vastCompanion.Element("AdParameters");
                }
                companion.AdSlotId = (string)vastCompanion.Attribute("adSlotId") ?? string.Empty;
                companion.AltText = (string)vastCompanion.Element("AltText") ?? string.Empty;
                companion.ApiFramework = (string)vastCompanion.Attribute("apiFramework") ?? string.Empty;
                companion.AssetHeight = (int?)vastCompanion.Attribute("assetHeight");
                companion.AssetWidth = (int?)vastCompanion.Attribute("assetWidth");
                companion.ClickThrough = GetUriValue(vastCompanion.Element("CompanionClickThrough"));
                // TODO: this is not in the schema
                //foreach (var vastTracking in vastCompanion.CompanionClickTracking)
                //{
                //    companion.ClickTracking.Add(new Uri(vastTracking));    
                //}
                if (vastCompanion.Elements("CreativeExtensions").Any())
                {
                    foreach (var vastExtension in vastCompanion.Element("CreativeExtensions").Elements("CreativeExtension"))
                    {
                        companion.Extensions.Add(new Extension()); // TODO
                    }
                }
                companion.ExpandedHeight = (int?)vastCompanion.Attribute("expandedHeight");
                companion.ExpandedWidth = (int?)vastCompanion.Attribute("expandedWidth");
                companion.Height = (int?)vastCompanion.Attribute("height") ?? 0;
                companion.Width = (int?)vastCompanion.Attribute("width") ?? 0;
                companion.Item = GetResources(vastCompanion).FirstOrDefault();
                companionCreative.Companions.Add(companion);
            }
            return companionCreative;
        }
        public static CreativeCompanions CreateCompanionAds(FWAd source, FWAdReference reference)
        {
            var result = new CreativeCompanions();

            foreach (var creative in source.Creatives)
            {
                var companion = new Companion();
                result.Companions.Add(companion);
                companion.AdSlotId = reference.SlotId ?? string.Empty;

                var rendition = creative.CreativeRenditions.OrderByDescending(cr => cr.Preference).FirstOrDefault(c => c.Asset.MimeType.ToLowerInvariant().StartsWith("image/"));
                if (rendition != null)
                {
                    var asset = rendition.Asset;
                    if (asset != null)
                    {
                        companion.Item = new StaticResource()
                        {
                            Value = new Uri(asset.Url),
                            CreativeType = asset.MimeType
                        };
                        companion.Width = rendition.Width;
                        companion.Height = rendition.Height;
                        companion.Id = asset.Id ?? string.Empty;
                    }
                }

                var allCallbacks = reference.EventCallbacks;
                foreach (var url in allCallbacks.Where(ec => ec.Type == FWCallbackType.Click && !ec.ShowBrowser).SelectMany(ec => ec.GetUrls()))
                {
                    companion.ClickTracking.Add(url);
                }

                var clickUrl = allCallbacks.Where(ec => ec.Type == FWCallbackType.Click && ec.ShowBrowser).SelectMany(ec => ec.GetUrls()).FirstOrDefault();
                if (clickUrl != null)
                {
                    companion.ClickThrough = new Uri(clickUrl);
                }
            }
            return result;
        }
        ActiveAdUnit CreateAdUnit(IEnumerable <ICreative> creativeSet, Ad ad, IAdSource adSource)
        {
            CreativeCompanions companions = null;
            var activeAdUnits             = new List <ActiveAdUnit>();

            foreach (var creative in creativeSet)
            {
                if (creative is CreativeLinear)
                {
                    var          linear             = (CreativeLinear)creative;
                    var          eligableMediaFiles = new Queue <MediaFile>(PrioritizeMedia(linear.MediaFiles));
                    ActiveAdUnit chosenAd           = null;
                    while (eligableMediaFiles.Any())
                    {
                        var media          = eligableMediaFiles.Dequeue();
                        var creativeSource = new LinearSource(linear, media);
                        var vpaid          = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                chosenAd = new ActiveAdUnit(creativeSource, vpaid, ad, adSource);
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }

                    if (chosenAd == null)
                    {
                        throw new LoadException(new Exception("Unable to find a player to play the linear ad."));
                    }
                    activeAdUnits.Add(chosenAd);
                }
                else if (creative is CreativeNonLinears)
                {
                    bool found      = false;
                    var  nonLinears = (CreativeNonLinears)creative;
                    foreach (var nonLinear in (nonLinears).NonLinears)
                    {
                        var creativeSource = new NonLinearSource(nonLinear, nonLinears);
                        var vpaid          = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                activeAdUnits.Add(new ActiveAdUnit(creativeSource, vpaid, ad, adSource));
                                found = true;
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }
                    if (!found)
                    {
                        throw new LoadException(new Exception("Unable to find a player to play any nonlinear ads."));
                    }
                }
                else if (creative is CreativeCompanions)
                {
                    companions = (CreativeCompanions)creative;
                }
                else /* not supported, ignore */ } {
        }
        return(activeAdUnits.FirstOrDefault());    // there should only be one linear or nonlinear ad in the sequenced group. Others are ignored.
    }