Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright (C) 2011 Google Inc. |
| 5 | |
| 6 | """Sample for retrieving credit-card offers from GAN.""" |
| 7 | |
| 8 | __author__ = 'leadpipe@google.com (Luke Blanshard)' |
| 9 | |
| 10 | import gflags |
| 11 | import httplib2 |
| 12 | import json |
| 13 | import logging |
| 14 | import os |
| 15 | import stat |
| 16 | import sys |
| 17 | |
| 18 | from django.conf import settings |
| 19 | from django.template import Template, Context |
| 20 | from django.template.loader import get_template |
| 21 | |
| 22 | from apiclient.discovery import build |
| 23 | from oauth2client.file import Storage |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 24 | from oauth2client.client import AccessTokenRefreshError |
| 25 | from oauth2client.client import flow_from_clientsecrets |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 26 | from oauth2client.tools import run |
| 27 | |
| 28 | settings.configure(DEBUG=True, TEMPLATE_DEBUG=True, |
| 29 | TEMPLATE_DIRS=('.')) |
| 30 | |
| 31 | |
| 32 | FLAGS = gflags.FLAGS |
| 33 | |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 34 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 35 | # application, including client_id and client_secret, which are found |
| 36 | # on the API Access tab on the Google APIs |
| 37 | # Console <http://code.google.com/apis/console> |
| 38 | CLIENT_SECRETS = '../client_secrets.json' |
| 39 | |
| 40 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 41 | # is missing. |
| 42 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 43 | WARNING: Please configure OAuth 2.0 |
| 44 | |
| 45 | To make this sample run you will need to populate the client_secrets.json file |
| 46 | found at: |
| 47 | |
| 48 | %s |
| 49 | |
| 50 | with information from the APIs Console <https://code.google.com/apis/console>. |
| 51 | |
| 52 | """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) |
| 53 | |
| 54 | # Set up a Flow object to be used if we need to authenticate. |
| 55 | FLOW = flow_from_clientsecrets(CLIENT_SECRETS, |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 56 | scope='https://www.googleapis.com/auth/gan.readonly', |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 57 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 58 | |
| 59 | # The gflags module makes defining command-line options easy for |
| 60 | # applications. Run this program with the '--help' argument to see |
| 61 | # all the flags that it understands. |
| 62 | gflags.DEFINE_enum('logging_level', 'DEBUG', |
| 63 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 64 | 'Set the level of logging detail.') |
| 65 | |
| 66 | gflags.DEFINE_enum("output_type", 'STDOUT', ['BOTH', 'HTML', 'STDOUT'], |
| 67 | 'Set how to output the results received from the API') |
| 68 | |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 69 | gflags.DEFINE_string('credentials_filename', '../credentials.dat', |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 70 | 'File to store credentials in', short_name='cf') |
| 71 | |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 72 | gflags.DEFINE_multistring('advertiser', [], |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 73 | 'If given, advertiser we should run as') |
| 74 | |
| 75 | |
| 76 | def usage(argv): |
| 77 | print 'Usage: %s <publisher-id>\n%s' % (argv[0], FLAGS) |
| 78 | sys.exit(1) |
| 79 | |
| 80 | |
| 81 | def main(argv): |
| 82 | # Let the gflags module process the command-line arguments |
| 83 | try: |
| 84 | argv = FLAGS(argv) |
| 85 | except gflags.FlagsError, e: |
| 86 | raise e |
| 87 | usage(argv) |
| 88 | |
| 89 | if len(argv) != 2: |
| 90 | usage(argv) |
| 91 | publisher = argv[1] |
| 92 | |
| 93 | # Set the logging according to the command-line flag |
| 94 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 95 | |
| 96 | # If the Credentials don't exist or are invalid run through the native client |
| 97 | # flow. The Storage object will ensure that if successful the good |
| 98 | # Credentials will get written back to a file. |
| 99 | storage = Storage(FLAGS.credentials_filename) |
| 100 | credentials = storage.get() |
| 101 | if credentials is None or credentials.invalid: |
| 102 | credentials = run(FLOW, storage) |
| 103 | |
| 104 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 105 | # with our good Credentials. |
| 106 | http = httplib2.Http() |
| 107 | http = credentials.authorize(http) |
| 108 | |
| 109 | service = build('gan', 'v1beta1', http=http) |
| 110 | |
| 111 | ccOffers = service.ccOffers() |
| 112 | |
| 113 | # Retrieve the relevant offers. |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 114 | request = ccOffers.list(publisher=publisher, |
| 115 | advertiser=FLAGS.advertiser, |
| 116 | projection='full') |
| 117 | response = request.execute() |
| 118 | response['publisher'] = publisher |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 119 | |
| 120 | if FLAGS.output_type in ["BOTH", "HTML"]: |
| 121 | template = get_template('offers_template.html') |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 122 | context = Context(response) |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 123 | |
| 124 | fname = '%s.html' % publisher |
| 125 | out = open(fname, 'w') |
| 126 | out.write(template.render(context).encode('UTF-8')) |
| 127 | os.fchmod(out.fileno(), stat.S_IROTH|stat.S_IRGRP|stat.S_IRUSR|stat.S_IWUSR) |
| 128 | out.close() |
| 129 | |
| 130 | print 'Wrote %s' % fname |
| 131 | |
| 132 | if FLAGS.output_type in ["BOTH", "STDOUT"]: |
leadpipe@wpgntav-ubiq70.hot.corp.google.com | bba4898 | 2011-11-02 16:27:24 -0500 | [diff] [blame] | 133 | print json.dumps(response, sort_keys=True, indent=4) |
Joe Gregorio | 093d9bf | 2011-09-08 16:09:55 -0400 | [diff] [blame] | 134 | |
| 135 | if __name__ == '__main__': |
| 136 | main(sys.argv) |