| 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;
|
|
|
13 |
|
|
|
14 |
import adwords.axis.v201309.basicoperations.AddTextAds;
|
|
|
15 |
import adwords.axis.v201309.basicoperations.PauseAd;
|
|
|
16 |
|
|
|
17 |
import in.shop2020.googleadwords.AdwordsAdGroup;
|
|
|
18 |
import in.shop2020.googleadwords.AdwordsAdGroupAd;
|
|
|
19 |
import in.shop2020.googleadwords.GoogleAdwordsServiceException;
|
|
|
20 |
import in.shop2020.googleadwords.GoogleAdwordsService.Client;
|
|
|
21 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
|
|
22 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
23 |
import in.shop2020.model.v1.catalog.status;
|
|
|
24 |
import in.shop2020.thrift.clients.AdwordsClient;
|
|
|
25 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
26 |
|
|
|
27 |
public class AdwordsTextAdItemPriceSync{
|
|
|
28 |
public static void main(String[] args) {
|
|
|
29 |
AdwordsClient adwordsServiceClient= null;
|
|
|
30 |
CatalogClient catalogServiceClient = null;
|
|
|
31 |
try {
|
|
|
32 |
adwordsServiceClient = new AdwordsClient();
|
|
|
33 |
catalogServiceClient = new CatalogClient();
|
|
|
34 |
} catch (TTransportException e1) {
|
|
|
35 |
e1.printStackTrace();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
if(adwordsServiceClient!=null){
|
|
|
39 |
Client client = adwordsServiceClient.getClient();
|
|
|
40 |
List<AdwordsAdGroup> adgroupList =null;
|
|
|
41 |
try {
|
|
|
42 |
adgroupList = client.getAllAdwordsAdGroups();
|
|
|
43 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
44 |
e.printStackTrace();
|
|
|
45 |
} catch (TException e) {
|
|
|
46 |
e.printStackTrace();
|
|
|
47 |
}
|
|
|
48 |
Set<AdwordsAdGroup> adGroupSet = new HashSet<AdwordsAdGroup>();
|
|
|
49 |
for(AdwordsAdGroup adgroup: adgroupList){
|
|
|
50 |
if(adgroup.getCatalogItemId()>0L){
|
|
|
51 |
adGroupSet.add(adgroup);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
List<Long> errorAdGroupList = new ArrayList<Long>();
|
|
|
55 |
List<Long> errorTextAdList = new ArrayList<Long>();
|
|
|
56 |
for(AdwordsAdGroup adGroup : adGroupSet){
|
|
|
57 |
if(catalogServiceClient!=null){
|
|
|
58 |
long systemItemPrice = 0l;
|
|
|
59 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
|
|
|
60 |
List<Item> itemList = null;
|
|
|
61 |
try {
|
|
|
62 |
itemList = catalogClient.getItemsByCatalogId(adGroup.getCatalogItemId());
|
|
|
63 |
} catch (CatalogServiceException e1) {
|
|
|
64 |
e1.printStackTrace();
|
|
|
65 |
} catch (TException e1) {
|
|
|
66 |
e1.printStackTrace();
|
|
|
67 |
}
|
|
|
68 |
List<Double> priceList = new ArrayList<Double>();
|
|
|
69 |
if(itemList!=null){
|
|
|
70 |
for(Item item : itemList){
|
|
|
71 |
if(status.ACTIVE==item.getItemStatus()){
|
|
|
72 |
priceList.add(item.getSellingPrice());
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
else{
|
|
|
77 |
continue;
|
|
|
78 |
}
|
|
|
79 |
Collections.sort(priceList);
|
|
|
80 |
systemItemPrice = (long)priceList.get(0).doubleValue();
|
|
|
81 |
long priceAtGoogleEnd = 0l;
|
|
|
82 |
List<AdwordsAdGroupAd> textAdList = null;
|
|
|
83 |
try {
|
|
|
84 |
textAdList = client.getAdwordsAdGroupAdsByAdgroupId(adGroup.getAdgroupId());
|
|
|
85 |
if(textAdList!=null){
|
|
|
86 |
for(AdwordsAdGroupAd textAd : textAdList){
|
|
|
87 |
Pattern pattern = Pattern.compile("Rs(.*?)#");
|
|
|
88 |
Matcher matcher = pattern.matcher(textAd.getDescription1());
|
|
|
89 |
String change = "";
|
|
|
90 |
while (matcher.find()) {
|
|
|
91 |
change=matcher.group(1);
|
|
|
92 |
}
|
|
|
93 |
if("".equalsIgnoreCase(change)){
|
|
|
94 |
continue;
|
|
|
95 |
}
|
|
|
96 |
try{
|
|
|
97 |
priceAtGoogleEnd = Long.parseLong(change);
|
|
|
98 |
if(priceAtGoogleEnd > 0l){
|
|
|
99 |
if(priceAtGoogleEnd != systemItemPrice){
|
|
|
100 |
try {
|
|
|
101 |
AdwordsAdGroupAd adgrpad = new AdwordsAdGroupAd();
|
|
|
102 |
adgrpad.setAdgroupadId(PauseAd.runExample(textAd.getAdgroupId(), textAd.getAdgroupadId()));
|
|
|
103 |
adgrpad.setAdgroupId(textAd.getAdgroupId());
|
|
|
104 |
adgrpad.setCampaignId(textAd.getCampaignId());
|
|
|
105 |
adgrpad.setDescription1(textAd.getDescription1());
|
|
|
106 |
adgrpad.setDescription2(textAd.getDescription2());
|
|
|
107 |
adgrpad.setDisplayUrl(textAd.getDisplayUrl());
|
|
|
108 |
adgrpad.setHeadline(textAd.getHeadline());
|
|
|
109 |
adgrpad.setUrl(textAd.getUrl());
|
|
|
110 |
client.updateAdwordsAdGroupAd(adgrpad);
|
|
|
111 |
} catch (Exception e1) {
|
|
|
112 |
System.out.println("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId());
|
|
|
113 |
continue;
|
|
|
114 |
}
|
|
|
115 |
String newDescription1 = textAd.getDescription1().replace(change, systemItemPrice+"");
|
|
|
116 |
Long adGroupAdId =0l;
|
|
|
117 |
try {
|
|
|
118 |
adGroupAdId = AddTextAds.runExample(textAd.getAdgroupId(), textAd.getHeadline(), newDescription1, textAd.getDescription2(), textAd.getUrl(), textAd.getDisplayUrl());
|
|
|
119 |
} catch (Exception e) {
|
|
|
120 |
errorAdGroupList.add(textAd.getAdgroupId());
|
|
|
121 |
errorTextAdList.add(textAd.getAdgroupadId());
|
|
|
122 |
continue;
|
|
|
123 |
}
|
|
|
124 |
if(adGroupAdId > 0l){
|
|
|
125 |
AdwordsAdGroupAd adgroupad = new AdwordsAdGroupAd();
|
|
|
126 |
adgroupad.setAdgroupadId(adGroupAdId);
|
|
|
127 |
adgroupad.setAdgroupId(textAd.getAdgroupId());
|
|
|
128 |
adgroupad.setCampaignId(textAd.getCampaignId());
|
|
|
129 |
adgroupad.setDescription1(newDescription1);
|
|
|
130 |
adgroupad.setDescription2(textAd.getDescription2());
|
|
|
131 |
adgroupad.setDisplayUrl(textAd.getDisplayUrl());
|
|
|
132 |
adgroupad.setHeadline(textAd.getHeadline());
|
|
|
133 |
adgroupad.setUrl(textAd.getUrl());
|
|
|
134 |
client.addAdwordsAdGroupAd(adgroupad);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
} catch(NumberFormatException e){
|
|
|
139 |
System.out.println(e.getMessage());
|
|
|
140 |
continue;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
} catch (GoogleAdwordsServiceException e) {
|
|
|
146 |
e.printStackTrace();
|
|
|
147 |
} catch (TException e) {
|
|
|
148 |
e.printStackTrace();
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
System.out.println("Error Ad Group List");
|
|
|
155 |
for(Long l: errorAdGroupList){
|
|
|
156 |
System.out.println(l);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
System.out.println("Error Text Ad List");
|
|
|
160 |
for(Long l: errorTextAdList){
|
|
|
161 |
System.out.println(l);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|