forked from guylifshitz/dj-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjspreadsheet.py
46 lines (36 loc) · 1.58 KB
/
djspreadsheet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow, Flow
from google.auth.transport.requests import Request
import os
import pickle
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# here enter the id of your google sheet
SAMPLE_SPREADSHEET_ID_input = '1FYdD43eZ8xzKlreqK8tygNljxglzlbg5Zp2imvbvtd0'
SAMPLE_RANGE_NAME = 'A1:AA1000'
def get_data():
global values_input, service
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret_722838719056-j20ouvfmh6ojareojo0s9jd04agnql16.apps.googleusercontent.com.json', SCOPES) # here enter the name of your downloaded JSON file
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result_input = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID_input,
range=SAMPLE_RANGE_NAME).execute()
values_input = result_input.get('values', [])
if not values_input and not values_expansion:
print('No data found.')
df = pd.DataFrame(values_input[1:], columns=values_input[0])
return df
get_data()