-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Slides & Google Drive #1031
Open
elyse-weiss
wants to merge
14
commits into
move-coop:main
Choose a base branch
from
elyse-weiss:google-slides
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+604
−4
Open
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5066e21
initial commit
baadc24
working code
8e1de17
tests and more
587b90f
updates
1fa27f0
remove bin
30a4f9a
shauna comments
a001133
docs
3c81c14
title
4dd1053
api
61014fc
goodbye flake8
a993d58
Merge branch 'main' into google-slides
shaunagm c93dbb7
Merge branch 'main' into google-slides
elyse-weiss 68c3685
getting tests to work
e663a1e
Merge branch 'main' into google-slides
elyse-weiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
initial commit
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import os | ||
import json | ||
import logging | ||
|
||
from parsons.etl.table import Table | ||
from parsons.google.utitities import setup_google_application_credentials | ||
from parsons.tools.credential_tools import decode_credential | ||
from google.oauth2 import service_account | ||
from googleapiclient.discovery import build | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class GoogleSlides: | ||
""" | ||
A connector for Google Slides, handling slide creation. | ||
`Args:` | ||
google_keyfile_dict: dict | ||
A dictionary of Google Drive API credentials, parsed from JSON provided | ||
by the Google Developer Console. Required if env variable | ||
``GOOGLE_DRIVE_CREDENTIALS`` is not populated. | ||
subject: string | ||
In order to use account impersonation, pass in the email address of the account to be | ||
impersonated as a string. | ||
""" | ||
|
||
def __init__(self, google_keyfile_dict=None, subject=None): | ||
|
||
scope = [ | ||
"https://www.googleapis.com/auth/presentations", | ||
"https://www.googleapis.com/auth/drive", | ||
] | ||
|
||
setup_google_application_credentials( | ||
google_keyfile_dict, "GOOGLE_DRIVE_CREDENTIALS" | ||
) | ||
google_credential_file = open(os.environ["GOOGLE_DRIVE_CREDENTIALS"]) | ||
credentials_dict = json.load(google_credential_file) | ||
|
||
credentials = Credentials.from_service_account_info( | ||
credentials_dict, scopes=scope, subject=subject | ||
) | ||
|
||
self.gsheets_client = build('slides', 'v1', credentials=credentials) | ||
|
||
|
||
def create_presentation(self, title): | ||
""" | ||
`Args:` | ||
title: str | ||
this is the title you'd like to give your presentation | ||
`Returns:` | ||
the presentation object | ||
""" | ||
|
||
body = {"title": title} | ||
presentation = self.presentations().create(body=body).execute() | ||
logger.info( | ||
f"Created presentation with ID:" f"{(presentation.get('presentationId'))}" | ||
) | ||
return presentation | ||
|
||
|
||
def get_presentation(self, presentation_id): | ||
""" | ||
`Args:` | ||
presentation_id: str | ||
this is the ID of the presentation to put the duplicated slide | ||
`Returns:` | ||
the presentation object | ||
""" | ||
|
||
presentation = ( | ||
self.presentations().get(presentationId=presentation_id).execute() | ||
) | ||
|
||
return presentation | ||
|
||
|
||
def get_slide(self, presentation_id, slide_number): | ||
""" | ||
`Args:` | ||
presentation_id: str | ||
this is the ID of the presentation to put the duplicated slide | ||
slide_number: int | ||
the slide number you are seeking | ||
`Returns:` | ||
the slide object | ||
""" | ||
|
||
presentation = self.get_presentation(self, presentation_id) | ||
slide = presentation["slides"][slide_number - 1] | ||
|
||
return slide | ||
|
||
|
||
def duplicate_slide(self, source_slide_id, presentation_id): | ||
""" | ||
`Args:` | ||
source_slide_id: str | ||
this is the ID of the source slide to be duplicated | ||
presentation_id: str | ||
this is the ID of the presentation to put the duplicated slide | ||
`Returns:` | ||
the ID of the duplicated slide | ||
""" | ||
|
||
batch_request = {"requests": [{"duplicateObject": {"objectId": source_slide_id}}]} | ||
response = ( | ||
self.presentations() | ||
.batchUpdate(presentationId=presentation_id, body=batch_request) | ||
.execute() | ||
) | ||
|
||
duplicated_slide_id = response["replies"][0]["duplicateObject"]["objectId"] | ||
|
||
return duplicated_slide_id | ||
|
||
|
||
def replace_slide_text( | ||
self, presentation_id, slide_id, original_text, replace_text | ||
): | ||
""" | ||
`Args:` | ||
presentation_id: str | ||
this is the ID of the presentation to put the duplicated slide | ||
origianl_text: str | ||
the text to be replaced | ||
replace_text: str | ||
the desired new text | ||
`Returns:` | ||
None | ||
""" | ||
|
||
reqs = [ | ||
{ | ||
"replaceAllText": { | ||
"containsText": {"text": original_text}, | ||
"replaceText": replace_text, | ||
"pageObjectIds": [slide_id], | ||
} | ||
}, | ||
]s | ||
|
||
self.presentations().batchUpdate( | ||
body={"requests": reqs}, presentationId=presentation_id | ||
).execute() | ||
|
||
return None | ||
|
||
|
||
def replace_slide_image(self, presentation_id, slide, obj, img_url): | ||
""" | ||
`Args:` | ||
presentation_id: str | ||
this is the ID of the presentation to put the duplicated slide | ||
slide: dict | ||
the slide object | ||
replace_text: str | ||
the desired new text | ||
`Returns:` | ||
None | ||
""" | ||
|
||
reqs = [ | ||
{ | ||
"createImage": { | ||
"url": img_url, | ||
"elementProperties": { | ||
"pageObjectId": slide["objectId"], | ||
"size": obj["size"], | ||
"transform": obj["transform"], | ||
}, | ||
} | ||
}, | ||
{"deleteObject": {"objectId": obj["objectId"]}}, | ||
] | ||
|
||
self.presentations().batchUpdate( | ||
body={"requests": reqs}, presentationId=presentation_id | ||
).execute() | ||
|
||
return None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments on google_drive apply to this connector