| 9225 |
manish.sha |
1 |
package in.shop2020.googleadwords.util;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Collections;
|
|
|
5 |
import java.util.HashSet;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Set;
|
|
|
8 |
import java.util.regex.Matcher;
|
|
|
9 |
import java.util.regex.Pattern;
|
|
|
10 |
|
|
|
11 |
import org.apache.thrift.TException;
|
|
|
12 |
import org.apache.thrift.transport.TTransportException;
|
| 9262 |
manish.sha |
13 |
import org.slf4j.Logger;
|
|
|
14 |
import org.slf4j.LoggerFactory;
|
| 9225 |
manish.sha |
15 |
|
|
|
16 |
import adwords.axis.v201309.basicoperations.AddTextAds;
|
|
|
17 |
import adwords.axis.v201309.basicoperations.PauseAd;
|
|
|
18 |
|
|
|
19 |
import in.shop2020.googleadwords.AdwordsAdGroup;
|
|
|
20 |
import in.shop2020.googleadwords.AdwordsAdGroupAd;
|
|
|
21 |
import in.shop2020.googleadwords.GoogleAdwordsServiceException;
|
|
|
22 |
import in.shop2020.googleadwords.GoogleAdwordsService.Client;
|
|
|
23 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
|
|
24 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
25 |
import in.shop2020.model.v1.catalog.status;
|
|
|
26 |
import in.shop2020.thrift.clients.AdwordsClient;
|
|
|
27 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
28 |
|
| 9262 |
manish.sha |
29 |
/*
|
|
|
30 |
* Class AdwordsTextAdItemPriceSync - To Change Price of Item in the AdGroup Text Ad
|
|
|
31 |
* Accordingly to Item Price stored in our system.
|
|
|
32 |
*/
|
|
|
33 |
|
| 9225 |
manish.sha |
34 |
public class AdwordsTextAdItemPriceSync{
|
| 9262 |
manish.sha |
35 |
private static Logger logger = LoggerFactory.getLogger(AdwordsTextAdItemPriceSync.class);
|
|
|
36 |
|
| 9225 |
manish.sha |
37 |
public static void main(String[] args) {
|
|
|
38 |
AdwordsClient adwordsServiceClient= null;
|
|
|
39 |
CatalogClient catalogServiceClient = null;
|
|
|
40 |
try {
|
|
|
41 |
adwordsServiceClient = new AdwordsClient();
|
|
|
42 |
catalogServiceClient = new CatalogClient();
|
|
|
43 |
} catch (TTransportException e1) {
|
| 9262 |
manish.sha |
44 |
System.out.println("Error while Getting AdwordsClient.. "+e1.getMessage());
|
|
|
45 |
logger.error("Error while Getting AdwordsClient.. "+e1.getMessage());
|
| 9225 |
manish.sha |
46 |
}
|
|
|
47 |
|
|
|
48 |
if(adwordsServiceClient!=null){
|
|
|
49 |
Client client = adwordsServiceClient.getClient();
|
| 9262 |
manish.sha |
50 |
//Get List of All Running AdGroups
|
| 9225 |
manish.sha |
51 |
List<AdwordsAdGroup> adgroupList =null;
|
|
|
52 |
try {
|
|
|
53 |
adgroupList = client.getAllAdwordsAdGroups();
|
|
|
54 |
} catch (GoogleAdwordsServiceException e) {
|
| 9262 |
manish.sha |
55 |
System.out.println("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
|
|
|
56 |
logger.error("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
|
| 9225 |
manish.sha |
57 |
} catch (TException e) {
|
| 9262 |
manish.sha |
58 |
System.out.println("Error while Getting Data from AdwordsClient.. "+e.getMessage());
|
|
|
59 |
logger.error("Error while Getting Data from AdwordsClient.. "+e.getMessage());
|
| 9225 |
manish.sha |
60 |
}
|
| 9262 |
manish.sha |
61 |
|
|
|
62 |
//Creating Set of AdGroups that having Catalog Id greater than zero.
|
| 9225 |
manish.sha |
63 |
Set<AdwordsAdGroup> adGroupSet = new HashSet<AdwordsAdGroup>();
|
|
|
64 |
for(AdwordsAdGroup adgroup: adgroupList){
|
|
|
65 |
if(adgroup.getCatalogItemId()>0L){
|
|
|
66 |
adGroupSet.add(adgroup);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
List<Long> errorAdGroupList = new ArrayList<Long>();
|
|
|
70 |
List<Long> errorTextAdList = new ArrayList<Long>();
|
|
|
71 |
for(AdwordsAdGroup adGroup : adGroupSet){
|
|
|
72 |
if(catalogServiceClient!=null){
|
|
|
73 |
long systemItemPrice = 0l;
|
|
|
74 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
|
| 9262 |
manish.sha |
75 |
//Get List of Items corresponding to Catalog Id.
|
| 9225 |
manish.sha |
76 |
List<Item> itemList = null;
|
|
|
77 |
try {
|
|
|
78 |
itemList = catalogClient.getItemsByCatalogId(adGroup.getCatalogItemId());
|
| 9262 |
manish.sha |
79 |
} catch (CatalogServiceException e) {
|
|
|
80 |
System.out.println("Catalog Service EXception.."+e.getMessage());
|
|
|
81 |
logger.error("Catalog Service EXception.."+e.getMessage());
|
|
|
82 |
} catch (TException e) {
|
|
|
83 |
System.out.println("Error While Getting Data from Catalog Client"+e.getMessage());
|
|
|
84 |
logger.error("Error While Getting Data from Catalog Client"+e.getMessage());
|
| 9225 |
manish.sha |
85 |
}
|
| 9262 |
manish.sha |
86 |
//Get Item system price. Here minimum item price will be considered.
|
| 9225 |
manish.sha |
87 |
List<Double> priceList = new ArrayList<Double>();
|
|
|
88 |
if(itemList!=null){
|
|
|
89 |
for(Item item : itemList){
|
|
|
90 |
if(status.ACTIVE==item.getItemStatus()){
|
|
|
91 |
priceList.add(item.getSellingPrice());
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
else{
|
|
|
96 |
continue;
|
|
|
97 |
}
|
|
|
98 |
Collections.sort(priceList);
|
|
|
99 |
systemItemPrice = (long)priceList.get(0).doubleValue();
|
|
|
100 |
long priceAtGoogleEnd = 0l;
|
| 9262 |
manish.sha |
101 |
//Get All AdGroup Ads as per AdGroup Id.
|
| 9225 |
manish.sha |
102 |
List<AdwordsAdGroupAd> textAdList = null;
|
|
|
103 |
try {
|
|
|
104 |
textAdList = client.getAdwordsAdGroupAdsByAdgroupId(adGroup.getAdgroupId());
|
|
|
105 |
if(textAdList!=null){
|
|
|
106 |
for(AdwordsAdGroupAd textAd : textAdList){
|
| 9262 |
manish.sha |
107 |
//Check For Price Pattern in the Ad.
|
| 9225 |
manish.sha |
108 |
Pattern pattern = Pattern.compile("Rs(.*?)#");
|
|
|
109 |
Matcher matcher = pattern.matcher(textAd.getDescription1());
|
|
|
110 |
String change = "";
|
|
|
111 |
while (matcher.find()) {
|
|
|
112 |
change=matcher.group(1);
|
|
|
113 |
}
|
|
|
114 |
if("".equalsIgnoreCase(change)){
|
|
|
115 |
continue;
|
|
|
116 |
}
|
|
|
117 |
try{
|
|
|
118 |
priceAtGoogleEnd = Long.parseLong(change);
|
|
|
119 |
if(priceAtGoogleEnd > 0l){
|
| 9262 |
manish.sha |
120 |
//Check for Price Change
|
| 9225 |
manish.sha |
121 |
if(priceAtGoogleEnd != systemItemPrice){
|
| 9262 |
manish.sha |
122 |
//Change status of Current Ad to PAUSED and Add a new Ad.
|
| 9225 |
manish.sha |
123 |
try {
|
|
|
124 |
AdwordsAdGroupAd adgrpad = new AdwordsAdGroupAd();
|
|
|
125 |
adgrpad.setAdgroupadId(PauseAd.runExample(textAd.getAdgroupId(), textAd.getAdgroupadId()));
|
|
|
126 |
adgrpad.setAdgroupId(textAd.getAdgroupId());
|
|
|
127 |
adgrpad.setCampaignId(textAd.getCampaignId());
|
|
|
128 |
adgrpad.setDescription1(textAd.getDescription1());
|
|
|
129 |
adgrpad.setDescription2(textAd.getDescription2());
|
|
|
130 |
adgrpad.setDisplayUrl(textAd.getDisplayUrl());
|
|
|
131 |
adgrpad.setHeadline(textAd.getHeadline());
|
|
|
132 |
adgrpad.setUrl(textAd.getUrl());
|
|
|
133 |
client.updateAdwordsAdGroupAd(adgrpad);
|
|
|
134 |
} catch (Exception e1) {
|
| 9262 |
manish.sha |
135 |
System.out.println("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
|
|
|
136 |
logger.error("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
|
| 9225 |
manish.sha |
137 |
continue;
|
|
|
138 |
}
|
|
|
139 |
String newDescription1 = textAd.getDescription1().replace(change, systemItemPrice+"");
|
|
|
140 |
Long adGroupAdId =0l;
|
|
|
141 |
try {
|
|
|
142 |
adGroupAdId = AddTextAds.runExample(textAd.getAdgroupId(), textAd.getHeadline(), newDescription1, textAd.getDescription2(), textAd.getUrl(), textAd.getDisplayUrl());
|
|
|
143 |
} catch (Exception e) {
|
|
|
144 |
errorAdGroupList.add(textAd.getAdgroupId());
|
|
|
145 |
errorTextAdList.add(textAd.getAdgroupadId());
|
|
|
146 |
continue;
|
|
|
147 |
}
|
|
|
148 |
if(adGroupAdId > 0l){
|
|
|
149 |
AdwordsAdGroupAd adgroupad = new AdwordsAdGroupAd();
|
|
|
150 |
adgroupad.setAdgroupadId(adGroupAdId);
|
|
|
151 |
adgroupad.setAdgroupId(textAd.getAdgroupId());
|
|
|
152 |
adgroupad.setCampaignId(textAd.getCampaignId());
|
|
|
153 |
adgroupad.setDescription1(newDescription1);
|
|
|
154 |
adgroupad.setDescription2(textAd.getDescription2());
|
|
|
155 |
adgroupad.setDisplayUrl(textAd.getDisplayUrl());
|
|
|
156 |
adgroupad.setHeadline(textAd.getHeadline());
|
|
|
157 |
adgroupad.setUrl(textAd.getUrl());
|
|
|
158 |
client.addAdwordsAdGroupAd(adgroupad);
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
} catch(NumberFormatException e){
|
|
|
163 |
System.out.println(e.getMessage());
|
|
|
164 |
continue;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
170 |
e.printStackTrace();
|
|
|
171 |
} catch (TException e) {
|
|
|
172 |
e.printStackTrace();
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
System.out.println("Error Ad Group List");
|
|
|
179 |
for(Long l: errorAdGroupList){
|
|
|
180 |
System.out.println(l);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
System.out.println("Error Text Ad List");
|
|
|
184 |
for(Long l: errorTextAdList){
|
|
|
185 |
System.out.println(l);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
}
|