/
sheets.py
139 lines (119 loc) · 4.37 KB
/
sheets.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import datetime
import os
import asyncio
from aiogoogle import Aiogoogle
# from googleapiclient.discovery import build
from rich import print
class SpreadSheet:
def __init__(self, credentials):
self.creds = credentials
# self.sheets = build('sheets', 'v4', credentials=self.creds)
self.sheet_range = 'Sheet1!A:C'
self.spreadsheet_url = None
async def _open_sheet(self, sheet_id):
'''Private method
Opens an existing spreadsheet.
'''
ss_id = sheet_id
print("Spreadsheet Found")
self.spreadsheet_id = ss_id
async with Aiogoogle(service_account_creds=self.creds) as aiogoogle:
sheet = await aiogoogle.discover('sheets', 'v4')
# print(dir(sheet))
res = await aiogoogle.as_service_account(
sheet.spreadsheets.values.get(
spreadsheetId=self.spreadsheet_id,
range=self.sheet_range,
)
)
print(dir(res))
# self.spreadsheet_url = sheet.get(spreadsheetId=ss_id).execute()
# result = sheet.values().get(spreadsheetId=ss_id, range=self.sheet_range).execute()
# values = result.get('values', [])
# if not values:
# print('Sheet Empty')
# return
# print('From\tSubject')
# for row in values:
# # if 'no-reply' in row[0].lower():
# # pass
# # elif 'noreply' in row[0].lower():
# # pass
# # else:
# # print(f'{row[0]}{row[1]}')
# print(f'{row[0]}{row[1]}')
def _create_sheet(self):
'''Private method
Creates a new spreadsheet.
'''
print("Creating new spreadsheet")
requests = []
# Set the spreadsheet's title.
requests.append({
'updateSpreadsheetProperties': {
'properties': {
'title': f"Emails - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
},
'fields': 'title'
}
})
body = {
"requests": requests
}
sheet = self.sheets.spreadsheets().create().execute()
if sheet:
self.spreadsheet_id = sheet['spreadsheetId']
sheet = self.sheets.spreadsheets()
self.spreadsheet_url = sheet.get(
spreadsheetId=self.spreadsheet_id).execute()
resp = self.sheets.spreadsheets().batchUpdate(
spreadsheetId=self.spreadsheet_id,
body=body
).execute()
print(dir(resp))
with open('spreadsheet.id', 'w') as f:
f.write(self.spreadsheet_id)
async def open_or_create_sheet(self):
'''Decide weather or not to create a new sheet or open an existing sheet
Nothing fancy going on here, basically if there's a spreadsheet.id file
then open it and use that ID. Otherwise create a new file.
'''
if os.path.exists('spreadsheet.id'):
with open('spreadsheet.id', 'r') as s_id:
ss_id = s_id.read()
await self._open_sheet(ss_id)
else:
self._create_sheet()
async def add_emails_to_sheet(self, data):
values = [email for email in data]
body = {'values': values}
sheet_result = self.sheets.spreadsheets().values().update(
spreadsheetId=self.spreadsheet_id,
range=self.sheet_range,
valueInputOption='USER_ENTERED',
# insertDataOption='INSERT_ROWS', # Uncomment for appending.
body=body
).execute()
print(f'{sheet_result.get("updatedCells")} Cells updated.')
def add_new_sheet(self, sheet_title):
requests = []
requests.append({
"addSheet": {
"properties": {
"title": sheet_title,
"tabColor": {
"red": 1.0,
"green": 0.3,
"blue": 0.4
}
}
}
})
body = {
'requests': requests,
}
resp = self.sheets.spreadsheets().batchUpdate(
spreadsheetId=self.spreadsheet_id,
body=body
).execute()
print(dir(resp))