-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
45 lines (31 loc) · 1.45 KB
/
Copy pathconfig.py
File metadata and controls
45 lines (31 loc) · 1.45 KB
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
import gspread
import httplib2
import logging
import sys
import os
from dotenv import load_dotenv
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
load_dotenv()
sheet_id = os.getenv("SHEET_ID")
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
class GoogleSheetsApiAuth:
@staticmethod
def get_google_services() -> tuple[gspread.Client, build]:
""" Returns gspread client object and Google Sheets API service using service account credentials """
creds_json = os.path.join(os.path.dirname(__file__), "credentials.json")
scopes = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(creds_json, scopes)
creds_service = ServiceAccountCredentials.from_json_keyfile_name(creds_json, scopes).authorize(httplib2.Http())
service = build('sheets', 'v4', http=creds_service)
client = gspread.authorize(credentials)
return client, service
@staticmethod
def get_sheet(spreadsheet: gspread.Worksheet) -> tuple[list[list[str]], list[str], list[str]]:
""" GET whole cells from table """
data = spreadsheet.get_all_values()
# # GET 'Ref' Row
ref_column = [row[0] for row in data[1:]]
# GET 'Type' Row
type_column = [row[9] for row in data[1:]]
return data, ref_column, type_column