drive

google.apis.drive.DRIVE_BACKUP_FILE = '/home/docs/checkouts/readthedocs.org/user_builds/dbling/checkouts/latest/google/apis/../drive_data_backup.pkl'

Location of pickled data when cached.

google.apis.drive.SEGMENT_SIZE = 4

Number of hours in a segment. Must be equally divisible by 24 to avoid issues.

class google.apis.drive.DriveAPI(http=None, impersonated_user_email=None, start=None, end=None, timezone=None)[source]

Class to interact with Google Drive APIs.

Documentation for the Python API:

Quick start guide:

Parameters:
  • http (httplib2.Http) – An Http object for sending the requests. In general, this should be left as None, which will allow for auto-adjustment of the kind of Http object to create based on whether a user’s email address is to be impersonated.
  • impersonated_user_email (str) – The email address of a user to impersonate. This requires domain-wide delegation to be activated. See https://developers.google.com/admin-sdk/reports/v1/guides/delegation for instructions.
  • start (str) – The earliest data to collect. Can be any kind of date string, as long as it is unambiguous (e.g. “2017”). It can even be slang, such as “a year ago”. Be aware, however, that only the day of the date will be used, meaning time information will be discarded.
  • end (str) – The latest data to collect. Same format rules apply for this as for the start parameter.
  • timezone (str) – The timezone to convert all timestamps to before compiling. This should be a standard timezone name. For reference, the list that the timezone will be compared against is available at https://github.com/newvem/pytz/blob/master/pytz/__init__.py. If omitted, the local timezone of the computer will be used.
activity(level, what=('files', 'revisions'), use_cached=False, **kwargs)[source]

Compile the user’s activity.

Note about revision history: One of the metadata fields for file revisions is called “keepForever”. This indicates whether to keep the revision forever, even if it is no longer the head revision. If not set, the revision will be automatically purged 30 days after newer content is uploaded. This can be set on a maximum of 200 revisions for a file.

Parameters:
  • level (str) –

    Level of detail on the activity. Accepted values:

    • 'dy': Activity is summarized by day
    • 'hr': Activity is summarized by hour, X:00:00 to X:59:59
    • 'sg': Activity throughout the day is divided into a number of segments (defined to be SEGMENT_SIZE divided by 24).
  • what (tuple or list) –

    Indicates what kind of content to scan for activity. Accepted values:

    • 'created'
    • 'revisions'
    • 'comments'
  • use_cached (bool) – Whether or not to use cached data. When set, this avoids downloading all the file metadata from Google if a cached version of the data is available on disk.
Returns:

A dictionary containing three keys: x, y, and z. Each of these stores a list suitable for passing as the data set for a plot.

Return type:

dict(str, list)

Raises:

ValueError – When the level or what parameters have an unsupported format or value.

get_about(fields='*')[source]

Retrieves information about the user’s Drive. and system capabilities.

https://developers.google.com/drive/v3/reference/about

Parameters:fields (string) – fields to be returned
Returns:JSON
team_drives

A list of team drives associated with the user.

Return type:list(str)
get_changes(spaces='drive', include_team_drives=True, restrict_to_my_drive=False, include_corpus_removals=None, include_removed=None)[source]

Return the changes for a Google Drive account.

The set of changes as returned by this method are more suited for a file syncing application.

In the returned dict, the key for changes in the user’s regular Drive is an empty string (''). The data for each Team Drive (assuming include_team_drives is True) is stored using a key in the format 'team_drive_X', where X is the ID of the Team Drive. For the form of the JSON data, go to https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.teamdrives.html#list

https://developers.google.com/drive/v3/reference/changes

Parameters:
  • spaces (str) – A comma-separated list of spaces to query within the user corpus. Supported values are ‘drive’, ‘appDataFolder’ and ‘photos’.
  • include_team_drives (bool) – Whether or not to include data from Team Drives as well as the user’s Drive.
  • restrict_to_my_drive (bool) – Whether to restrict the results to changes inside the My Drive hierarchy. This omits changes to files such as those in the Application Data folder or shared files which have not been added to My Drive.
  • include_corpus_removals (bool) – Whether changes should include the file resource if the file is still accessible by the user at the time of the request, even when a file was removed from the list of changes and there will be no further change entries for this file.
  • include_removed (bool) – Whether to include changes indicating that items have been removed from the list of changes, for example by deletion or loss of access.
Returns:

All data on changes by the user in JSON format and stored in a dict.

Return type:

dict(str, dict)

gen_file_data(fields='*', spaces='drive', include_team_drives=True, corpora=None)[source]

Generate the metadata for the user’s Drive files.

This function is a generator, so it yields the metadata for one file at a time. For the format of the dict generated, see https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html#list

Parameters:
  • fields (str) – The metadata fields to retrieve.
  • spaces (str) – A comma-separated list of spaces to query within the user corpus. Supported values are ‘drive’, ‘appDataFolder’ and ‘photos’.
  • include_team_drives (bool) – Whether or not to include data from Team Drives as well as the user’s Drive.
  • corpora (str) – Comma-separated list of bodies of items (files/documents) to which the query applies. Supported bodies are ‘user’, ‘domain’, ‘teamDrive’ and ‘allTeamDrives’. ‘allTeamDrives’ must be combined with ‘user’; all other values must be used in isolation. Prefer ‘user’ or ‘teamDrive’ to ‘allTeamDrives’ for efficiency.
Returns:

The file metadata.

Return type:

dict

export_drive_file(file_data, download_path)[source]

Exports and converts .g* files to real files and then downloads them

https://developers.google.com/drive/v3/reference/files/export

Parameters:
  • file_data (JSON) – List of file(s) to be downloaded
  • download_path – Path where the file will be downloaded
Returns:

boolean True if downloads succeeded, False if Downloads failed.

export_real_file(file_data, download_path)[source]

Downloads real files. AKA not .g*

https://developers.google.com/drive/v3/reference/files/export

Parameters:
  • file_data (JSON) – List of file(s) to be downloaded
  • download_path – Path where the file will be downloaded
Returns:

Nothing

download_files(file_list_array=False)[source]

Downloads files from the user’s drive

https://developers.google.com/drive/v3/web/manage-downloads

Parameters:file_list_array (array) – list of file(s) to be downloaded
Returns:Nothing
get_app_folder(fields='nextPageToken, files(id, name)')[source]

Returns the data in the users app data folder

https://developers.google.com/drive/v3/reference/files/list

Parameters:fields (string) – fields to be returned
Returns:JSON
get_photo_data(fields='nextPageToken, files(id, name)')[source]

Returns the data about the user’s photos

https://developers.google.com/drive/v3/reference/files/list

Parameters:fields (string) – fields to be returned
Returns:JSON
google.apis.drive.crunch(level, **kwargs)[source]

Consolidate the data to the specified level.

Parameters:
  • data (CalDict) – The data from parsing the Drive metadata.
  • level (str) – Must be one of dy, sg, or hr. For an explanation of these options, see the docstring for DriveAPI.activity().
  • start (datetime.date) – The earliest data to collect.
  • end (datetime.date) – The latest data to collect.
Returns:

Tuple with two elements. The first is a DateRange object which stores the first and last days with activity (the range of dates that the data corresponds to) in its start and end attributes, respectively. Both of these attributes are date objects.

The second element in the returned tuple is a list containing the data for each day. The contents of this list vary based on the value of level:

  • dy: A single list of int s, one for each day.
  • sg: list s of int s. Each list corresponds to a segment, each int corresponds to a day. These lists are in reverse order, meaning the first list represents the last segment of a day.
  • hr: list s of int s. Each list corresponds to an hour, each int corresponds to a day. These lists are in reverse order, meaning the first list represents the last hour of a day.

Return type:

tuple(DateRange, list(list(int)))