apis

google.apis.get_api(api, **kwargs)[source]

Shortcut for creating an API object.

Parameters:
  • api (str) –

    Name of the API to instantiate. Acceptable values are:

    • 'drive'
    • 'plus'
    • 'people'
    • 'dir'
    • 'gmail'
    • 'reports'
  • kwargs (dict) – Set of keyword arguments to pass to the object’s constructor.
Returns:

An instance of the created object.

Return type:

DriveAPI or PlusAPI or PeopleAPI or DirectoryAPI or GmailAPI or ReportsAPI

Other Functions and Classes

The following functions and classes are helpers to the code documented elsewhere.

util

Google API Client Library Page https://developers.google.com/api-client-library/python/reference/pydoc Python Quick Start Page https://developers.google.com/drive/v3/web/quickstart/python

exception google.util.InvalidCredsError[source]

Raised when HTTP credentials don’t work.

class google.util.CalDict[source]

A dict-like class for storing hourly data for a year.

This is intended to have a set of keys that correspond to years. Since Python’s syntax dictates that objects cannot have attributes with names consisting only of numbers (e.g. cal.2017), one solution would be to name the year keys cal.y2017, cal.y2016, etc. This is the intended convention for CalDict objects and aligns with how month and day data is named.

Once you have created an instance of CalDict, you can easily create the structures necessary to store a year’s worth of data like so:

>>> cal = CalDict()
>>> cal[2017]

Just accessing the 2017 key (which is an int) assigns its value to be a dict with 12 keys, one for each month, numbered 1 through 12. Each of those keys points to a dict object with 31 keys, numbered 1 through 31. The day keys point to a list of 24 integers, initialized to 0. This allows you to increment the value for a particular hour immediately after instantiation, like the following, which increments the counter for the 2 PM hour block on August 31, 2016:

>>> cal2 = CalDict()
>>> y, m, d = 2016, 8, 31
>>> cal2[y][m][d][14] += 1

Since all months in a CalDict instance have 31 days, I recommend you use an external method of validating a particular date before storing or retrieving data.

google.util.get_credentials(scope=None, application_name=None, secret=None, credential_file=None)[source]

Create the credential file for accessing the Google APIs.

https://developers.google.com/drive/v3/web/quickstart/python

Parameters:
  • scope (str) – String of Scopes separated by spaces to give access to different Google APIs. Defaults to SCOPES.
  • application_name (str) – Name of this Application. Defaults to APPLICATION_NAME.
  • secret (str) – The secret file given from Google. Should be named client_secret.json. Defaults to CLIENT_SECRET_FILE.
  • credential_file (str) – Name of the credential file to be created. Defaults to CREDENTIAL_FILE.
Returns:

Credential object.

Raises:

InvalidCredsError – if the credential file is missing or invalid.

google.util.set_http(impersonated_user_email=None)[source]

Create and return the Http object used to communicate with Google.

https://developers.google.com/drive/v3/web/quickstart/python

Parameters:impersonated_user_email (str) – Email address of the User to be impersonated. This uses domain wide delegation to do the impersonation.
Returns:The Http object.
Return type:httplib2.Http
Raises:InvalidCredsError – if the credential file is missing or invalid.
google.util.print_json(obj, sort=False, indent=2)[source]

Print the JSON object in a human readable format.

Parameters:
  • obj (dict or list) – JSON-serializable object.
  • sort (bool) – Whether to sort the keys before printing.
  • indent (int) – Number of spaces to indent.
Return type:

None

google.util.convert_mime_type_and_extension(google_mime_type)[source]

Return the conversion type and extension for the given Google MIME type.

Converts mimeType given from google to one of our choosing for export conversion This is necessary to download .g* files.

Information on MIME types:

Parameters:google_mime_type (str) – mimeType given from Google API
Returns:Tuple in the form (conversion type, extension). If no supported conversion is supported for the given MIME type, the tuple will be (False, False).
Return type:tuple

const

google.const.CLIENT_SECRET_FILE = 'client_secret.json'

This file is obtained from Google through the API pages. Guide posted below

Look for the “Create authorization credentials” subsection

google.const.CREDENTIAL_FILE = 'test_creds.json'

Name of the file that is made by get_credentials

google.const.APPLICATION_NAME = 'dbling'

Name of the application

google.const.DOWNLOAD_DIRECTORY = None

Optional, if set to a path the user’s drive files will be downloaded to that location

google.const.PAGE_SIZE = 1000

Page size for requests. Specifies the number of records to be returned in a single reply. The accepted range for most requests is [1, 1000].

google.const.SCOPES = 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.appfolder https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/contacts.readonly https://www.googleapis.com/auth/admin.directory.device.chromeos https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.device.mobile.readonly https://www.googleapis.com/auth/admin.directory.customer.readonly https://www.googleapis.com/auth/admin.reports.audit.readonly https://www.googleapis.com/auth/admin.reports.usage.readonly'

Scope: https://developers.google.com/drive/v3/web/about-auth

plot

google.plot.HEATMAP_COLORS = ('#e7f0fa', '#c9e2f6', '#95cbee', '#0099dc', '#4ab04a', '#ffd73e', '#eec73a', '#e29421', '#e29421', '#f05336', '#ce472e')

Color scale used by the heat map

google.plot.STOP_FACTOR = 80

Changes how quickly the colors go to the maximum

google.plot.stop(i)[source]

Return the i th color stop.

In color gradients, the point where a defined color is (as opposed to in between the defined colors, where the colors are “graded”) is called a “stop”. This Python function defines an exponential math function that returns floating point values, in the range of 0 to 1, that define where the gradient stops should occur. In the heatmap() function, these values will be used to determine the color of each cell based on the normalized values of the z parameters.

The number of stops is determined by the number of colors defined in HEATMAP_COLORS. The math function used is below. In it, m = STOP_FACTOR and n = len(HEATMAP_COLORS).

\[\frac{m^{(i/(n - 1))} - 1}{m - 1}\]
Parameters:i (int) – The current stop number. Must be a value between 0 and len(HEATMAP_COLORS) - 1, i.e. [0, n).
Returns:Where the i th color stop should occur. Will always be a value between 0 and 1.
Return type:float
Raises:ValueError – When i isn’t between 0 and len(HEATMAP_COLORS) -1.
google.plot.heatmap(x, y, z, title='')[source]

Create and return a heat map figure object for the given data.

Parameters:
  • x (list or tuple) – Data for the x-axis.
  • y (list or tuple) – Data for the y-axis.
  • z (list or tuple) – Data for the z-axis.
  • title (str) – A title for the figure.
Returns:

The object with the data’s graph. With this object you can then call its iplot() method to show the graph.

Return type:

plotly.graph_objs.Figure