Rev 9262 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.googleadwords.util;import java.rmi.RemoteException;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import adwords.axis.v201309.basicoperations.GetTextAds;import in.shop2020.googleadwords.AdwordsAdGroup;import in.shop2020.googleadwords.AdwordsAdGroupAd;import in.shop2020.googleadwords.GoogleAdwordsService.Client;import in.shop2020.googleadwords.GoogleAdwordsServiceException;import in.shop2020.thrift.clients.AdwordsClient;/** Class AdwordsSaholicTextAdsSync - To Sync Text Ads between Google Adwords and our Database.*/public class AdwordsSaholicTextAdsSync{private static Logger logger = LoggerFactory.getLogger(AdwordsSaholicTextAdsSync.class);public static void main(String[] args) {AdwordsClient adwordsServiceClient = null;try {adwordsServiceClient = new AdwordsClient();} catch (TTransportException e) {e.printStackTrace();}Client client = adwordsServiceClient.getClient();List<AdwordsAdGroup> adgroupList = null;try {adgroupList = client.getAllAdwordsAdGroups();} catch (GoogleAdwordsServiceException e1) {System.out.println("Error while requesting All AdGroups Google Adwords Service.."+e1.getMessage());logger.error("Error while requesting All AdGroups Google Adwords Service.."+e1.getMessage());} catch (TException e1) {System.out.println("Error while getting All AdGroups Google Adwords Service.."+e1.getMessage());logger.error("Error while getting All AdGroups Google Adwords Service.."+e1.getMessage());}/*List<AdwordsAdGroupAd> adgroupadList =null;try {adgroupadList = client.getAllAdwordsAdGroupAds();} catch (GoogleAdwordsServiceException e) {e.printStackTrace();} catch (TException e) {e.printStackTrace();}*//*Set<Long> avaliableAdGroupSet = new HashSet<Long>();for(AdwordsAdGroupAd adgroupad : adgroupadList ){avaliableAdGroupSet.add(adgroupad.getAdgroupId());}System.out.println("Adgroup Size"+avaliableAdGroupSet.size());for(Long l : avaliableAdGroupSet){System.out.println(l);}*/List<Long> errorAdgroups = new ArrayList<Long>();for(int j=0;j<adgroupList.size();j++){AdwordsAdGroup adgroup = adgroupList.get(j);long adgroupId = adgroup.getAdgroupId();/*if(!avaliableAdGroupSet.contains((Long)adgroupId)){*/List<Map<String, String>> adgroupAdDataMainList = null;try {adgroupAdDataMainList = GetTextAds.runExample(adgroupId);} catch (Exception e) {e.printStackTrace();errorAdgroups.add(adgroupId);continue;}if(adgroupAdDataMainList!=null){for(int i=0;i<adgroupAdDataMainList.size();i++){Map<String,String> adgroupAdDataMap = adgroupAdDataMainList.get(i);if(adgroupAdDataMap!=null){System.out.println(adgroupAdDataMap.get("AdgroupId")+" "+adgroupAdDataMap.get("AdgroupAdId")+" "+adgroupAdDataMap.get("Headline")+ " "+adgroupAdDataMap.get("DisplayUrl")+" "+adgroupAdDataMap.get("Descrption1")+" "+adgroupAdDataMap.get("Descrption2")+" "+adgroupAdDataMap.get("Url"));AdwordsAdGroupAd adgroupadDB = null;try {adgroupadDB = client.getAdwordsAdgroupAdByAdId(Long.parseLong(adgroupAdDataMap.get("AdgroupAdId")));} catch (NumberFormatException e1) {System.out.println(e1.getMessage());} catch (GoogleAdwordsServiceException e1) {System.out.println("Error while requesting TextAd Google Adwords Service.."+e1.getMessage());logger.error("Error while requesting TextAd Google Adwords Service.."+e1.getMessage());} catch (TException e1) {System.out.println("Error while getting TextAd Google Adwords Service.."+e1.getMessage());logger.error("Error while getting TextAd Google Adwords Service.."+e1.getMessage());}AdwordsAdGroupAd adgroupad= new AdwordsAdGroupAd();adgroupad.setAdgroupId(Long.parseLong(adgroupAdDataMap.get("AdgroupId")));adgroupad.setCampaignId(adgroup.getCampaignId());adgroupad.setAdgroupadId(Long.parseLong(adgroupAdDataMap.get("AdgroupAdId")));adgroupad.setHeadline(adgroupAdDataMap.get("Headline"));adgroupad.setDisplayUrl(adgroupAdDataMap.get("DisplayUrl"));adgroupad.setDescription1(adgroupAdDataMap.get("Descrption1"));adgroupad.setDescription2(adgroupAdDataMap.get("Descrption2"));adgroupad.setUrl(adgroupAdDataMap.get("Url"));if(adgroupadDB.getAdgroupadId()>0l){adgroupad.setId(adgroupadDB.getId());try {client.updateAdwordsAdGroupAd(adgroupad);} catch (GoogleAdwordsServiceException e) {System.out.println("Error while Requesting Updation TextAd Google Adwords Service.."+e.getMessage());logger.error("Error while Requesting Updation TextAd Google Adwords Service.."+e.getMessage());} catch (TException e) {System.out.println("Error while Updating TextAd Google Adwords Service.."+e.getMessage());logger.error("Error while Updating TextAd Google Adwords Service.."+e.getMessage());}}else{try {client.addAdwordsAdGroupAd(adgroupad);} catch (GoogleAdwordsServiceException e) {System.out.println("Error while Requesting Addition TextAd Google Adwords Service.."+e.getMessage());logger.error("Error while Requesting Addition TextAd Google Adwords Service.."+e.getMessage());} catch (TException e) {System.out.println("Error while Adding TextAd Google Adwords Service.."+e.getMessage());logger.error("Error while Adding TextAd Google Adwords Service.."+e.getMessage());}}}}}}System.out.println("$$$$$$$$$$$$$$$$$$ Error AdGroups List $$$$$$$$$$$$$$$$$$$$$$$$$");for(Long l : errorAdgroups){System.out.println(l);}}}