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 |
| 24 | from oauth2client.client import OAuth2WebServerFlow |
| 25 | from oauth2client.tools import run |
| 26 | |
| 27 | settings.configure(DEBUG=True, TEMPLATE_DEBUG=True, |
| 28 | TEMPLATE_DIRS=('.')) |
| 29 | |
| 30 | |
| 31 | FLAGS = gflags.FLAGS |
| 32 | |
| 33 | # Set up a Flow object to be used if we need to authenticate. This |
| 34 | # sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with |
| 35 | # the information it needs to authenticate. Note that it is called |
| 36 | # the Web Server Flow, but it can also handle the flow for native |
| 37 | # applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA> |
| 38 | # The client_id client_secret are copied from the API Access tab on |
| 39 | # the Google APIs Console <http://code.google.com/apis/console>. When |
| 40 | # creating credentials for this application be sure to choose an Application |
| 41 | # type of "Installed application". |
| 42 | FLOW = OAuth2WebServerFlow( |
| 43 | client_id='767567128246-ti2q06i1neqm5boe2m1pqdc2riivhk41.apps.googleusercontent.com', |
| 44 | client_secret='UtdXI8nKD2SEcQRLQDZPkGT9', |
| 45 | scope='https://www.googleapis.com/auth/gan.readonly', |
| 46 | user_agent='gan-ccoffers-sample/1.0') |
| 47 | |
| 48 | # The gflags module makes defining command-line options easy for |
| 49 | # applications. Run this program with the '--help' argument to see |
| 50 | # all the flags that it understands. |
| 51 | gflags.DEFINE_enum('logging_level', 'DEBUG', |
| 52 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 53 | 'Set the level of logging detail.') |
| 54 | |
| 55 | gflags.DEFINE_enum("output_type", 'STDOUT', ['BOTH', 'HTML', 'STDOUT'], |
| 56 | 'Set how to output the results received from the API') |
| 57 | |
| 58 | gflags.DEFINE_string('credentials_filename', 'offers.dat', |
| 59 | 'File to store credentials in', short_name='cf') |
| 60 | |
| 61 | gflags.DEFINE_multistring('advertiser', None, |
| 62 | 'If given, advertiser we should run as') |
| 63 | |
| 64 | |
| 65 | def usage(argv): |
| 66 | print 'Usage: %s <publisher-id>\n%s' % (argv[0], FLAGS) |
| 67 | sys.exit(1) |
| 68 | |
| 69 | |
| 70 | def main(argv): |
| 71 | # Let the gflags module process the command-line arguments |
| 72 | try: |
| 73 | argv = FLAGS(argv) |
| 74 | except gflags.FlagsError, e: |
| 75 | raise e |
| 76 | usage(argv) |
| 77 | |
| 78 | if len(argv) != 2: |
| 79 | usage(argv) |
| 80 | publisher = argv[1] |
| 81 | |
| 82 | # Set the logging according to the command-line flag |
| 83 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 84 | |
| 85 | # If the Credentials don't exist or are invalid run through the native client |
| 86 | # flow. The Storage object will ensure that if successful the good |
| 87 | # Credentials will get written back to a file. |
| 88 | storage = Storage(FLAGS.credentials_filename) |
| 89 | credentials = storage.get() |
| 90 | if credentials is None or credentials.invalid: |
| 91 | credentials = run(FLOW, storage) |
| 92 | |
| 93 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 94 | # with our good Credentials. |
| 95 | http = httplib2.Http() |
| 96 | http = credentials.authorize(http) |
| 97 | |
| 98 | service = build('gan', 'v1beta1', http=http) |
| 99 | |
| 100 | ccOffers = service.ccOffers() |
| 101 | |
| 102 | # Retrieve the relevant offers. |
| 103 | list_call = ccOffers.list(publisher=publisher, |
| 104 | # TODO(leadpipe): add back when advertiser is repeated |
| 105 | # advertiser=FLAGS.advertiser, |
| 106 | projection='full') |
| 107 | list = list_call.execute() |
| 108 | list['publisher'] = publisher |
| 109 | |
| 110 | if FLAGS.output_type in ["BOTH", "HTML"]: |
| 111 | template = get_template('offers_template.html') |
| 112 | context = Context(list) |
| 113 | |
| 114 | fname = '%s.html' % publisher |
| 115 | out = open(fname, 'w') |
| 116 | out.write(template.render(context).encode('UTF-8')) |
| 117 | os.fchmod(out.fileno(), stat.S_IROTH|stat.S_IRGRP|stat.S_IRUSR|stat.S_IWUSR) |
| 118 | out.close() |
| 119 | |
| 120 | print 'Wrote %s' % fname |
| 121 | |
| 122 | if FLAGS.output_type in ["BOTH", "STDOUT"]: |
| 123 | print json.dumps(list, sort_keys=True, indent=4) |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main(sys.argv) |