Created
June 12, 2015 14:17
-
-
Save pathcl/41864cea9ac0eacea1a2 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import csv | |
import sys | |
import os | |
import json | |
import webbrowser | |
import httplib2 | |
from oauth2client.file import Storage | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.tools import run | |
import gdata.docs.service | |
import gdata.spreadsheet.service | |
import gdata.spreadsheet.text_db | |
import datetime | |
import argparse | |
from collections import defaultdict | |
try: | |
import gdata.spreadsheet.service | |
except ImportError, e: | |
errMsg = "please install gdata see http://goo.gl/Eg22wF :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
try: | |
from argparse import ArgumentParser | |
import argparse | |
except ImportError, e: | |
errMsg = "please install argparser see http://goo.gl/qg0KCw :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
try: | |
import csv | |
except ImportError, e: | |
errMsg = "please install csv see http://goo.gl/MQ4DzI :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
if (len(sys.argv) != 2): #very ugly validation | |
errMsg = ("%s %s %s") % ("usage", sys.argv[0], "-csv /tmp/some.csv") | |
print >> sys.stderr, errMsg | |
sys.exit(3) | |
def parseArgs(): | |
thaWord = 'spreadSHIT' | |
parser = ArgumentParser(description=("%s %s") % ('Arguments for this auto filler of google ', thaWord)) | |
parser.add_argument('-e', '--email', | |
type=str, default='sysreporter@domain.tld', | |
help=("%s %s") % ('email that could write on some', thaWord)) | |
parser.add_argument('-p', '--password', | |
type=str, default='somePass', | |
help=("%s %s") % ('email that could write on some', thaWord)) | |
parser.add_argument('-k', '--key', | |
type=str, default='Aasdasdas232341212-MlPCoNAs', | |
help=("%s %s %s") % ('the key of the ', thaWord, 'such as 22ASasd3232HAszZZHwerwerc')) | |
parser.add_argument('-wid', '--worksheet_id', | |
#type=str, default='0', | |
type=str, default='1', | |
help=("%s %s %s") % ('the key of the ', thaWord, 'i mean the key of the id,the first one is od6')) | |
parser.add_argument('-csv','--data_on_csv',type=argparse.FileType('r'), | |
help=("%s %s %s") % ('the file with data to upload on', thaWord, 'such as /tmp/some.csv') | |
) | |
args = parser.parse_args() | |
return args | |
storage = Storage("creds.dat") | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
credentials = run(flow_from_clientsecrets("client_secrets.json", scope=["https://spreadsheets.google.com/feeds"]), storage) | |
def getGdataCredentials(client_secrets="client_secrets.json", storedCreds="creds.dat", scope=["https://spreadsheets.google.com/feeds"], force=False): | |
storage = Storage(storedCreds) | |
credentials = storage.get() | |
if credentials is None or credentials.invalid or force: | |
credentials = run(flow_from_clientsecrets(client_secrets, scope=scope), storage) | |
if credentials.access_token_expired: | |
credentials.refresh(httplib2.Http()) | |
return credentials | |
def getAuthorizedSpreadsheetClient(client_secrets="client_secrets.json", storedCreds="creds.dat", force=False): | |
credentials = getGdataCredentials(client_secrets=client_secrets, storedCreds=storedCreds, scope=["https://spreadsheets.google.com/feeds"], force = force) | |
client = gdata.spreadsheet.service.SpreadsheetsService( | |
additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}) | |
return client | |
def main(args): | |
client = getAuthorizedSpreadsheetClient() | |
client.debug = False | |
csvFh = args.data_on_csv | |
csvReader = csv.reader(csvFh) | |
fullRow = {} | |
rows = [] | |
row = {} | |
#here we put the headers of the columns, they SHOULD BE THE SAME as on the spreadShit | |
headings = ['hypervisor', 'powerstatus', 'vmwaretoolsver', 'vmwaretoolsstat', 'numcpus','memoryinmb', 'disksizesingbs', 'uuid', 'name', 'hostname', 'ip', 'kind', 'completename', 'pool'] | |
#all this tags should have LESS than 15 chars | |
#and be the same on the spreadSHIT and DO NOT call as any reserved word on gdata api, such as "guest" | |
try: | |
for row in csvReader: | |
h = 0 | |
for col in row: | |
index = headings[h] | |
fullRow[index] = col | |
h += 1 | |
rows.append(fullRow) | |
try: | |
client.InsertRow(fullRow, args.key, args.worksheet_id) | |
except Exception as e: | |
print e | |
finally: | |
csvFh.close() | |
return | |
if __name__ == '__main__': | |
args = parseArgs() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment