Skip to content
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

Script to download a document as plain text #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions gdoc2latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def fetchGoogleDoc(urlOrGdocFile,email='',password=''):
# find the doc url
if urlOrGdocFile.startswith("https://"):
url = urlOrGdocFile
elif urlOrGdocFile.endswith(".gdoc"):
elif urlOrGdocFile.endswith(".gdoc") or urlOrGdocFile.endswith(".gddoc"):
filename = urlOrGdocFile
f = open(filename, "r")
content = json.load(f)
Expand All @@ -69,7 +69,11 @@ def fetchGoogleDoc(urlOrGdocFile,email='',password=''):
raise Exception(str(urlOrGdocFile) + " not a google doc URL or .gdoc filename")
# pull out the document id
try:
docId = re.search("/document/d/([^/]+)/", url).group(1)
result = re.search("/document/d/([^/]+)/|/open\?id=([^&/]+)", url)
docId = result.group(1) or result.group(2)
# Two possible formats (2017-10-03):
# https://drive.google.com/open?id=idididid
# https://docs.google.com/document/d/idididid/edit?usp=sharing
except Exception:
raise Exception("can't find a google document ID in " + str(urlOrGdocFile))
# construct an export URL
Expand Down
42 changes: 42 additions & 0 deletions gdoc2text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python2.7
# gdoc2latex.py uses Python 2.7 syntax.

# Note that the Google Drive REST API v3 is great for downloading files in a range of formats
# using the HTTP Accept header (e.g. Accept: text/plain).
# https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents

"""
usage:
python gdoc2text.py <URL> [<username>]
python gdoc2text.py <.gdoc or .gddoc filename> [<username>]

example:
python gdoc2text.py https://docs.google.com/document/d/1yEyXxtEeQ5_E7PibjYpofPC6kP4jMG-EieKhwkK7oQE/edit
python gdoc2text.py test.gddoc

example for private documents:
python gdoc2text.py https://docs.google.com/document/d/1yEyXxtEeQ5_E7PibjYpofPC6kP4jMG-EieKhwkK7oQE/edit USERNAME
"""

from gdoc2latex import fetchGoogleDoc, html_to_text
import getpass
import sys

def main():
arg_count = len(sys.argv) - 1
if arg_count == 0 or arg_count > 2:
sys.stderr.write(__doc__)
sys.exit(1)

if arg_count == 1:
html = fetchGoogleDoc(sys.argv[1])
else:
password = getpass.getpass()
html = fetchGoogleDoc(sys.argv[1], sys.argv[2], password)

text = html_to_text(html)
sys.stdout.write(text)


if __name__ == '__main__':
main()