def pullAdsIntoCampaign(campaign, placement, modifiedAds=None): print("pulling ads") videocodes = [ "»FP»", "»NC»", "»TV»", "»VP»", "»VO»", "»VP»", "»VS»" ] englishAds = ["431196351", "431196357", "431196360", "431196366"] spanishAds = ["431196348", "431196354", "431196363", "431196345"] videoAds = ["431196342", "431196372", "431196375", "431196369"] videoHispAds = ["433756626", "433757505", "433830394", "433832422"] adsToUse = None if "Hispanic" in campaign.body["name"]: adsToUse = spanishAds else: adsToUse = englishAds for code in videocodes: if code in placement.body["name"]: if "Hispanic" in campaign.body["name"]: adsToUse = videoHispAds else: adsToUse = videoAds if modifiedAds != None or len(modifiedAds) == 0: adsToUse = getModifiedAds(adsToUse, modifiedAds) for ads in adsToUse: currentAd = Ad(ads, self.eventLoop, self.session).copy(campaign.body["id"]) currentAd.insertAd()
def analyzePlacementAssignments(placementArray): addArray = [x for x in placementArray if x["action"] == "Add"] removeArray = [x for x in placementArray if x["action"] == "Remove"] for element in addArray: placementIDPattern = re.compile('id:(.*,)') idString = element["newValue"] idGrouping = re.search(placementIDPattern, idString).group(0) placementID = re.sub("id: |,", "", idGrouping) adId = element["objectId"] if adId in invalidAdSet: continue currentAd = checkSession(Ad(adId, initialEventLoop, initialSession)) try: currentAssignments = currentAd.body["creativeRotation"][ "creativeAssignments"] except: invalidAdSet.add(currentAd.body["id"]) print(adId) continue currentPlacement = checkSession( Placement(placementID, initialEventLoop, initialSession)) AdObject = extractAdInfo(currentAd) PlacementObject = extractPlacementInfo(currentPlacement) SiteObject = extractSiteInfo( checkSession( Sites(currentPlacement.body['siteId'], initialEventLoop, initialSession))) CampaignObject = extractCampaignInfo( checkSession( AsyncCampaign(currentPlacement.body["campaignId"], initialEventLoop, initialSession))) for assignment in currentAssignments: finalObject = {} CreativeInfo = checkSession(Creative(assignment["creativeId"])) CreativeInfo.body["clickthrough"] = assignment[ "clickThroughUrl"]['computedClickThroughUrl'] timestamp = int( CreativeInfo.body["lastModifiedInfo"]["time"]) / 1e3 CreativeInfo.body[ "startTime"] = datetime.datetime.fromtimestamp( timestamp).strftime('%m/%d/%y %I:%M %p') CreativeInfo = extractCreativeInfo(CreativeInfo) finalObject.update(AdObject) finalObject.update(PlacementObject) finalObject.update(SiteObject) finalObject.update(CampaignObject) finalObject.update(CreativeInfo) generateInsertQuery(finalObject) for element in removeArray: placementIDPattern = re.compile('id:(.*,)') idString = element["oldValue"] idGrouping = re.search(placementIDPattern, idString).group(0) placementID = re.sub("id: |,", "", idGrouping) adId = element["objectId"] selectObject = {"Ad_ID": adId, "Placement_ID": placementID} DeactivateRows(selectObject)
def getModifiedAds(adsToUse, existingads): modifiedAds = [] existingads = [x["name"].strip() for x in existingads] for ad in adsToUse: currentAd = Ad(ad, self.eventLoop, self.session) adName = currentAd.body["name"] if adName.strip() not in existingads: modifiedAds.append(ad) return modifiedAds
def updateAdInfo(adArray): for ad in adArray: adId = ad["objectId"] if adId in updateAdSet: continue adInfo = extractAdInfo( checkSession(Ad(adId, initialEventLoop, initialSession))) selectObject = {"Ad_ID": adId} updateRows(adInfo, selectObject) updateAdSet.add(adId)
def isTrafficked(self,csdDict): import datetime from modules.Ad import Ad from modules.Creative import Creative from modules.AsyncCampaign import AsyncCampaign import numpy currentMonth = datetime.datetime.now().strftime("%B").lower() currentDate = datetime.datetime.now() def getCSDCreatives(): campaign = checkSession(AsyncCampaign(self.body["campaignId"],initialEventLoop,initialSession), initialSession) df = csdDict[campaign.body["name"].strip()] creativeArray = df.loc[df["Id"] == numpy.int64(self.body["id"])].iloc[0].values.tolist() creativeArray = creativeArray[9:len(creativeArray)] creativeArray = [x for x in creativeArray if isinstance(x,str)] return creativeArray def formatDateTime(datetimeString): import datetime date = datetimeString.split('T')[0] time = datetimeString.split('T')[1] year = int(date.split('-')[0]) month = int(date.split('-')[1]) day = int(date.split('-')[2]) date = datetime.date(year,month,day) date = date.strftime("%m/%d/%y") hour = int(time.split(':')[0]) minute = int(time.split(':')[1]) time = datetime.time(hour,minute) time = time.strftime("%I:%M %p") return (date + " " + time) def checkDate(dateString): try: date = datetime.datetime.strptime(dateString, "%Y-%m-%dT%H:%M:%S.000Z") > currentDate except: date = datetime.datetime.strptime(dateString, "%Y-%m-%dT%H:%M:%S.999Z") > currentDate return date def checkSession(session, initialSession): if initialSession != session: return session else: return initialSession initialSession = self.session initialEventLoop = self.eventLoop self.getAdList() self.testAds = self.ads response = [x for x in self.ads if "Brand-neutral" not in x['name'] and "TRACKING" not in x["name"] and x["active"] == True and "AD_SERVING_DEFAULT_AD" not in x["type"] and checkDate(x["endTime"])] self.response = response self.ads = [{"id":x["id"]} for x in response] if not self.ads: self.trafficked = False self.creativeName = "Not Trafficked" self.creativeDate = "Not Trafficked" self.adStart = "Not Trafficked" self.adEnd= "Not Trafficked" return self for adBody in self.ads: ad = Ad(adBody["id"],initialEventLoop, initialSession) initialSession = checkSession(ad.session, initialSession) try: creativeAssignments = ad.body["creativeRotation"]["creativeAssignments"] except: continue for creative in creativeAssignments: creativeID = creative["creativeId"] creativeElement = Creative(creativeID,initialEventLoop, initialSession) initialSession = checkSession(creativeElement.session, initialSession) creativeName = creativeElement.body["name"] csdCreatives = getCSDCreatives() creativeNameToTest = "»".join(creativeName.split("»")[:len(creativeName.split("»"))-2]) for element in csdCreatives: creativeTestingString = "»".join(element.split("»")[:len(element.split("»"))-2]) if creativeNameToTest == creativeTestingString: self.trafficked = True self.creativeName = creativeName self.adStart = formatDateTime(ad.body["startTime"]) self.adEnd= formatDateTime(ad.body["endTime"]) timestamp = int(creativeElement.body["lastModifiedInfo"]["time"]) / 1e3 self.creativeDate = datetime.datetime.fromtimestamp(timestamp).strftime('%m/%d/%y %I:%M %p') return self if currentMonth in creativeName.lower(): self.trafficked = True self.creativeName = creativeName self.adStart = formatDateTime(ad.body["startTime"]) self.adEnd= formatDateTime(ad.body["endTime"]) timestamp = int(creativeElement.body["lastModifiedInfo"]["time"]) / 1e3 self.creativeDate = datetime.datetime.fromtimestamp(timestamp).strftime('%m/%d/%y %I:%M %p') return self else: self.trafficked = False self.creativeName = creativeName self.adStart = formatDateTime(ad.body["startTime"]) self.adEnd= formatDateTime(ad.body["endTime"]) timestamp = int(creativeElement.body["lastModifiedInfo"]["time"]) / 1e3 self.creativeDate = datetime.datetime.fromtimestamp(timestamp).strftime('%m/%d/%y %I:%M %p') return self
def activateTrackingAds(self): from modules.Ad import Ad from modules.AsyncCampaign import AsyncCampaign from modules.Placements import Placement import re # async def wait(payload, placementObject): # async with self.session.patch(self.url, headers=self.auth, data=self.json.dumps(payload)) as r: # text = await r.text() # if r.status == 200: # response = self.json.loads(text)['placementAssignments'] # if placementObject in response: # print("{0} updated successfully".format(self.body['name'])) # else: # print("{0} failed to update.".format(self.body['name'])) # self.handleError(text) def getModifiedAds(adsToUse, existingads): modifiedAds = [] existingads = [x["name"].strip() for x in existingads] for ad in adsToUse: currentAd = Ad(ad, self.eventLoop, self.session) adName = currentAd.body["name"] if adName.strip() not in existingads: modifiedAds.append(ad) return modifiedAds def pullAdsIntoCampaign(campaign, placement, modifiedAds=None): print("pulling ads") videocodes = [ "»FP»", "»NC»", "»TV»", "»VP»", "»VO»", "»VP»", "»VS»" ] englishAds = ["431196351", "431196357", "431196360", "431196366"] spanishAds = ["431196348", "431196354", "431196363", "431196345"] videoAds = ["431196342", "431196372", "431196375", "431196369"] videoHispAds = ["433756626", "433757505", "433830394", "433832422"] adsToUse = None if "Hispanic" in campaign.body["name"]: adsToUse = spanishAds else: adsToUse = englishAds for code in videocodes: if code in placement.body["name"]: if "Hispanic" in campaign.body["name"]: adsToUse = videoHispAds else: adsToUse = videoAds if modifiedAds != None or len(modifiedAds) == 0: adsToUse = getModifiedAds(adsToUse, modifiedAds) for ads in adsToUse: currentAd = Ad(ads, self.eventLoop, self.session).copy(campaign.body["id"]) currentAd.insertAd() def associatePlacement(ad, placement): try: ad.body['placementAssignments'] except: ad.body['placementAssignments'] = [] placementObject = { "active": True, "placementIdDimensionValue": placement.body["idDimensionValue"], "sslRequired": placement.body["sslRequired"], "placementId": placement.body["id"] } if placementObject not in ad.body['placementAssignments']: ad.body['placementAssignments'].append(placementObject) ad.url = "https://www.googleapis.com/dfareporting/v3.1/userprofiles/{profile_id}/ads?id={adId}".format( profile_id=self.profile_id, adId=ad.body["id"]) payload = { "placementAssignments": ad.body['placementAssignments'] } ad.insertPlacement(payload, placementObject, placement.body["campaignId"]).activateAd() regex = re.compile("[1-5]L(M|G)") for placmentID in range(0, len(self.logs)): print("checking placement %s of %s" % (str(placmentID + 1), len(self.logs))) placement = Placement(self.logs[placmentID], self.eventLoop, self.session).getAdList() print(placement.body["name"]) campaign = AsyncCampaign(placement.body['campaignId'], self.eventLoop, self.session) campaignName = campaign.body["name"] if ("_SS_" in placement.body["name"] or "»SS»" in placement.body["name"]) and regex.search(campaignName): placement.getAdList() placement.ads = [ x for x in placement.ads if "TRACKING" in x["name"] and x["archived"] == False ] campaign.getAds() trackingAdList = [ x for x in campaign.adList if "TRACKING" in x["name"] and x["archived"] == False ] print("tracking ad length = : " + str(len(trackingAdList))) if len(trackingAdList) > 4: BonusAd = [ x for x in campaign.adList if "TRACKING" in x["name"] and "Bonus" in x["name"] ][0] FirstAd = [ x for x in campaign.adList if "TRACKING" in x["name"] and "First" in x["name"] ][0] HolidayAd = [ x for x in campaign.adList if "TRACKING" in x["name"] and "Holiday" in x["name"] ][0] GeneralAd = [ x for x in campaign.adList if "TRACKING" in x["name"] and "General" in x["name"] ][0] trackingAdList = [BonusAd, HolidayAd, FirstAd, GeneralAd] if len(trackingAdList) < 4: pullAdsIntoCampaign(campaign, placement, trackingAdList) campaign.getAds() trackingAdList = [ x for x in campaign.adList if "TRACKING" in x["name"] ] numberOfAds = len(placement.ads) print(numberOfAds) if numberOfAds < 4: for ad in trackingAdList: adObject = Ad(ad["id"], self.eventLoop, self.session) associatePlacement(adObject, placement) return self
def analyzeCreativeAssignments(creativeArray): addArray = [x for x in creativeArray if x["action"] == "Add"] removeArray = [x for x in creativeArray if x["action"] == "Remove"] updateArray = [x for x in creativeArray if x["action"] == "Update"] for element in addArray: creativeIDPattern = re.compile('id:(.*, a)') idString = element["newValue"] idGrouping = re.search(creativeIDPattern, idString).group(0) creativeID = re.sub("id: |, a", "", idGrouping) adId = element["objectId"] selectObject = {"Ad_ID": adId} currentAd = checkSession(Ad(adId, initialEventLoop, initialSession)) for assignment in currentAd.body["creativeRotation"][ "creativeAssignments"]: if assignment['creativeId'] == creativeID: CreativeInfo = checkSession( Creative(assignment["creativeId"])) CreativeInfo.body["clickthrough"] = assignment[ "clickThroughUrl"]['computedClickThroughUrl'] timestamp = int( CreativeInfo.body["lastModifiedInfo"]["time"]) / 1e3 CreativeInfo.body[ "startTime"] = datetime.datetime.fromtimestamp( timestamp).strftime('%m/%d/%y %I:%M %p') CreativeInfo = extractCreativeInfo(CreativeInfo) break rows = selectQuery(selectObject) for column in CreativeInfo: rows[column] = CreativeInfo[column] for row in rows.iterrows(): generateInsertQuery(row[1].to_dict()) for element in removeArray: creativeIDPattern = re.compile('id:(.*, a)') idString = element["oldValue"] idGrouping = re.search(creativeIDPattern, idString).group(0) creativeID = re.sub("id: |, a", "", idGrouping) adId = element["objectId"] selectObject = {"Ad_ID": adId, "Creative_ID": creativeID} DeactivateRows(selectObject) for element in updateArray: creativeIDPattern = re.compile('id:(.*, a)') idString = element["oldValue"] idGrouping = re.search(creativeIDPattern, idString).group(0) creativeID = re.sub("id: |, a", "", idGrouping) if creativeID in updateCreativeSet: continue adId = element["objectId"] currentAd = checkSession(Ad(adId, initialEventLoop, initialSession)) for assignment in currentAd.body["creativeRotation"][ "creativeAssignments"]: if assignment['creativeId'] == creativeID: CreativeInfo = checkSession( Creative(assignment["creativeId"])) CreativeInfo.body["clickthrough"] = assignment[ "clickThroughUrl"]['computedClickThroughUrl'] timestamp = int( CreativeInfo.body["lastModifiedInfo"]["time"]) / 1e3 CreativeInfo.body[ "startTime"] = datetime.datetime.fromtimestamp( timestamp).strftime('%m/%d/%y %I:%M %p') CreativeInfo = extractCreativeInfo(CreativeInfo) selectObject = {"Creative_ID": creativeID} updateRows(CreativeInfo, selectObject) updateCreativeSet.add(creativeID) break
def analyzeCampaignList(campaignList, process, queue, p1Num, p2Num): import datetime campaignObject = AsyncCampaign(20124958) initialSession = campaignObject.session initialEventLoop = campaignObject.eventLoop rowArray = [] length = len(campaignList) print(length) if process == "One": campaignRange = range(0,length,1) step = 1 number = p1Num else: campaignRange = range(length - 1,0,-1) step = -1 number = p2Num length = len(campaignList) for campaign in campaignRange: print("analyzing campaign {2} out of {3}: {0} Process:{1}".format(campaignList[campaign]["name"], process, campaign + 1, length)) number.value += step print("P1 = {0}, P2 = {1}".format(p1Num.value, p2Num.value)) if p1Num.value > p2Num.value: print("Process: {0} exiting".format(process)) queue.put(rowArray) return campaignBody = AsyncCampaign(campaignList[campaign]["id"], initialEventLoop, initialSession).getPlacementList() initialSession = checkSession(campaignBody.session, initialSession) for placementObject in campaignBody.placements: placement = Placement(placementObject["id"], initialEventLoop, initialSession).getAdList() response = [x for x in placement.ads if "Brand-neutral" not in x['name'] and "TRACKING" not in x["name"] and x["active"] == True and "AD_SERVING_DEFAULT_AD" not in x["type"]] placement.ads = [{"id":x["id"]} for x in response] initialSession = checkSession(placement.session, initialSession) if placement.body["siteId"] not in MasterSiteList: site = Sites(placement.body["siteId"], initialEventLoop, initialSession).body MasterSiteList[site["id"]] = site["name"] else: siteBody = {"id": placement.body["siteId"], "name":MasterSiteList[placement.body["siteId"]]} site = siteBody MasterSiteList[site["id"]] = site["name"] siteName = "{0} ({1})".format(site["name"], site["id"]) for adBody in placement.ads: if adBody["id"] not in MasterAdList: ad = Ad(adBody["id"],initialEventLoop, initialSession) initialSession = checkSession(ad.session, initialSession) MasterAdList[adBody["id"]] = ad else: ad = MasterAdList[adBody["id"]] try: hardCutoff = ad.body["deliverySchedule"]["hardCutoff"] except: hardCutoff = True try: creativeAssignments = ad.body["creativeRotation"]["creativeAssignments"] except: pass for creative in creativeAssignments: clickThroughURL = creative["clickThroughUrl"]["computedClickThroughUrl"] creativeID = creative["creativeId"] if creativeID not in MasterCreativeList: creativeElement = Creative(creativeID,initialEventLoop, initialSession) initialSession = checkSession(creativeElement.session, initialSession) MasterCreativeList[creativeID] = creativeElement else: creativeElement = MasterCreativeList[creativeID] creativeName = creativeElement.body["name"] creativeType = creativeElement.body["type"] adObject = {"Campaign Name": campaignBody.body['name']} adObject["Campaign ID"] = campaignBody.body["id"] adObject["Site"] = siteName adObject["Placement ID"] = placement.body["id"] adObject["Placement Name"] = placement.body['name'] adObject["Start Date"] = placement.body["pricingSchedule"]["startDate"] adObject["End Date"] = placement.body["pricingSchedule"]["endDate"] adObject["Ad ID"] = ad.body['id'] adObject["Ad Name"] = ad.body['name'] adObject["Ad Type"] = ad.body["type"] adObject["Ad is Active"] = TrueToYes(ad.body['active']) adObject["Ad Start Time"] =formatDateTime(ad.body["startTime"]) adObject["Ad End Time"] =formatDateTime(ad.body["endTime"]) adObject["Hard Cut-Off"] = TrueToYes(hardCutoff) adObject["Creative ID"] = creativeID timestamp = int(creativeElement.body["lastModifiedInfo"]["time"]) / 1e3 adObject["Creative Date"] = datetime.datetime.fromtimestamp(timestamp).strftime('%m/%d/%y %I:%M %p') adObject["Creative Name"] = creativeName adObject["Creative Type"] = creativeType adObject["Creative Click-Through URL"] = clickThroughURL rowArray.append(adObject) print("appending %s Process: %s" % (creativeID, process))