PrmScrp / sgoogle.py
H4CK3R-5M4CK3R
updated everything
03757f2
import gspread
from google.oauth2 import service_account
from googleapiclient.discovery import build
import time
class GoogleSheetAutomator:
def __init__(self, name: list, folder_id: str, creds_dict: dict, outputfile: str):
self.columns = name
self.folder_id = folder_id
scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive'
]
creds = service_account.Credentials.from_service_account_info(creds_dict, scopes=scope)
self.client = gspread.authorize(creds)
self.worksheet_main = self.client.create(outputfile, folder_id)
self.worksheet = self.worksheet_main.sheet1
self.worksheet.append_row(self.columns)
def save(self, data_dict):
"""
Save a new row of data to the Google Sheet.
:param data_dict: Dictionary with keys as column names and values as the data to save.
"""
row_data = [data_dict.get(column, '') for column in self.columns]
self.worksheet.append_row(row_data)
time.sleep(2)
def save_to_file(self):
"""
Since the data is saved in real-time to Google Sheets,
this method can simply return the sheet URL.
"""
return self.worksheet_main.url
def transfer_ownership(self, mail: str):
resp = self.worksheet_main.share(mail, perm_type='user', role='writer', notify=True)
self.worksheet_main.transfer_ownership(resp.json()["id"])
return True