| 9028 |
manish.sha |
1 |
package in.shop2020.googleadwords.util;
|
|
|
2 |
|
|
|
3 |
import java.rmi.RemoteException;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.HashSet;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Map;
|
|
|
8 |
import java.util.Set;
|
|
|
9 |
|
|
|
10 |
import org.apache.thrift.TException;
|
|
|
11 |
import org.apache.thrift.transport.TTransportException;
|
| 9262 |
manish.sha |
12 |
import org.slf4j.Logger;
|
|
|
13 |
import org.slf4j.LoggerFactory;
|
| 9028 |
manish.sha |
14 |
|
|
|
15 |
import adwords.axis.v201309.basicoperations.GetTextAds;
|
|
|
16 |
|
|
|
17 |
import in.shop2020.googleadwords.AdwordsAdGroup;
|
|
|
18 |
import in.shop2020.googleadwords.AdwordsAdGroupAd;
|
|
|
19 |
import in.shop2020.googleadwords.GoogleAdwordsService.Client;
|
|
|
20 |
import in.shop2020.googleadwords.GoogleAdwordsServiceException;
|
|
|
21 |
import in.shop2020.thrift.clients.AdwordsClient;
|
|
|
22 |
|
| 9262 |
manish.sha |
23 |
/*
|
|
|
24 |
* Class AdwordsSaholicTextAdsSync - To Sync Text Ads between Google Adwords and our Database.
|
|
|
25 |
*/
|
|
|
26 |
|
| 9028 |
manish.sha |
27 |
public class AdwordsSaholicTextAdsSync{
|
| 9262 |
manish.sha |
28 |
private static Logger logger = LoggerFactory.getLogger(AdwordsSaholicTextAdsSync.class);
|
|
|
29 |
|
| 9028 |
manish.sha |
30 |
public static void main(String[] args) {
|
|
|
31 |
AdwordsClient adwordsServiceClient = null;
|
|
|
32 |
try {
|
|
|
33 |
adwordsServiceClient = new AdwordsClient();
|
|
|
34 |
} catch (TTransportException e) {
|
|
|
35 |
e.printStackTrace();
|
|
|
36 |
}
|
|
|
37 |
Client client = adwordsServiceClient.getClient();
|
|
|
38 |
List<AdwordsAdGroup> adgroupList = null;
|
|
|
39 |
try {
|
|
|
40 |
adgroupList = client.getAllAdwordsAdGroups();
|
| 9262 |
manish.sha |
41 |
} catch (GoogleAdwordsServiceException e1) {
|
|
|
42 |
System.out.println("Error while requesting All AdGroups Google Adwords Service.."+e1.getMessage());
|
|
|
43 |
logger.error("Error while requesting All AdGroups Google Adwords Service.."+e1.getMessage());
|
|
|
44 |
} catch (TException e1) {
|
|
|
45 |
System.out.println("Error while getting All AdGroups Google Adwords Service.."+e1.getMessage());
|
|
|
46 |
logger.error("Error while getting All AdGroups Google Adwords Service.."+e1.getMessage());
|
| 9028 |
manish.sha |
47 |
}
|
| 9262 |
manish.sha |
48 |
/*List<AdwordsAdGroupAd> adgroupadList =null;
|
| 9028 |
manish.sha |
49 |
try {
|
|
|
50 |
adgroupadList = client.getAllAdwordsAdGroupAds();
|
|
|
51 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
52 |
e.printStackTrace();
|
|
|
53 |
} catch (TException e) {
|
|
|
54 |
e.printStackTrace();
|
| 9262 |
manish.sha |
55 |
}*/
|
|
|
56 |
/*Set<Long> avaliableAdGroupSet = new HashSet<Long>();
|
| 9028 |
manish.sha |
57 |
for(AdwordsAdGroupAd adgroupad : adgroupadList ){
|
|
|
58 |
avaliableAdGroupSet.add(adgroupad.getAdgroupId());
|
|
|
59 |
}
|
|
|
60 |
System.out.println("Adgroup Size"+avaliableAdGroupSet.size());
|
|
|
61 |
for(Long l : avaliableAdGroupSet){
|
|
|
62 |
System.out.println(l);
|
| 9262 |
manish.sha |
63 |
}*/
|
| 9028 |
manish.sha |
64 |
|
|
|
65 |
List<Long> errorAdgroups = new ArrayList<Long>();
|
| 9262 |
manish.sha |
66 |
for(int j=0;j<adgroupList.size();j++){
|
| 9028 |
manish.sha |
67 |
AdwordsAdGroup adgroup = adgroupList.get(j);
|
|
|
68 |
long adgroupId = adgroup.getAdgroupId();
|
| 9262 |
manish.sha |
69 |
/*if(!avaliableAdGroupSet.contains((Long)adgroupId)){*/
|
|
|
70 |
List<Map<String, String>> adgroupAdDataMainList = null;
|
|
|
71 |
try {
|
|
|
72 |
adgroupAdDataMainList = GetTextAds.runExample(adgroupId);
|
|
|
73 |
} catch (Exception e) {
|
|
|
74 |
e.printStackTrace();
|
|
|
75 |
errorAdgroups.add(adgroupId);
|
|
|
76 |
continue;
|
|
|
77 |
}
|
| 9317 |
manish.sha |
78 |
if(adgroupAdDataMainList!=null){
|
|
|
79 |
for(int i=0;i<adgroupAdDataMainList.size();i++){
|
|
|
80 |
Map<String,String> adgroupAdDataMap = adgroupAdDataMainList.get(i);
|
|
|
81 |
if(adgroupAdDataMap!=null){
|
|
|
82 |
System.out.println(adgroupAdDataMap.get("AdgroupId")+" "
|
|
|
83 |
+adgroupAdDataMap.get("AdgroupAdId")+" "
|
|
|
84 |
+adgroupAdDataMap.get("Headline")+ " "
|
|
|
85 |
+adgroupAdDataMap.get("DisplayUrl")+" "
|
|
|
86 |
+adgroupAdDataMap.get("Descrption1")+" "
|
|
|
87 |
+adgroupAdDataMap.get("Descrption2")+" "
|
|
|
88 |
+adgroupAdDataMap.get("Url"));
|
|
|
89 |
AdwordsAdGroupAd adgroupadDB = null;
|
| 9028 |
manish.sha |
90 |
try {
|
| 9317 |
manish.sha |
91 |
adgroupadDB = client.getAdwordsAdgroupAdByAdId(Long.parseLong(adgroupAdDataMap.get("AdgroupAdId")));
|
|
|
92 |
} catch (NumberFormatException e1) {
|
|
|
93 |
System.out.println(e1.getMessage());
|
|
|
94 |
} catch (GoogleAdwordsServiceException e1) {
|
|
|
95 |
System.out.println("Error while requesting TextAd Google Adwords Service.."+e1.getMessage());
|
|
|
96 |
logger.error("Error while requesting TextAd Google Adwords Service.."+e1.getMessage());
|
|
|
97 |
} catch (TException e1) {
|
|
|
98 |
System.out.println("Error while getting TextAd Google Adwords Service.."+e1.getMessage());
|
|
|
99 |
logger.error("Error while getting TextAd Google Adwords Service.."+e1.getMessage());
|
| 9262 |
manish.sha |
100 |
}
|
| 9317 |
manish.sha |
101 |
AdwordsAdGroupAd adgroupad= new AdwordsAdGroupAd();
|
|
|
102 |
adgroupad.setAdgroupId(Long.parseLong(adgroupAdDataMap.get("AdgroupId")));
|
|
|
103 |
adgroupad.setCampaignId(adgroup.getCampaignId());
|
|
|
104 |
adgroupad.setAdgroupadId(Long.parseLong(adgroupAdDataMap.get("AdgroupAdId")));
|
|
|
105 |
adgroupad.setHeadline(adgroupAdDataMap.get("Headline"));
|
|
|
106 |
adgroupad.setDisplayUrl(adgroupAdDataMap.get("DisplayUrl"));
|
|
|
107 |
adgroupad.setDescription1(adgroupAdDataMap.get("Descrption1"));
|
|
|
108 |
adgroupad.setDescription2(adgroupAdDataMap.get("Descrption2"));
|
|
|
109 |
adgroupad.setUrl(adgroupAdDataMap.get("Url"));
|
|
|
110 |
if(adgroupadDB.getAdgroupadId()>0l){
|
|
|
111 |
adgroupad.setId(adgroupadDB.getId());
|
|
|
112 |
try {
|
|
|
113 |
client.updateAdwordsAdGroupAd(adgroupad);
|
|
|
114 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
115 |
System.out.println("Error while Requesting Updation TextAd Google Adwords Service.."+e.getMessage());
|
|
|
116 |
logger.error("Error while Requesting Updation TextAd Google Adwords Service.."+e.getMessage());
|
|
|
117 |
} catch (TException e) {
|
|
|
118 |
System.out.println("Error while Updating TextAd Google Adwords Service.."+e.getMessage());
|
|
|
119 |
logger.error("Error while Updating TextAd Google Adwords Service.."+e.getMessage());
|
|
|
120 |
}
|
| 9028 |
manish.sha |
121 |
}
|
| 9317 |
manish.sha |
122 |
else{
|
|
|
123 |
try {
|
|
|
124 |
client.addAdwordsAdGroupAd(adgroupad);
|
|
|
125 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
126 |
System.out.println("Error while Requesting Addition TextAd Google Adwords Service.."+e.getMessage());
|
|
|
127 |
logger.error("Error while Requesting Addition TextAd Google Adwords Service.."+e.getMessage());
|
|
|
128 |
} catch (TException e) {
|
|
|
129 |
System.out.println("Error while Adding TextAd Google Adwords Service.."+e.getMessage());
|
|
|
130 |
logger.error("Error while Adding TextAd Google Adwords Service.."+e.getMessage());
|
|
|
131 |
}
|
|
|
132 |
}
|
| 9028 |
manish.sha |
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
System.out.println("$$$$$$$$$$$$$$$$$$ Error AdGroups List $$$$$$$$$$$$$$$$$$$$$$$$$");
|
|
|
138 |
for(Long l : errorAdgroups){
|
|
|
139 |
System.out.println(l);
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|