-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets_funcs.py
107 lines (93 loc) · 3.43 KB
/
sheets_funcs.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
from __future__ import print_function
from array import array
import enum
from attr import field
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from setup import creds
SPREADSHEET_ID = '13zCT1ubfOCAejVlr5UwQNSp1XK3v3aiSopWkxjVLJp0'
PERSONAL_SPREEDSHEET_ID = '1T4zoflM3u1fZ4mDryZXbQhEAxSJg7EbZk-674Fcb7bI'
users_data = {}
class FIELD(enum.Enum):
TECH = 1
DESIGN = 2
SOCIAL = 3
ENTER_STUDIO = 4
GROUPS_DATA = {
FIELD.TECH: "קבוצות מציאת עבודה - הייטק!A4:A",
FIELD.DESIGN: "קבוצות מציאת עובדה - מעצבים!A4:A",
FIELD.SOCIAL: "קבוצות מציאת עבודה - סושיאל!A4:A",
FIELD.ENTER_STUDIO: "קבוצות מציאת פרוייקטים!A4:A"
}
EMAIL_LIST_DATA = {
FIELD.TECH: "רשומים למציאת עבודה - מתכנתים!A2:B",
FIELD.DESIGN: "רשומים למציאת עבודה - מעצבים!A2:B",
FIELD.SOCIAL: "רשומים למציאת עבודה - סושיאל ושיווק!A2:B",
FIELD.ENTER_STUDIO: ""
}
KEYWORDS_DATA = {
FIELD.TECH: "מילות מפתח!B2:B",
FIELD.DESIGN: "מילות מפתח!C2:C",
FIELD.SOCIAL: "מילות מפתח!D2:D",
FIELD.ENTER_STUDIO: "מילות מפתח!A2:A"
}
def get_groups(field: FIELD) -> array:
groups = []
for group in get_spreadsheet_data(GROUPS_DATA[field]):
groups.append(group[0])
return groups
def get_emails(field: FIELD) -> array:
if field == FIELD.ENTER_STUDIO:
return ["livne@s-tov.org.il"]
emails = []
for email in get_spreadsheet_data(EMAIL_LIST_DATA[field]):
emails.append(email[1])
return emails
def get_keywords(field: FIELD) -> array:
keywords = []
for keyword in get_spreadsheet_data(KEYWORDS_DATA[field]):
if keyword:
keywords.append(keyword[0])
return keywords
def get_spreadsheet_data(spreadsheet_range: str, id=SPREADSHEET_ID):
try:
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=id,
range=spreadsheet_range).execute()
values = result.get('values', [])
if not values:
print('No data found.')
return
else:
return values
except HttpError as err:
print(err)
def get_user_data(email: str) -> dict:
if not users_data:
load_users_data()
return users_data.get(email, None)
def load_users_data():
range = 'Form Responses 1!B2:H100'
data = get_spreadsheet_data(range, PERSONAL_SPREEDSHEET_ID)
for value in data:
users_data[value[2]] = {
"name": value[0],
"field": hebrew_to_field(value[1]),
"blacklist": value[4].split(','),
"keywords": value[5],
"send_email": ('מייל' in value[6]),
"send_whatsapp": ('ווטספ' in value[6])
}
def hebrew_to_field(hebrew_str: str) -> FIELD:
# Converts hebrew string to a field type
switcher = {
"תכנות": FIELD.TECH,
"עיצוב גרפי": FIELD.DESIGN,
"שיווק": FIELD.SOCIAL,
"פרוייקטים לסטודיו": FIELD.ENTER_STUDIO}
return switcher.get(hebrew_str, None)