-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDoc_Reader.py
106 lines (83 loc) · 3.38 KB
/
GDoc_Reader.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
import re
import csv
import os
import datetime
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import client_list
class ReadSpreadsheet:
def __init__(self, chosenClient, ourClients):
self.SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
self.KEY_FILE_LOCATION = 'client_secrets.json'
self.chosenClient = chosenClient
self.clients = ourClients
self.sheets = []
self.data = []
self.cleanData = []
def initialize(self):
# Initializes an API V4 service object.
#
# Returns:
# An authorized API V4 service object.
credentials = ServiceAccountCredentials.from_json_keyfile_name(
self.KEY_FILE_LOCATION, self.SCOPES)
# Build the service object.
self.serviceObject = build('sheets', 'v4', credentials=credentials)
def getSheets(self):
spreadsheetidname = self.clients[self.chosenClient]
result = self.serviceObject.spreadsheets().get(spreadsheetId=spreadsheetidname).execute()
mysheets = result.get('sheets')
for each in mysheets:
self.sheets.append(each.get('properties').get('title'))
def getData(self):
spreadsheetidname = self.clients[self.chosenClient]
majordimensionname = 'COLUMNS'
rangenames = []
for sheet in self.sheets:
rangename = '%s!A:ZZ' % sheet
rangenames.append(rangename)
for ranges in rangenames:
result = self.serviceObject.spreadsheets().values().get(
spreadsheetId=spreadsheetidname, range=ranges, majorDimension=majordimensionname).execute()
self.data.append(result.get('values', []))
def emailCleaner(self):
expression = re.compile(ur'[\w\.-]+@[\w\.-]+\.[a-zA-Z]+', re.UNICODE)
for l1 in self.data:
for l2 in l1:
for l3 in l2:
match = re.search(expression, l3)
if match:
email = match.group(0)
if not any(email in s for s in self.cleanData):
self.cleanData.append(email)
def csvWriter(self):
todaysDate = datetime.date.today().strftime('%y%m%d')
fileName = "Mailing_List_%s_%s.csv" % (self.chosenClient.replace(" ", "-"), todaysDate)
with open(os.path.expanduser(os.path.join("~/Desktop", fileName)), 'wb') as csvfile:
mailing_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for i in self.cleanData:
mailing_writer.writerow([i])
def runAll(self):
self.initialize()
self.getSheets()
self.getData()
self.emailCleaner()
self.csvWriter()
if __name__ == '__main__':
for client in client_list.ourClients:
print client.title()
print "All\n"
chooseTheClient = raw_input("Which client?: ").lower()
if chooseTheClient == "all":
for each in client_list.ourClients:
a = ReadSpreadsheet(each, client_list.ourClients)
a.runAll()
exit(0)
try:
myKey = client_list.ourClients[chooseTheClient]
except KeyError:
print "No client %s exists" %chooseTheClient
exit(1)
a = ReadSpreadsheet(chooseTheClient, client_list.ourClients)
a.runAll()
exit(0)