Skip to content

Instantly share code, notes, and snippets.

@cspickert
Created January 20, 2012 23:40

Revisions

  1. Cameron Spickert revised this gist Jan 25, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions GoogleSpreadsheets.py
    Original file line number Diff line number Diff line change
    @@ -45,11 +45,12 @@ def download(self, spreadsheet, gid=0, format="csv"):
    password = getpass.getpass()
    spreadsheet_id = "" # (spreadsheet id here)

    # Create a client object
    # Create client and spreadsheet objects
    gs = Client(email, password)
    ss = Spreadsheet(spreadsheet_id)

    # Request a file-like object containing the spreadsheet's contents
    csv_file = gs.download(spreadsheet_id)
    csv_file = gs.download(ss)

    # Parse as CSV and print the rows
    for row in csv.reader(csv_file):
  2. Cameron Spickert revised this gist Jan 25, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions GoogleSpreadsheets.py
    Original file line number Diff line number Diff line change
    @@ -46,10 +46,10 @@ def download(self, spreadsheet, gid=0, format="csv"):
    spreadsheet_id = "" # (spreadsheet id here)

    # Create a client object
    gs = GoogleSpreadsheetsClient(email, password)
    gs = Client(email, password)

    # Request a file-like object containing the spreadsheet's contents
    csv_file = gs.get_spreadsheet(spreadsheet_id)
    csv_file = gs.download(spreadsheet_id)

    # Parse as CSV and print the rows
    for row in csv.reader(csv_file):
  3. Cameron Spickert revised this gist Jan 25, 2012. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions GoogleSpreadsheets.py
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,14 @@

    import re, urllib, urllib2

    class GoogleSpreadsheetsClient(object):
    class Spreadsheet(object):
    def __init__(self, key):
    super(Spreadsheet, self).__init__()
    self.key = key

    class Client(object):
    def __init__(self, email, password):
    super(GoogleSpreadsheetsClient, self).__init__()
    super(Client, self).__init__()
    self.email = email
    self.password = password

    @@ -18,18 +23,18 @@ def _get_auth_token(self, email, password, source, service):
    }
    req = urllib2.Request(url, urllib.urlencode(params))
    return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]

    def get_auth_token(self):
    source = type(self).__name__
    return self._get_auth_token(self.email, self.password, source, service="wise")
    def get_spreadsheet(self, spreadsheet_id, gid=0, export_format="csv"):

    def download(self, spreadsheet, gid=0, format="csv"):
    url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
    headers = {
    "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
    "GData-Version": "3.0"
    }
    req = urllib2.Request(url_format % (spreadsheet_id, export_format, gid), headers=headers)
    req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
    return urllib2.urlopen(req)

    if __name__ == "__main__":
  4. Cameron Spickert renamed this gist Jan 21, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Cameron Spickert revised this gist Jan 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GoogleSpreadsheetsClient.py
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ def get_spreadsheet(self, spreadsheet_id, gid=0, export_format="csv"):
    # Create a client object
    gs = GoogleSpreadsheetsClient(email, password)

    # Request a file-like object containing the translation spreadsheet's contents
    # Request a file-like object containing the spreadsheet's contents
    csv_file = gs.get_spreadsheet(spreadsheet_id)

    # Parse as CSV and print the rows
  6. Cameron Spickert created this gist Jan 20, 2012.
    51 changes: 51 additions & 0 deletions GoogleSpreadsheetsClient.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/python

    import re, urllib, urllib2

    class GoogleSpreadsheetsClient(object):
    def __init__(self, email, password):
    super(GoogleSpreadsheetsClient, self).__init__()
    self.email = email
    self.password = password

    def _get_auth_token(self, email, password, source, service):
    url = "https://www.google.com/accounts/ClientLogin"
    params = {
    "Email": email, "Passwd": password,
    "service": service,
    "accountType": "HOSTED_OR_GOOGLE",
    "source": source
    }
    req = urllib2.Request(url, urllib.urlencode(params))
    return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]

    def get_auth_token(self):
    source = type(self).__name__
    return self._get_auth_token(self.email, self.password, source, service="wise")

    def get_spreadsheet(self, spreadsheet_id, gid=0, export_format="csv"):
    url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
    headers = {
    "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
    "GData-Version": "3.0"
    }
    req = urllib2.Request(url_format % (spreadsheet_id, export_format, gid), headers=headers)
    return urllib2.urlopen(req)

    if __name__ == "__main__":
    import getpass
    import csv

    email = "" # (your email here)
    password = getpass.getpass()
    spreadsheet_id = "" # (spreadsheet id here)

    # Create a client object
    gs = GoogleSpreadsheetsClient(email, password)

    # Request a file-like object containing the translation spreadsheet's contents
    csv_file = gs.get_spreadsheet(spreadsheet_id)

    # Parse as CSV and print the rows
    for row in csv.reader(csv_file):
    print ", ".join(row)