-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleplusparser_daterangeresults.py
203 lines (147 loc) · 5.77 KB
/
googleplusparser_daterangeresults.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from apiclient.discovery import build
import datetime
import time
from dbconnect import connection
import gc
def delete_existingGP(company):
c, conn = connection()
conn.set_character_set('utf8')
c.execute('SET NAMES utf8;')
c.execute('SET CHARACTER SET utf8;')
c.execute('SET character_set_connection=utf8;')
c.execute("set session sql_mode='';")
conn.commit()
query = "delete from googleplus where company = '%s'" % company
# first delete existing
data = c.execute(query)
conn.commit()
c.close()
conn.close()
gc.collect()
return data
def db_insert(company, data, branch):
c, conn = connection()
conn.set_character_set('utf8')
c.execute('SET NAMES utf8;')
c.execute('SET CHARACTER SET utf8;')
c.execute('SET character_set_connection=utf8;')
c.execute("set session sql_mode='';")
conn.commit()
# then insert new
created = ''
name = ''
content = ''
post_type = ''
likes = None
shares = None
comments = None
company_code = ''
try:
if data[0] and data[0].strip() is not '':
created = data[0]
except IndexError as e:
print e
try:
if data[7] and data[7].strip() is not '':
name = data[7]
except IndexError as e:
print e
try:
if data[6] and data[6].strip() is not '':
content = data[6]
except IndexError as e:
print e
try:
if data[5] and data[5].strip() is not '':
post_type = data[5]
except IndexError as e:
print e
try:
if data[3] and data[3] is not '':
likes = data[3]
except IndexError as e:
print e
try:
if data[4] and data[4] is not '':
shares = data[4]
except IndexError as e:
print e
try:
if data[2] and data[2] is not '':
comments = data[2]
except IndexError as e:
print e
try:
if data[8] and data[8].strip() is not '':
company_code = data[8]
except IndexError as e:
print e
query = "insert into googleplus (company, company_code, created, name, content, type, likes, shares, comments, branch_id) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
c.execute(query, (company, company_code, created, name.encode('utf-8'), content.encode('utf-8'), post_type, likes, shares, comments, branch))
conn.commit()
c.close()
conn.close()
gc.collect()
def GooglePlusParser(company, since, delete, branch):
# since='01/02/2016'
# until='20/03/2016'
until = time.strftime("%d/%m/%Y")
startDate = datetime.datetime.strptime(since, '%d/%m/%Y').strftime('%Y-%m-%d')
startDate = datetime.datetime.strptime(startDate, '%Y-%m-%d')
endDate = datetime.datetime.strptime(until, '%d/%m/%Y').strftime('%Y-%m-%d')
endDate = datetime.datetime.strptime(endDate, '%Y-%m-%d')
# API_KEY = "xxxxxxxxxxxx_yyyyyyyyyyyyyyyyyyyyyyyyyy"# copied from project credentials page
API_KEY = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
MAX_RESULTS = 200
service = build('plus', 'v1', developerKey=API_KEY)
activity_feed = service.activities().list(userId=company, collection="public", maxResults=20)
# items = activity_feed.execute().get('items', [])
activity_results = []
activity_date=endDate
if delete==1:
delete_existingGP(company)
# while activity_feed != None and len(activity_results) < MAX_RESULTS:
while activity_feed != None and activity_date > startDate:
activities = activity_feed.execute()
if 'items' in activities:
for activity in activities['items']:
activity_date= datetime.datetime.strptime(activity["published"].split('T')[0], '%Y-%m-%d')
company_code = activity['actor']['displayName']
replies=0
plusoners=0
resharers=0
objectType=''
displayName=''
content=''
if "attachments" in activity["object"]:
if len(activity["object"]["attachments"])>0:
objectType=activity["object"]["attachments"][0]["objectType"]
if "displayName" in activity["object"]["attachments"][0]:
displayName=activity["object"]["attachments"][0]["displayName"].encode("utf-8").decode("utf-8")
# if "content" in activity["object"]["attachments"][0]:
# content=activity["object"]["attachments"][0]["content"].encode("utf-8").decode("utf-8")
if "title" in activity:
content=activity["title"].encode("utf-8").decode("utf-8")
if activity["object"]["replies"]["totalItems"]:
replies = activity["object"]["replies"]["totalItems"]
if activity["object"]["plusoners"]["totalItems"]:
plusoners = activity["object"]["plusoners"]["totalItems"]
if activity["object"]["resharers"]["totalItems"]:
resharers = activity["object"]["resharers"]["totalItems"]
gp=[
activity["published"].split('T')[0]+" "+activity["published"].split('T')[1][:-1],
activity["updated"],
replies,
plusoners,
resharers,
objectType,
displayName,
content,
company_code
]
activity_results.extend([gp])
db_insert(company, gp, branch)
activity_feed = service.activities().list_next(activity_feed, activities)
return activity_results
# Example:
# print GooglePlusParser('+vodafonegr')