-
Notifications
You must be signed in to change notification settings - Fork 1
/
active_user_bot.py
110 lines (80 loc) · 3.23 KB
/
active_user_bot.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
#! /usr/bin/python3
# Imports
import praw
import time
import csv
import datetime as dt
import matplotlib.pyplot as plt
class OnlineUserBot:
"""A simple bot that finds the number of users online a specifc Subreddit."""
def __init__(self, subreddit_name, interval, lengthOfTime, client_id, client_secret, password, user_agent, username):
self.subreddit_name = subreddit_name
self.interval = interval
self.lengthOfTime = lengthOfTime
self.client_id = client_id
self.client_id = client_id
self.client_secret = client_secret
self.password = password
self.user_agent = user_agent
self.username = username
def auth(self):
self.reddit = praw.Reddit(client_id = self.client_id,
client_secret = self.client_secret,
password = self.password,
user_agent = self.user_agent,
username = self.username)
self.subreddit = self.reddit.subreddit(self.subreddit_name)
def findNumberOnlineUsers(self):
return self.subreddit.accounts_active
def write_information_to_CSV(self, _online_users, _readable_time):
with open('data.csv', "a") as csvFile:
filewriter = csv.writer(csvFile)
filewriter.writerow([_online_users, _readable_time])
csvFile.close()
def activateBot(self):
elapsed_time = time.clock()
while elapsed_time < self.lengthOfTime:
self.auth()
readable_time = str(dt.datetime.now().time().strftime("%H-%M-%S"))
elapsed_time = time.clock()
online_users = self.findNumberOnlineUsers()
self.write_information_to_CSV(online_users, readable_time)
print(elapsed_time)
time.sleep(self.interval)
# Plot data
def plotData(subreddit_name):
# create list holders for our data.
number_of_online_users = []
dateAndTime= []
with open('data.csv', "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter = ',')
for row in csv_reader:
try:
number_of_online_users.append(int(row[0]))
time = row[1]
dateAndTime.append(time)
except Exception as e:
if e == IndexError:
pass
# Print data
print(number_of_online_users)
print(dateAndTime)
plt.plot(dateAndTime, number_of_online_users)
plt.xlabel('Time')
plt.ylabel('Online Users')
plt.title('Subreddit: ' + subreddit_name)
ax = plt.subplot()
every_nth = 40
for i, tick in enumerate(ax.xaxis.get_ticklabels()):
if i % every_nth != 0:
tick.set_visible(False)
for i, line in enumerate(ax.xaxis.get_ticklines()):
if i % every_nth != 0:
line.set_visible(False)
plt.xticks(fontsize=8, rotation=60)
plt.show()
if __name__ == '__main__':
# Change the input to this class with your information from the Reddit API (See README.md for instructions)
bot = OnlineUserBot('SUBREDDIT_NAME', INTERVAL, TOTAL_TIME, 'CLIENT_ID','CLIENT_SECRET','PASSWORD','USER_AGENT','USERNAME')
bot.activateBot()
plotData('SUBREDDIT_NAME')