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