-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.py
More file actions
executable file
·159 lines (129 loc) · 5.7 KB
/
data.py
File metadata and controls
executable file
·159 lines (129 loc) · 5.7 KB
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
# webex-messaging-activity-report-sample
# Copyright (c) 2019 Cisco and/or its affiliates.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sqlite3
import webexteamssdk
from webexteamssdk import WebexTeamsAPI
from datetime import datetime, timedelta
import json
def importData(conn, webexAccessToken, startDate, endDate):
cursor = conn.cursor()
resp = cursor.execute(
'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name="messages"'
)
if resp.fetchone()[0] == 0:
resp = cursor.execute(
"""
CREATE TABLE messages (
roomTitle text,
created text,
id text UNIQUE,
parentId text,
roomId text,
roomType text,
text text,
personId text,
personEmail text,
html text,
mentionedPeople text,
mentionedGroups text,
threadCreated text )
"""
)
conn.commit
api = WebexTeamsAPI(access_token=webexAccessToken)
print("\nRetrieving active spaces...")
rooms = api.rooms.list(sortBy="lastactivity")
startTime = (datetime.strptime(startDate, "%Y/%m/%d")).astimezone()
endTime = (datetime.strptime(endDate, "%Y/%m/%d") + timedelta(days=1)).astimezone()
roomCount = 0
messageCount = 0
print("Importing messages (spaces/messages)...", end="")
roomsListRetries = 0
while roomsListRetries < 2:
try:
for room in rooms:
if room.lastActivity < startTime:
break
roomCount += 1
messages = api.messages.list(
roomId=room.id, before=endTime.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
)
data = []
for message in messages:
if message.created < startTime:
break
if message.created > endTime:
continue
messageCount += 1
print(
f"\rImporting messages (spaces/messages): {roomCount}/{messageCount}",
end="",
)
mentionedPeople = (
message.mentionedPeople
if hasattr(message, "mentionedPeople")
else None
)
mentionedGroups = (
message.mentionedGroups
if hasattr(message, "mentionedGroups")
else None
)
parentId = (
message.json_data["parentId"]
if ("parentId" in message.json_data.keys())
else None
)
messageCreated = message.created.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
threadCreated = messageCreated if parentId is None else None
data.append(
(
room.title,
messageCreated,
message.id,
parentId,
message.roomId,
message.roomType,
message.text,
message.personId,
message.personEmail,
message.html,
json.dumps(mentionedPeople),
json.dumps(mentionedGroups),
threadCreated,
)
)
try:
cursor.executemany(
"REPLACE INTO messages VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", data
)
cursor.execute(
"REPLACE INTO messages "
+ "(roomTitle,created,id,parentId,roomId,roomType,text,personId,personEmail,html,mentionedPeople,mentionedGroups,threadCreated) "
+ "SELECT m2.roomTitle,m2.created,m2.id,m2.parentId,m2.roomId,m2.roomType,m2.text,m2.personId,m2.personEmail,m2.html,m2.mentionedPeople,m2.mentionedGroups,m1.created "
+ "FROM messages m1 "
+ "INNER JOIN messages m2 ON m1.id = m2.parentId"
)
conn.commit()
except Exception as err:
print(f"nERROR in SQL REPLACE INTO: { err }")
except webexteamssdk.ApiError as err:
roomsListRetries += 1
print(f"nERROR in rooms.list: { err }")
break
print()