Пример #1
0
        private void LoadCreatives(DeliveryFile file)
        {
            using (var reader = new JsonDynamicReader(file.OpenContents(), "$.data[*].*"))
            {
                Mappings.OnFieldRequired = fieldName =>
                {
                    try
                    {
                        return(reader.Current[fieldName]);
                    }
                    catch (Exception ex)
                    {
                        Log(String.Format("Failed to read field '{0}' for creative id = {1} in file {2}, ex: {3}", fieldName, reader.Current.creative_id, file.Name, ex.Message), LogMessageType.Error);
                        return(null);
                    }
                };
                while (reader.Read())
                {
                    var creative = new CompositeCreative();
                    CreativeMappings.Apply(creative);

                    _creativeCache.Add(reader.Current.creative_id, creative);
                }
            }
        }
		private static void SetCompositeCreativeTk(CompositeCreative compCreative)
		{
			foreach (var part in compCreative.Parts)
			{
				compCreative.TK = String.Format("{0}_{1}", compCreative.TK, part.Value.TK);
			}
			if (compCreative.TK.Length > 1) compCreative.TK = compCreative.TK.Remove(0, 1);
		} 
Пример #3
0
        private Ad CreateAd(CsvDynamicReader adsReader)
        {
            //--------------
            // Ad
            //--------------
            var ad = new Ad
            {
                OriginalID = adsReader.Current[AdWordsConst.AdIDFieldName],
                Channel    = GetChannel("Google"),
                Account    = GetAccount("Bbinary"),
                Fields     = new Dictionary <EdgeField, object>(),
                TK         = adsReader.Current[AdWordsConst.AdIDFieldName],
                EdgeType   = GetEdgeType("Ad")
            };

            //--------------
            // Ad Type
            //--------------
            var adTypeValue = adsReader.Current[AdWordsConst.AdTypeFieldName].ToString();
            var devicePreferenceColumnValue = adsReader.Current[AdWordsConst.AdDevicePreferenceFieldName].ToString();

            //is mobile ad ?
            if (devicePreferenceColumnValue.Equals(AdWordsConst.AdDevicePreferenceMobileFieldValue))
            {
                adTypeValue = string.Format("Mobile {0}", adTypeValue);
            }

            ad.Fields.Add(GetExtraField("AdType"), new StringValue
            {
                Value    = adTypeValue,
                TK       = adTypeValue,
                EdgeType = GetEdgeType("AdType")
            });

            //------------------
            // Destination Url
            //------------------
            if (!String.IsNullOrWhiteSpace(adsReader.Current[AdWordsConst.DestUrlFieldName]))
            {
                ad.MatchDestination = new Destination
                {
                    Value    = adsReader.Current[AdWordsConst.DestUrlFieldName],
                    TK       = adsReader.Current[AdWordsConst.DestUrlFieldName],
                    EdgeType = GetEdgeType("Destination")
                }
            }
            ;

            //--------------
            // Campaign
            //--------------
            var campaign = new Campaign
            {
                Name       = adsReader.Current[AdWordsConst.CampaignFieldName],
                OriginalID = adsReader.Current[AdWordsConst.CampaignIdFieldName],
                TK         = adsReader.Current[AdWordsConst.CampaignIdFieldName],
                EdgeType   = GetEdgeType("Campaign")
            };

            ad.Fields.Add(GetExtraField("Campaign"), campaign);

            //--------------
            // Ad group
            //--------------
            var adGroup = new StringValue
            {
                Value      = adsReader.Current[AdWordsConst.AdGroupFieldName],
                OriginalID = adsReader.Current[AdWordsConst.AdGroupIdFieldName],
                Fields     = new Dictionary <EdgeField, object>(),
                TK         = adsReader.Current[AdWordsConst.AdGroupIdFieldName],
                EdgeType   = GetEdgeType("AdGroup")
            };

            adGroup.Fields.Add(GetExtraField("Campaign"), campaign);
            ad.Fields.Add(GetExtraField("AdGroup"), adGroup);

            //---------------------
            // Composite Creatives
            //---------------------
            // composite creative and composite creative definition
            var compCreative = new CompositeCreative
            {
                Parts    = new Dictionary <CompositePartField, SingleCreative>(),
                EdgeType = GetEdgeType("CompositeCreative")
            };
            var compCreativeMatch = new CompositeCreativeMatch
            {
                CreativesMatches = new Dictionary <CompositePartField, SingleCreativeMatch>(),
                Creative         = compCreative,
                EdgeType         = GetEdgeType("CompositeCreativeMatch")
            };

            //----------------------------------
            // Display Url as text creative
            //----------------------------------
            SingleCreative creative = new TextCreative
            {
                Text = adsReader.Current[AdWordsConst.DisplayURLFieldName],
                //TextCreativeType = new TextCreativeType
                //{
                //	Value = "Url",
                //	TK =  "Url",
                //	EdgeType = GetEdgeType("TextCreativeType")
                //},
                EdgeType = GetEdgeType("TextCreative"),
                TK       = String.Format("{0}_{1}", "Url", adsReader.Current[AdWordsConst.DisplayURLFieldName])
            };

            compCreative.Parts.Add(GetCompositePartField("DisplayUrlCreative"), creative);

            SingleCreativeMatch match = new TextCreativeMatch
            {
                Creative = creative,
                EdgeType = GetEdgeType("TextCreativeMatch"),
                TK       = adsReader.Current[AdWordsConst.DisplayURLFieldName]
            };

            compCreativeMatch.CreativesMatches.Add(GetCompositePartField("DisplayUrlMatch"), match);

            if (String.Equals(Convert.ToString(adsReader.Current[AdWordsConst.AdTypeFieldName]), "Image ad"))
            {
                //----------------------------------
                // Image as Image creative
                //----------------------------------
                // format for example: Ad name: 468_60_Test7options_Romanian.swf; 468 x 60
                var imageParams = adsReader.Current[AdWordsConst.AdFieldName].Trim().Split(new[] { ':', ';' });

                creative = new ImageCreative
                {
                    Image    = imageParams[1].Trim(),
                    EdgeType = GetEdgeType("ImageCreative"),
                    TK       = imageParams[1].Trim()
                };
                compCreative.Parts.Add(GetCompositePartField("SingleCreative"), creative);

                match = new ImageCreativeMatch
                {
                    Creative  = creative,
                    ImageSize = imageParams[2].Trim(),
                    EdgeType  = GetEdgeType("ImageCreativeDefinition"),
                    TK        = String.Format("{0}_{1}", imageParams[1].Trim(), imageParams[2].Trim())
                };
                compCreativeMatch.CreativesMatches.Add(GetCompositePartField("SingleCreativeMatch"), match);
            }
            else
            {
                //----------------------------------
                // Title as Text creative
                //----------------------------------
                creative = new TextCreative
                {
                    Text = adsReader.Current.Ad,
                    //TextCreativeType = new TextCreativeType
                    //{
                    //	Value = "Text",
                    //	TK = "Text",
                    //	EdgeType = GetEdgeType("TextCreativeType")
                    //},
                    EdgeType = GetEdgeType("TextCreative"),
                    TK       = String.Format("{0}_{1}", "Text", adsReader.Current.Ad)
                };
                compCreative.Parts.Add(GetCompositePartField("SingleCreative"), creative);

                match = new TextCreativeMatch
                {
                    Creative = creative,
                    EdgeType = GetEdgeType("TextCreativeMatch"),
                    TK       = adsReader.Current.Ad
                };
                compCreativeMatch.CreativesMatches.Add(GetCompositePartField("SingleCreativeMatch"), match);

                //----------------------------------
                // Description 1 as Text creative
                //----------------------------------
                creative = new TextCreative
                {
                    Text = adsReader.Current["Description line 1"],
                    //TextCreativeType = new TextCreativeType
                    //{
                    //	Value = "Text",
                    //	TK = "Text",
                    //	EdgeType = GetEdgeType("TextCreativeType")
                    //},
                    EdgeType = GetEdgeType("TextCreative"),
                    TK       = String.Format("{0}_{1}", "Text", adsReader.Current["Description line 1"])
                };
                compCreative.Parts.Add(GetCompositePartField("Desc1Creative"), creative);

                match = new TextCreativeMatch
                {
                    Creative = creative,
                    EdgeType = GetEdgeType("TextCreativeMatch"),
                    TK       = adsReader.Current["Description line 1"]
                };
                compCreativeMatch.CreativesMatches.Add(GetCompositePartField("Desc1Definition"), match);

                //----------------------------------
                // Description 2 as Text creative
                //----------------------------------
                creative = new TextCreative
                {
                    Text = adsReader.Current["Description line 2"],
                    //TextCreativeType = new TextCreativeType
                    //{
                    //	Value = "Text",
                    //	TK = "Text",
                    //	EdgeType = GetEdgeType("TextCreativeType")
                    //},
                    EdgeType = GetEdgeType("TextCreative"),
                    TK       = String.Format("{0}_{1}", "Text", adsReader.Current["Description line 2"])
                };
                compCreative.Parts.Add(GetCompositePartField("Desc2Creative"), creative);

                match = new TextCreativeMatch
                {
                    Creative = creative,
                    EdgeType = GetEdgeType("TextCreativeMatch"),
                    TK       = adsReader.Current["Description line 2"]
                };
                compCreativeMatch.CreativesMatches.Add(GetCompositePartField("Desc2Definition"), match);
            }

            SetCompositeCreativeTk(compCreative);
            SetCompositeCreativeDefinitionTk(compCreativeMatch);
            ad.CreativeMatch          = compCreativeMatch;
            ad.CreativeMatch.Creative = compCreative;

            // add Ad to cache of Ads
            _importedAds.Add(ad.OriginalID, ad);
            return(ad);
        }