-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
executable file
·212 lines (187 loc) · 6.82 KB
/
main.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
204
205
206
207
import datetime, glob, json, re
from collections import defaultdict, Counter
import users, threads
################################################################################
# this is the main implementation of the API, consisting of many
# functions for the user to use in their analysis of the data
################################################################################
#first load the files we need
with open('THREADS.json', 'r') as f:
COMMENTS_BY_THREAD = json.load(f)
with open('USERS.json', 'r') as f2:
COMMENTS_BY_USER = json.load(f2)
def badInput():
"""prints out bad input for wrong inputs"""
print "bad input, try again"
def getThread(threadID):
"""returns a Thread object for the thread with ID _threadID_"""
thread = None
THREAD = COMMENTS_BY_THREAD.get(threadID, None)
if THREAD:
thread = threads.Thread(threadID, THREAD)
return thread
def threadResponse(threadID, choice = "All"):
"""returns a list of response times for the thread with ID
_threadID. allows for the usual choice of structure with
_choice"""
thread = getThread(threadID)
if choice not in ("All", "Top", "Reply"):
badInput()
else:
return thread.responseTimes(choice)
def threadText(threadID, choice = "All"):
"""returns the a list of the text of thread with _threadID_.
allows the user to choose top-level or replies only, using
_choice_ = "All" or "Reply"""
thread = getThread(threadID)
if choice == "All":
return thread.all_text
elif choice == "Top":
return thread.top_text
elif choice == "Reply":
return thread.reply_text
else:
badInput()
def threadTimes(threadID, choice = "All"):
"""returns a list of times of the comments on the thread,
allows for choice of top-level or replies only as before"""
thread = getThread(threadID)
if choice == "All":
return thread.all_times
elif choice == "Top":
return thread.top_times
elif choice == "Reply":
return thread.reply_times
else:
badInput()
def threadUsers(threadID, choice = "All"):
"""returns a list of userIDs for the users that commented
in thread _threadID_. allows for choice of top-level or
replies using _choice_"""
thread = getThread(threadID)
if choice == "All":
return thread.all_users
elif choice == "Reply":
return thread.reply_users
elif choice == "Top":
return thread.top_users
else:
badInput()
def threadCommentTime(threadID, choice = "All"):
"""returns list of tuples of _threadID_ thread of
messages with their times. allows for _choice_ of top
and reply"""
thread = getThread(threadID)
if choice == "All":
return thread.all_text_time
elif choice == "Top":
return thread.top_text_time
elif choice == "Reply":
return thread.reply_text_time
else:
badInput()
def threadUserCounter(threadID, choice = "All"):
"""returns Counter of most users which commented the
most in _threadID_ thread. allows for _choice_ of
only top-level or replies"""
thread = getThread(threadID)
return thread.counterUsers(choice)
def getUser(userID):
"""returns a User object for the user with _userID_"""
COMMENTS = COMMENTS_BY_USER[userID]
user = users.User(userID, COMMENTS)
return user
def userResponse(userID, choice = "All"):
"""returns the response times of comments made by User with _userID_,
allows for top-level or replies with _choice_"""
user = getUser(userID)
if choice != "All": #need to load the additional structure
user.getStructure()
return user.responseTimes(choice)
def userText(userID, choice = "All"):
"""returns a list of the text of the messages made by _userID_. allows
for _choice_ of top or reply only"""
user = getUser(userID)
if choice != "All":
user.getStructure()
if choice == "All":
return user.all_text
elif choice == "Top":
return user.top_text
elif choice == "Reply":
return user.reply_text
else:
badInput()
def userTimes(userID, choice = "All"):
"""returns a list of the times of the messages made by _userID_. allows for
_choice_ of top or reply only"""
user = getUser(userID)
if choice != "All":
user.getStructure()
if choice == "All":
return user.all_times
elif choice == "Top":
return user.top_times
elif choice == "Reply":
return user.reply_times
else:
badInput()
def userThreads(userID, choice = "All"):
"""returns a list of threadIDs of the threads _userID_ posted in. allows for
_choice_ of top or reply only"""
user = getUser(userID)
if choice != "All":
user.getStructure()
if choice == "All":
return user.all_threads
elif choice == "Top":
return user.top_threads
elif choice == "Reply":
return user.reply_threads
else:
badInput()
def userTextTimes(userID, choice = "All"):
"""returns list of tuples of messages with their post times for _userID_,
allows for _choice_ of top or reply"""
user = getUser(userID)
if choice != "All":
user.getStructure()
if choice == "All":
return user.all_text_time
elif choice == "Top":
return user.top_text_time
elif choice == "Reply":
return user.reply_text_time
else:
badInput()
def userThreadCounter(userID, choice = "All"):
"""returns a Counter of the threads that _userID_ commented in.
allows for _choice_ of top or reply"""
user = getUser(userID)
if choice != "All":
user.getStructure()
return user.counterThreads(choice)
def cutThread(threadID, date_time):
"""allows the user to look at the contents of a thread from the
start until the given _date_time_. this will be a datetime object,
datetime(year, month, day, hour, minute, second, microsecond, timezone).
just returns the slice of the thread as a new Thread object,
which the user can then apply functions to"""
THREAD = COMMENTS_BY_THREAD[threadID]
CUT_THREAD = [] #only want comments before date_time
for comment in THREAD:
formatted = threads.Thread.getTime(comment['time'])
if formatted <= date_time:
CUT_THREAD.append(comment)
#now we have the top level comments from t=0 to _datetime_,
#but we still need to chop off the replies that were made after _datetime_
for com in CUT_THREAD:
new_replies = [] #only want the replies up till _datetime_
if com['replies']:
for reply in com['replies']:
form_reply = threads.Thread.getTime(reply['time'])
if form_reply <= date_time:
new_replies.append(reply)
com['replies'] = new_replies
thread = threads.Thread(threadID, CUT_THREAD)
return thread