-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataRetrieval.py
132 lines (100 loc) · 3.62 KB
/
dataRetrieval.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
import requests
import dataHelper
import json
'''
This module is responsible for gathering data from either the API or from
local files.
'''
'''
Current plan is to have these functions support both getting data from the api
and local data from a download
Need to implement supporting local json reading (streaming using ijson library)
Might want to use the class for chatMessages here to support storing message
data more easily
'''
baseEndpoint = "https://api.groupme.com/v3"
clientId = "4y3lFZ9DhxlgXX88VoQImZ9cLN9YK7u2YpocK6Jst9fCMoxn"
#getting the access token from a file so it is not public on Git
#in the future we will have user input their own access token on UI
accessToken = ""
with open('Files\\Access Token.txt') as f:
accessToken = f.read()
'''
Returns a json response from the given endpoint
Returns False on error
'''
def doAPIRequest(endpoint):
try:
#call the API
response = requests.get(url=endpoint)
response.raise_for_status()
return response.json()["response"]
except requests.exceptions.RequestException as e:
#on any error, return False
print(e)
return False
'''
Returns a list of tuples of all members in the group (userId, username)
'''
def getAPIGroupMembers(groupId):
endpoint = baseEndpoint + "/groups/" + groupId + "?token=" + accessToken
response = doAPIRequest(endpoint)
members = []
for member in response["members"]:
members.append((member["user_id"], member["nickname"]))
return members
'''
Gets all of the user's active groups
Returns a list of json responses for all active groups
'''
def getAPIGroups():
#responses are pagenated, potentially need multiple requests to get all groups
groups = [] #list of json responses
pageNum = 1
while True:
endpoint = baseEndpoint + "/groups?&per_page=30&omit=memberships&page=" + str(pageNum) + "&token=" + accessToken
response = doAPIRequest(endpoint)
#if the response is empty, we have gotten every group or an error
if(not response):
return groups
groups.extend(response)
pageNum += 1
'''
Gets a chunk of messages from a group, messages are retrieved from newest to oldest
groupId - (string) ID of the group to get messages from
beforeId - (string) Message ID, get the messages before the given message ID (optional)
Returns a list of json responses for messages, empty list if no messages are retrieved
'''
def getAPIMessageChunk(groupId, beforeId=None):
limit = 100 #how many messages to get in one request (max 100)
endpoint = baseEndpoint + "/groups/" + groupId + "/messages?limit=100"
#if a beforeId is provided, include it in the request
if(beforeId):
endpoint += "&before_id=" + beforeId
endpoint += "&token=" + accessToken
response = doAPIRequest(endpoint)
if(not response):
return []
return response["messages"]
'''
The "chunk" is all the group's message data, could potentially use a lot of memory
Should eventually find some way to stream the data in for less memory usage if it is
a problem.
Returns a list of json responses
'''
def getLocalMessageChunk(groupId):
with open(str(groupId) + "/message.json", encoding = 'UTF-8') as json_file:
print("Loading messages")
return json.load(json_file)
'''
Returns a list of tuples of all members in the group (userId, username)
'''
def getLocalGroupMembers():
return "Implement here"
'''
Identify which groups are downloaded locally
'''
#need to find how to format the return, I don't think a group json response comes with
#locally downloaded data
def getLocalGroups():
return None