def TempAnalyzePlacement(placement): isLMA = PlacementUtils.checkLMA(Api, placement) if isLMA: placement = PlacementUtils.getPlacement(Api, placement["id"]) date = datetime.datetime(2019, 2, 1) placementStartDate = UtilUtils.placementDateToDatetime( placement["pricingSchedule"]["startDate"]) if placementStartDate > date: try: if "enabledVideoFormats" not in placement["videoSettings"][ "transcodeSettings"]: return True placement["videoSettings"]["transcodeSettings"][ "enabledVideoFormats"] = { 'kind': 'dfareporting#transcodeSetting', 'enabledVideoFormats': [] } except: try: placement["videoSettings"]["transcodeSettings"] = { 'kind': 'dfareporting#transcodeSetting', 'enabledVideoFormats': [] } except: placement["videoSettings"] = {} PlacementUtils.updatePlacement( Api, {"videoSettings": placement["videoSettings"]}, placement["id"]) return True else: return True return False
def getPlacement(placement_id): global currentCSDList placement = PlacementUtils.getPlacement(Api,placement_id) campaignName = CampaignUtils.getCampaign(Api, placement["campaignId"])["name"].strip() if "CANCELLED" in placement["name"]: return print(campaignName) if campaignName not in currentCSDList: rowID = csdDict[campaignName][1] sheetID = csdDict[campaignName][0] currentCSDList[campaignName] = Smartsheets.getCSD(sheetID,rowID) isTrafficked = PlacementUtils.checkIfTrafficked(Api, placement,currentCSDList[campaignName]) for key in isTrafficked: series.loc[series.placement_id==int(placement_id), key] = isTrafficked[key] series.loc[series.placement_id==int(placement_id), "start_date"] = placement["pricingSchedule"]["startDate"] series.loc[series.placement_id==int(placement_id), "end_date"] = placement["pricingSchedule"]["endDate"]
def checkCampaign(x): placement = PlacementUtils.getPlacement(Api, x["objectId"]) print("checking " + placement["name"]) campaign = CampaignUtils.getCampaign(Api, placement["campaignId"]) if regex.search(campaign["name"]): return placement else: return None
def determineDimensions(placement): if placement["compatibility"] == 'IN_STREAM_VIDEO' or "_SS_" in placement["name"] or "»SS»" in placement["name"]: try: dimensions = str(placement["name"]).split("»")[5] dimensions = re.sub("\(NA\)\+|\(NA\+\)|NA\+","",dimensions) return dimensions except: return str(placement["name"]).split("_")[3] else: return PlacementUtils.sizeToDimension(placement["size"])
def analyzePlacements(placementId): placement = PlacementUtils.getPlacement(Api, placementId) if "»SS»" not in placement["name"]: return None campaignId = placement["campaignId"] campaign = CampaignUtils.getCampaign(Api, campaignId) regex = re.compile("[1-5]L(M|G)") if regex.search(campaign["name"]): return {"Campaign Name":campaign["name"], "Placement Id": placement["id"], "Placement Name":placement["name"]} else: return None
def fitPlacement(changeLog, action): finalObject = {} placement = PlacementUtils.getPlacement(Api, changeLog["objectId"]) def getCampaignName(campaignID): global CampaignObject try: return CampaignObject[campaignID] except: campaign = CampaignUtils.getCampaign(Api, campaignID) CampaignObject[campaignID] = campaign["name"] print("adding {campaignName}".format( campaignName=campaign["name"])) return campaign["name"] def getCreationDate(creationDate): return datetime.datetime.fromtimestamp(creationDate // 1000).strftime("%Y-%m-%d") finalObject["Campaign_id"] = placement['campaignId'] finalObject["Campaign_name"] = getCampaignName(placement['campaignId']) finalObject["Placement_id"] = placement["id"] finalObject["Placement_Name"] = placement["name"] finalObject["Planned_Cost"] = int( placement['pricingSchedule']["pricingPeriods"][0] ["rateOrCostNanos"]) / 1000000000 finalObject["Planned_Impressions"] = placement['pricingSchedule'][ "pricingPeriods"][0]["units"] finalObject["Start_date"] = placement['pricingSchedule']['startDate'] finalObject["End_date"] = placement['pricingSchedule']['endDate'] finalObject["Date of Creation"] = getCreationDate( int(placement["createInfo"]["time"])) finalObject["Action"] = action try: finalObject["Placement_Group_Id"] = placement["placementGroupId"] if placement["placementGroupId"] in PlacementgroupObject: finalObject["Placement_Group_Name"] = PlacementgroupObject[ placement["placementGroupId"]] else: finalObject["Placement_Group_Name"] = Api.generateRequestUrl( "placementGroups", objectId=placement["placementGroupId"] ).get().response["name"] PlacementgroupObject[placement[ "placementGroupId"]] = finalObject["Placement_Group_Name"] except: finalObject["Placement_Group_Id"] = "N/A" finalObject["Placement_Group_Name"] = "N/A" FinalPlacementArray.append(finalObject) return None
def updateSheet(sheet, placementList): global RemovedPlacements try: sheet['Creative\n Rotation'] = sheet['Creative\n Rotation'].apply( floatToPercent) except: sheet['Creative Rotation'] = sheet['Creative Rotation'].apply( floatToPercent) sheet["Start Date"] = sheet["Start Date"].apply(datetimeToDate) sheet["End Date"] = sheet["End Date"].apply(datetimeToDate) DCMidList = set([int(x["id"]) for x in placementList]) CSDidList = set(sheet["Id"].tolist()) campaignName = sheet.iloc[0]["Campaign"] placementsToAdd = DCMidList.difference(CSDidList) # placementsToRemove = CSDidList.difference(DCMidList) for placement in CSDidList.copy(): currentPlacement = PlacementUtils.getPlacement(Api, placement) print(currentPlacement["name"]) CSDPlacement = sheet[sheet.Id == placement] if checkPlacementEndDate( currentPlacement["pricingSchedule"]["endDate"]): CSDPlacement["End Date"] = currentPlacement["pricingSchedule"][ "endDate"] RemovedPlacements = RemovedPlacements.append(CSDPlacement) sheet = sheet[sheet.Id != placement] CSDidList = set(sheet["Id"].tolist()) sheet = sheet.reset_index(drop=True) for placement in CSDidList.copy(): CSDPlacement = sheet[sheet.Id == placement] currentPlacement = PlacementUtils.getPlacement(Api, placement) print(currentPlacement["name"]) indexOfPlacement = sheet[sheet.Id == placement].index[0] + 2 if placement in DCMidList: if checkPlacementEndDate( currentPlacement["pricingSchedule"]["endDate"]): CSDPlacement["End Date"] = currentPlacement["pricingSchedule"][ "endDate"] RemovedPlacements = RemovedPlacements.append(CSDPlacement) sheet = sheet[sheet.Id != placement] sheet = sheet.reset_index(drop=True) continue DCMidList.remove(placement) CSDidList.remove(placement) CSDPlacementInfo = { "D": CSDPlacement["Name"].values[0], "E": CSDPlacement["Start Date"].values[0], "F": CSDPlacement["End Date"].values[0], "H": CSDPlacement["Dimensions"].values[0] } DCMPlacementInfo = { "D": currentPlacement["name"], "E": UtilUtils.formatPlacementDate( currentPlacement["pricingSchedule"]["startDate"]), "F": UtilUtils.formatPlacementDate( currentPlacement["pricingSchedule"]["endDate"]), "H": determineDimensions(currentPlacement) } for column in CSDPlacementInfo: #Sometimes Placement dates can come in as strings, or Datetimes, so we have to convert them just in case. if type(CSDPlacementInfo[column]) == datetime.datetime: CSDPlacementInfo[column] = datetime.datetime.strftime( CSDPlacementInfo[column], "%m/%d/%Y") if type(CSDPlacementInfo[column]) == np.datetime64 or ( type(CSDPlacementInfo[column]) == str and (column == "E" or column == "F")): CSDPlacementInfo[column] = pd.to_datetime( str(CSDPlacementInfo[column])).strftime("%m/%d/%Y") if CSDPlacementInfo[column] != DCMPlacementInfo[column]: if column == "D" and "Carat" in DCMPlacementInfo[column]: continue rowColumn = "{column}{indexOfPlacement}".format( column=column, indexOfPlacement=indexOfPlacement) ChangesArray.append({ "column": rowColumn, "data": DCMPlacementInfo[column], "format": yellow }) print(column, CSDPlacementInfo[column]) print(column, DCMPlacementInfo[column]) if placementsToAdd: #RowNum accounts for the fact that the Dataframe is 0-indexed, and the Headers are removed. rowNum = len(sheet) + 2 for placement in placementsToAdd: currentPlacement = PlacementUtils.getPlacement(Api, placement) if checkPlacementEndDate( currentPlacement["pricingSchedule"]["endDate"]): continue DCMPlacementInfo = { "A": campaignName, "B": PlacementUtils.getAndFormatSite(Api, currentPlacement), "C": placement, "D": currentPlacement["name"], "E": UtilUtils.formatPlacementDate( currentPlacement["pricingSchedule"]["startDate"]), "F": UtilUtils.formatPlacementDate( currentPlacement["pricingSchedule"]["endDate"]), "G": currentPlacement["compatibility"].capitalize(), "H": determineDimensions(currentPlacement) } for column in DCMPlacementInfo: rowColumn = "{column}{indexOfPlacement}".format( column=column, indexOfPlacement=rowNum) ChangesArray.append({ "column": rowColumn, "data": DCMPlacementInfo[column], "format": yellow }) rowNum += 1 # if placementsToRemove: # for placement in placementsToRemove: # #the two functions in the same way as the RowNum in the previous "if" statement # indexOfPlacement = sheet[sheet.Id == placement].index[0] + 2 # CSDPlacement = sheet[sheet.Id == placement] # CSDPlacementInfo = {"A":CSDPlacement["Campaign"].values[0],"B":CSDPlacement["Site"].values[0],"C":CSDPlacement["Id"].values[0],"D":CSDPlacement["Name"].values[0], "E":CSDPlacement["Start Date"].values[0], "F":CSDPlacement["End Date"].values[0],"G":CSDPlacement["Compatibility"].values[0], "H":CSDPlacement["Dimensions"].values[0]} # for column in CSDPlacementInfo: # rowColumn = "{column}{indexOfPlacement}".format(column=column,indexOfPlacement=indexOfPlacement) # ChangesArray.append({"column":rowColumn,"data":CSDPlacementInfo[column],"format":red}) return sheet
def determineDimensions(placement): if placement["compatibility"] == 'IN_STREAM_VIDEO' or "_SS_" in placement[ "name"]: return str(placement["name"]).split("_")[3] else: return PlacementUtils.sizeToDimension(placement["size"])
"L1": "Creative File 3", "M1": "Creative File 4", "N1": "Creative File 5" } urlObject = {'A1': "Creative File", "B1": "Creative URL"} try: campaignName = excel["TPS Placements"].iloc[0]["Campaign"] except: campaignName = excel["SS Placements"].iloc[0]["Campaign"] listValues = {"searchString": campaignName} campaign = CampaignUtils.getCampaignByName(Api, listValues) listValules = {"campaignIds": campaign["id"]} placementList = [ x for x in PlacementUtils.listPlacement(Api, listValules) if x["archived"] == False ] TPSPlacements = [x for x in placementList if "_TPS_" in x["name"]] SSPlacements = [x for x in placementList if "_SS_" in x["name"]] if len(TPSPlacements) > 0: TPSsheet = updateSheet(excel["TPS Placements"], TPSPlacements) excel["TPS Placements"] = TPSsheet TPSsheet.to_excel(writer, sheet_name="TPS Placements", index=False) TPSworksheet = writer.sheets['TPS Placements'] for obj in headerObject: TPSworksheet.write(obj, headerObject[obj], headerBlue) for obj in ChangesArray: TPSworksheet.write(obj["column"], obj["data"], obj["format"]) ChangesArray = []
def analyzeFile(file_name): import pandas as pd from v3modules import CampaignUtils, PlacementUtils import numpy as np import openpyxl import re LMAOverride = False if ".csv" in file_name: csv = pd.read_csv(file_name) file_name = re.sub('.csv', '.xlsx', file_name) else: csv = pd.read_excel(file_name, sheetname=0) #for item in file_name csv.fillna('N/A', inplace=True) def isEvidon(placementName): nonlocal LMAOverride if LMAOverride == True: return "Y" for string in Evidon: if string in placementName: return "Y" return "N" def nameChange(name, site): global counters import re site = site.loc[counters] if "Carat_" in name: site = re.sub(r'\(\d+\)', "", site).strip() + "_" name = re.sub("Carat_", site, name) name = re.sub("Â", "", name) else: name = re.sub("Â", "", name) counters += 1 return name def dateTimetoDate(value): import datetime import re if isinstance(value, datetime.date): value = value.strftime("%m" + "/" + "%d" + "/" + "%Y") month = value.split('/')[0] if month.startswith('0'): month = re.sub('0', "", month) day = value.split('/')[1] if day.startswith('0'): day = re.sub('0', "", day) year = value.split('/')[2] date = month + "/" + day + "/" + year value = date return value csv["Start date"] = csv["Start date"].apply(dateTimetoDate) csv["End date"] = csv["End date"].apply(dateTimetoDate) csv["Name"] = csv["Name"].apply(nameChange, args=(csv["Site"], )) csv["Evidon"] = csv["Name"].apply(isEvidon) csv = csv[csv['Compatibility'] != 'N/A'] placementID = csv["Id"].iloc[0] placement = PlacementUtils.getPlacement(Api, placementID) campaignID = placement['campaignId'] campaign = CampaignUtils.getCampaign(Api, campaignID)["name"] temp = file_name.split("_") temp.insert(1, re.sub("/", "", campaign.split("_")[0])) file_name = "_".join(temp) for code in LMA: if code in campaign: LMAOverride = True #campaign = campaignName csv['Campaign'] = campaign csv['Creative Rotation'] = np.nan csv['Creative File 1'] = np.nan csv['Creative File 2'] = np.nan csv['Creative File 3'] = np.nan csv['Creative File 4'] = np.nan csv['Creative File 5'] = np.nan del csv['Object type'] del csv['Status'] # REORDER COLUMNS csv = csv[[ 'Campaign', 'Site', 'Id', 'Name', 'Start date', 'End date', 'Compatibility', 'Dimensions', 'Creative Rotation', 'Creative File 1', 'Creative File 2', 'Creative File 3', 'Creative File 4', 'Creative File 5' ]] csv_ss_placement = csv[csv['Dimensions'] == '1x1'] csv = csv[csv['Dimensions'] != '1x1'] site_list = csv_ss_placement['Site'].unique().tolist() def getDimensions(placement): if type(placement) is str: try: return str(placement).split("_")[3] except: dimensions = str(placement).split("»")[5] if "T1" in dimensions: dimensions = "120+N/A" else: dimensions = re.sub("NA\+", "", dimensions) return dimensions csv_ss_placement["Dimensions"] = csv_ss_placement["Name"].apply( getDimensions) csv["Dimensions"] = csv["Name"].apply(getDimensions) for i in range(3): csv_ss_placement = csv_ss_placement.append(pd.Series( [ np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, ], index=csv_ss_placement.columns.tolist()), ignore_index=True) csv_ss_placement = csv_ss_placement.append(pd.Series( [ 'Site', 'Contact', np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, ], index=csv_ss_placement.columns.tolist()), ignore_index=True) siteRow = len(csv_ss_placement) + 1 for site in site_list: csv_ss_placement = csv_ss_placement.append(pd.Series( [ site, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, ], index=csv_ss_placement.columns.tolist()), ignore_index=True) urls = pd.DataFrame(data={'Creative File': [], 'Creative URL': []}) writer = pd.ExcelWriter('Output/%s' % (file_name), engine='xlsxwriter') workbook = writer.book headerObject = { "A1": "Campaign", "B1": "Site", "C1": "Id", "D1": "Name", "E1": "Start Date", "F1": "End Date", "G1": "Compatibility", "H1": "Dimensions", "I1": "Evidon", "J1": "Creative Rotation", "K1": "Creative File 1", "L1": "Creative File 2", "M1": "Creative File 3", "N1": "Creative File 4", "O1": "Creative File 5" } urlObject = {'A1': "Creative File", "B1": "Creative URL"} siteObject = {'A' + str(siteRow): "Site", 'B' + str(siteRow): "Contact"} format1 = workbook.add_format({'bg_color': '#0AADE9'}) if len(csv) != 0: csv.to_excel(writer, sheet_name='TPS Placements', index=False) worksheet_csv = writer.sheets['TPS Placements'] for obj in headerObject: worksheet_csv.write(obj, headerObject[obj], format1) if len(csv_ss_placement) > 4: print(len(csv_ss_placement)) csv_ss_placement.to_excel(writer, sheet_name='SS Placements', index=False) worksheet_csv_ss_placements = writer.sheets['SS Placements'] for obj in headerObject: worksheet_csv_ss_placements.write(obj, headerObject[obj], format1) for obj in siteObject: worksheet_csv_ss_placements.write(obj, siteObject[obj], format1) urls.to_excel(writer, sheet_name='URLs', index=False) worksheet_URLs = writer.sheets['URLs'] for obj in urlObject: worksheet_URLs.write(obj, urlObject[obj], format1) writer.save()
campaignId = placement["campaignId"] if campaignId in LMACampaignObj: return True if campaignId in NotLMAObj: return False print("checking " + str(campaignId)) campaign = CampaignUtils.getCampaign(Api, campaignId) if "LMA" in campaign["name"]: LMACampaignObj[campaign["id"]] = campaign["name"] return True else: NotLMAObj[campaign["id"]] = campaign["name"] return False placementList = PlacementUtils.listPlacement(Api, listValues) placementList = [ { "Placement Name": x["name"], "Placement Id": x["id"], "Placement Start Date": UtilUtils.formatPlacementDate(x["pricingSchedule"]["startDate"]), "Placement End Date": UtilUtils.formatPlacementDate(x["pricingSchedule"]["endDate"]), "Campaign ID": x["campaignId"], "Campaign Name": LMACampaignObj[x["campaignId"]] } for x in placementList
finalLogs = Createlogs + UpdateLogs length = str(len(finalLogs)) print(length + " checking each placement Now") finalLogs = [checkCampaign(x) for x in finalLogs if checkSubAccount(x)] finalLogs = [x for x in finalLogs if x != None] length = str(len(finalLogs)) print(length) finalPlacementArray = [] for placement in finalLogs: placementAdList = AdUtils.listAd(Api, {"placementIds":placement["id"]}) campaignAdList = AdUtils.listAd(Api, {"campaignIds":placement["campaignId"]}) campaign = CampaignUtils.getCampaign(Api, placement["campaignId"]) placementAdList = [x for x in placementAdList if "TRACKING" in x["name"] and x["archived"] == False] campaignAdList = [x for x in campaignAdList if "TRACKING" in x["name"] and x["archived"] == False] PlacementUtils.pushStaticClickTracking(Api, placement["id"]) if len(campaignAdList) < 4: pullAdsIntoCampaign(placement["campaignId"], placement, campaignAdList) campaignAdList = AdUtils.listAd(Api, {"campaignIds":placement["campaignId"]}) campaignAdList = [x for x in campaignAdList if "TRACKING" in x["name"] and x["archived"] == False] if len(placementAdList) < 4: for ad in campaignAdList: adObject = AdUtils.getAd(Api, ad["id"]) AdUtils.associatePlacement(adObject,placement,Api) finalPlacementArray.append({"Placement Name": placement["name"],"Campaign Name": campaign["name"], "Placement ID":placement["id"]}) if len(finalPlacementArray) > 0: df = pandas.DataFrame(data=finalPlacementArray) df = df[["Campaign Name", "Placement ID","Placement Name"]] writer = pandas.ExcelWriter('Updated SS Placements.xlsx',engine='xlsxwriter') workbook = writer.book
try: CampaignName = excel["TPS Placements"].iloc[0]["Campaign"] except: CampaignName = excel["SS Placements"].iloc[0]["Campaign"] for code in LMA: if code in CampaignName: LMAOverride = True try: RemovedPlacements = excel["Ended Placements"] except: pass listValues = {"searchString":CampaignName, "archived":False} campaign = CampaignUtils.getCampaignByName(Api, listValues) listValules = {"campaignIds":campaign["id"]} placementList = [x for x in PlacementUtils.listPlacement(Api, listValules) if x["archived"] == False ] TPSPlacements = [x for x in placementList if "_TPS_" in x["name"] or "»TP»" in x["name"]] SSPlacements = [x for x in placementList if "_SS_" in x["name"] or "»SS»" in x["name"]] if len(TPSPlacements) > 0: try: TPSsheet = updateSheet(excel["TPS Placements"], TPSPlacements) except: excel["TPS Placements"] = pd.DataFrame(columns=["Campaign", "Site", "Id", "Name", "Start Date", "End Date", "Compatibility", "Dimensions", "Evidon", "Creative Rotation", "Creative File 1", "Creative File 2", "Creative File 3", "Creative File 4", "Creative File 5"]) TPSsheet = updateSheet(excel["TPS Placements"], TPSPlacements) excel["TPS Placements"] = TPSsheet TPSsheet.to_excel(writer, sheet_name="TPS Placements",index=False) TPSworksheet = writer.sheets['TPS Placements'] for obj in headerObject: TPSworksheet.write(obj, headerObject[obj], headerBlue) for obj in ChangesArray: TPSworksheet.write(obj["column"],obj["data"],obj["format"])
from v3modules import DCMAPI, PlacementUtils, SiteUtils, ChangeLogUtils, UtilUtils, CampaignUtils import datetime Api = DCMAPI.DCMAPI() finalPlacementList = [] noTranscodes = set() transcodes = { "Samsung AdHub": (PlacementUtils.getPlacement(Api, 237980865), '1552768'), "National Cable Communications, LLC (NCC Media)": (PlacementUtils.getPlacement(Api, 227647455), '1483997'), "Gamut Media": (PlacementUtils.getPlacement(Api, 236055453), '3135124'), "Pulpo Media": (PlacementUtils.getPlacement(Api, 236970593), '1454811'), "KSL TV": (PlacementUtils.getPlacement(Api, 237246942), '4259704'), "Amazon AAP - SS": (PlacementUtils.getPlacement(Api, 237803655), '5087264'), "Hulu Latino": (PlacementUtils.getPlacement(Api, 236696286), '3161248'), "EMX Digital": (PlacementUtils.getPlacement(Api, 235786514), '4942271'), "Comcast Digital": (PlacementUtils.getPlacement(Api, 235823848), '4212262'), "Amobee": (PlacementUtils.getPlacement(Api, 235819558), '1547892'), "Premion": (PlacementUtils.getPlacement(Api, 227647443), '4161236'), "roku.com": (PlacementUtils.getPlacement(Api, 237116714), '2259437'), "Crackle.com": (PlacementUtils.getPlacement(Api, 237447154), '2709502'), "FoxSports.com": (PlacementUtils.getPlacement(Api, 237777513), '1512120'), "Hulu.com 1": (PlacementUtils.getPlacement(Api, 227393591), '1408680'), "Conversant, Inc": (PlacementUtils.getPlacement(Api, 235826116), '2216548'), "Univision": (PlacementUtils.getPlacement(Api, 237105971), '1388034'), "CBS Interactive": (PlacementUtils.getPlacement(Api, 227390249), '1378712'),