-
Notifications
You must be signed in to change notification settings - Fork 1
/
so4t_inactive_users.py
167 lines (134 loc) · 5.54 KB
/
so4t_inactive_users.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
'''
This Python script is a labor of love and has no formal support from Stack Overflow.
If you run into difficulties, reach out to the person who provided you with this script.
Or, open an issue here: https://github.com/jklick-so/so4t_inactive_users/issues
'''
# Standard Python libraries
import argparse
import csv
import time
# Local libraries
from so4t_api_v2 import V2Client
def main():
# Get command-line arguments
args = get_args()
if args.days: # Number of days of inactivity to consider a user inactive
inactive_days = args.days
else:
inactive_days = 180 # Default to 180 days
# Create a client object
client = V2Client(args.url, key=args.key, token=args.token)
# Get user data
users = get_user_data(client)
# Create user lists
current_unix_time = int(time.time())
inactive_threshold = current_unix_time - (inactive_days * 86400)
all_inactive_users = []
inactive_users_without_contributions = []
inactive_users_with_contributions = []
for user in users:
if user['last_access_date'] < inactive_threshold:
user['inactive_days'] = round((current_unix_time - user['last_access_date']) / 86400)
all_inactive_users.append(user)
if not (user['answer_count'] > 0 or user['question_count'] > 0 or user['article_count'] > 0
or user['comment_count'] > 0):
inactive_users_without_contributions.append(user)
else:
inactive_users_with_contributions.append(user)
# Write user data to CSV
write_user_data(f'all_users_inactive_for_{inactive_days}_days.csv', all_inactive_users)
write_user_data(f'noncontributing_users_inactive_for_{inactive_days}_days.csv',
inactive_users_without_contributions)
write_user_data(f'contributing_users_inactive_for_{inactive_days}_days.csv',
inactive_users_with_contributions)
def get_args():
parser = argparse.ArgumentParser(
prog='so4t_inactive_users.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Uses the Stack Overflow for Teams API to create \
a user report.',
epilog = 'Example for Stack Overflow Business: \n'
'python3 so4t_inactive_users.py --url "https://stackoverflowteams.com/c/TEAM-NAME" '
'--token "YOUR_TOKEN" \n\n'
'Example for Stack Overflow Enterprise: \n'
'python3 so4t_inactive_users.py --url "https://SUBDOMAIN.stackenterprise.co" '
'--key "YOUR_KEY"\n\n')
parser.add_argument('--url',
type=str,
help='Base URL for your Stack Overflow for Teams instance. ')
parser.add_argument('--token',
type=str,
help='API token for your Stack Overflow for Teams instance. ')
parser.add_argument('--key',
type=str,
help='API key value. Required if using Stack Overflow Enterprise')
parser.add_argument('--days',
type=int,
help='Only report on users that have been inactive by at least this many '
'days. Default is 180 days.')
return parser.parse_args()
def get_user_data(client):
if client.soe: # Stack Overflow Enterprise
filter_attributes = [
'user.answer_count',
'user.down_vote_count',
'user.is_deactivated',
'user.question_count',
'user.up_vote_count',
'user.verified_email'
]
filter_string = client.create_filter(filter_attributes)
else: # Stack Overflow Business
filter_string = '!6WPIommaBqvw3'
users = client.get_all_users(filter_string)
# Filter out users with a user_id less than `1`
users = [user for user in users if user['user_id'] > 0]
# Add additional user fields
for user in users:
user['article_count'] = 0
user['comment_count'] = 0
# Get additional user data from comments and articles
# Question count and answer count are already included in the user data
articles = client.get_all_articles()
comments = client.get_all_comments()
for article in articles:
try:
article_owner_id = article['owner']['user_id']
except KeyError: # Article was made by a deleted user
continue
for user in users:
if user['user_id'] == article_owner_id:
user['article_count'] += 1
for comment in comments:
try:
comment_owner_id = comment['owner']['user_id']
except KeyError: # Comment was made by a deleted user
continue
for user in users:
if user['user_id'] == comment_owner_id:
user['comment_count'] += 1
return users
def write_user_data(file_name, users):
with open(file_name, 'w', newline='') as csv_file:
field_names = [
'user_id',
'account_id',
'verified_email',
'display_name',
'inactive_days',
'is_deactivated',
'reputation',
'answer_count',
'question_count',
'article_count',
'comment_count',
'down_vote_count',
'up_vote_count'
]
writer = csv.DictWriter(csv_file, fieldnames=field_names, extrasaction='ignore')
writer.writeheader()
for user in users:
writer.writerow(user)
print(f"User data written to {file_name}")
if __name__ == '__main__':
main()