Skip to content

Instantly share code, notes, and snippets.

@wdecoster
Created July 8, 2018 14:50
Show Gist options
  • Select an option

  • Save wdecoster/9984ce30b21c7c5d751458672c724310 to your computer and use it in GitHub Desktop.

Select an option

Save wdecoster/9984ce30b21c7c5d751458672c724310 to your computer and use it in GitHub Desktop.
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from time import time
from datetime import datetime
class Document(object):
def __init__(self, title, id):
"""Title is free, id is google docs identifier"""
self.title = title
self.id = id
def char_count(self, service):
data = service.files().export(fileId=self.id, mimeType='text/plain').execute()
if data:
decoded = data.decode('utf-8')
return len([i for i in decoded if i not in ['\n', '\r', '_']])
else:
return False
def main():
service = setup_api(version='v3')
documents = [
Document(title="Paper1", id="ID1"),
Document(title="Paper2", id="ID2"),
Document(title="Paper3", id="ID3"),
Document(title="Paper4", id="ID4"),
]
for paper in documents:
wc = paper.char_count(service)
if wc:
now = datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S')
print(f"{paper.title}\t{now}\t{wc}")
def setup_api(version='v2'):
"""
Load api, or open browser for authorization
If you adapt the scopes you have to delete the credentials.json file
to enable reauthorization
"""
SCOPES = (
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets.readonly',
)
store = file.Storage('credentials.json')
client_secret = 'client_secret.json'
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets(client_secret, SCOPES)
creds = tools.run_flow(flow, store)
return build('drive', version, http=creds.authorize(Http()))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment