Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
package adwords.axis.v201309.misc;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.Dimensions;
19
import com.google.api.ads.adwords.axis.v201309.cm.Media;
20
import com.google.api.ads.adwords.axis.v201309.cm.MediaPage;
21
import com.google.api.ads.adwords.axis.v201309.cm.MediaServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.MediaSize;
23
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
24
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
25
import com.google.api.ads.adwords.axis.v201309.cm.Predicate;
26
import com.google.api.ads.adwords.axis.v201309.cm.PredicateOperator;
27
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
28
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
29
import com.google.api.ads.adwords.lib.client.AdWordsSession;
30
import com.google.api.ads.common.lib.auth.OfflineCredentials;
31
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
32
import com.google.api.ads.common.lib.utils.Maps;
33
import com.google.api.client.auth.oauth2.Credential;
34
 
35
import java.util.Map;
36
 
37
/**
38
 * This example gets all images and videos. To upload an image, run
39
 * UploadImage.java. To upload video, see:
40
 * http://adwords.google.com/support/aw/bin/answer.py?hl=en&answer=39454.
41
 *
42
 * Credentials and properties in {@code fromFile()} are pulled from the
43
 * "ads.properties" file. See README for more info.
44
 *
45
 * Tags: MediaService.get
46
 *
47
 * @author Kevin Winter
48
 */
49
public class GetAllImagesAndVideos {
50
 
51
  private static final int PAGE_SIZE = 100;
52
 
53
  public static void main(String[] args) throws Exception {
54
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
55
    // and can be used in place of a service account.
56
    Credential oAuth2Credential = new OfflineCredentials.Builder()
57
        .forApi(Api.ADWORDS)
58
        .fromFile()
59
        .build()
60
        .generateCredential();
61
 
62
    // Construct an AdWordsSession.
63
    AdWordsSession session = new AdWordsSession.Builder()
64
        .fromFile()
65
        .withOAuth2Credential(oAuth2Credential)
66
        .build();
67
 
68
    AdWordsServices adWordsServices = new AdWordsServices();
69
 
70
    runExample(adWordsServices, session);
71
  }
72
 
73
  public static void runExample(
74
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
75
    // Get the MediaService.
76
    MediaServiceInterface mediaService =
77
        adWordsServices.get(session, MediaServiceInterface.class);
78
 
79
    int offset = 0;
80
 
81
    // Create selector.
82
    Selector selector = new Selector();
83
    selector.setFields(new String[] {"MediaId", "Width", "Height", "MimeType"});
84
    selector.setPaging(new Paging(offset, PAGE_SIZE));
85
    selector.setOrdering(new OrderBy[] {new OrderBy("MediaId", SortOrder.ASCENDING)});
86
    selector.setPaging(new Paging(offset, PAGE_SIZE));
87
 
88
    // Create predicates.
89
    Predicate typePredicate =
90
        new Predicate("Type", PredicateOperator.IN, new String[] {"IMAGE", "VIDEO"});
91
    selector.setPredicates(new Predicate[] {typePredicate});
92
 
93
    MediaPage page = null;
94
 
95
    do {
96
      // Get all images.
97
      page = mediaService.get(selector);
98
 
99
      // Display images.
100
      if (page != null && page.getEntries() != null) {
101
        for (Media media : page.getEntries()) {
102
          Map<MediaSize, Dimensions> dimensions = Maps.toMap(media.getDimensions());
103
          System.out.println("Media with id '" + media.getMediaId() +
104
              (!dimensions.isEmpty()
105
                  ? "', dimensions '" + dimensions.get(MediaSize.FULL).getWidth() + "x"
106
                    + dimensions.get(MediaSize.FULL).getHeight()
107
                  : "") + "', and MIME type '"
108
              + media.getMediaType() + "' was found.");
109
        }
110
      } else {
111
        System.out.println("No images/videos were found.");
112
      }
113
      offset += PAGE_SIZE;
114
      selector.getPaging().setStartIndex(offset);
115
    } while (offset < page.getTotalNumEntries());
116
  }
117
}