Cleaned up ZillowGrabber code, removed Test and Credentials

This commit is contained in:
tsb1995 2020-06-07 10:40:41 -07:00
parent 48b00bf284
commit d3dd81ab00
2 changed files with 49 additions and 52 deletions

View File

@ -76,16 +76,16 @@ range_ = "Sheet1!A:A"
d = datetime.datetime.today() d = datetime.datetime.today()
sheet_name = 'Housing ' + d.strftime('%d-%m-%Y') sheet_name = 'Housing ' + d.strftime('%d-%m-%Y')
def main(): creds = None
creds = None # The file token.pickle stores the user's access and refresh tokens, and is
# The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first
# created automatically when the authorization flow completes for the first # time.
# time. if os.path.exists('token.pickle'):
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token: with open('token.pickle', 'rb') as token:
creds = pickle.load(token) creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid: # If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token: if creds and creds.expired and creds.refresh_token:
creds.refresh(Request()) creds.refresh(Request())
else: else:
@ -96,44 +96,42 @@ def main():
with open('token.pickle', 'wb') as token: with open('token.pickle', 'wb') as token:
pickle.dump(creds, token) pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds) service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API # Call the Sheets API
sheet = service.spreadsheets() sheet = service.spreadsheets()
# Create new Sheet # Create new Sheet
spreadsheet = { spreadsheet = {
'properties': { 'properties': {
'title': sheet_name 'title': sheet_name
} }
} }
spreadsheet = service.spreadsheets().create(body=spreadsheet, spreadsheet = service.spreadsheets().create(body=spreadsheet,
fields='spreadsheetId').execute() fields='spreadsheetId').execute()
spreadsheet_id = spreadsheet.get('spreadsheetId') spreadsheet_id = spreadsheet.get('spreadsheetId')
print('Spreadsheet ID: {0}'.format(spreadsheet.get('spreadsheetId'))) print('Spreadsheet ID: {0}'.format(spreadsheet.get('spreadsheetId')))
# Fill in header # Fill in header
list = [df.columns.tolist()] list = [df.columns.tolist()]
resource = { resource = {
"majorDimension": "ROWS", "majorDimension": "ROWS",
"values": list "values": list
} }
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, body=resource, request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, body=resource,
valueInputOption="USER_ENTERED") valueInputOption="USER_ENTERED")
response = request.execute() response = request.execute()
# Fill in body # Fill in body
list = df.values.tolist() list = df.values.tolist()
resource = { resource = {
"majorDimension": "ROWS", "majorDimension": "ROWS",
"values": list "values": list
} }
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, body=resource, request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, body=resource,
valueInputOption="USER_ENTERED") valueInputOption="USER_ENTERED")
response = request.execute() response = request.execute()
main()

View File

@ -1 +0,0 @@
{"installed":{"client_id":"495919805073-o2o41tu9c9pv9mpo4ugjdcca6h5jg5u7.apps.googleusercontent.com","project_id":"zillowfun-1581659411719","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"HL2xQvsWITylqar1GsC8noW1","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}