-
Notifications
You must be signed in to change notification settings - Fork 1
/
redditviz.py
102 lines (82 loc) · 2.69 KB
/
redditviz.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
import pygame
from pygame.locals import *
import praw
from math import pi, cos, sin, sqrt
import time
from pprint import pprint
import os
# center window on screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# pygame
pygame.init()
w, h = 1024, 768
screen = pygame.display.set_mode((w, h))
screen.fill((255,255,255))
pygame.display.flip()
done = False
# screen pos vars
cx, cy = w/2, h/2
# graph scalers
scale = 300
inc = .01
# praw
r = praw.Reddit('Reddit test script -- amstocker.wordpress.com')
thread = r.get_submission(
submission_id = "1oo0ug",
comment_limit = None
)
# tracked a comment object
tracked_comments = {}
# tracked by username
tracked_users = {}
### -----FUNCTIONS-----
# function to increment node v1 towards node v2 by d
def increment(v1, v2, d):
p = (v2[0]-v1[0], v2[1]-v1[1])
s = d/sqrt(p[0]**2 + p[1]**2)
p = (p[0]*s, p[1]*s)
return (v1[0]+p[0], v1[1]+p[1])
# function to move users based on commenting
def increment_users(parent, comments, user_dict):
for comment in comments:
if comment.__class__.__name__ != 'MoreComments':
user_dict[comment.author] = increment(
user_dict[comment.author],
user_dict[parent.author],
inc
)
if len(comment.replies) > 0:
increment_users(comment, comment.replies, user_dict)
# store data for all initially active users in a thread
def init_users(thread, user_dict):
user_dict[thread.author] = (0,0)
for comment in praw.helpers.flatten_tree(thread.comments):
if comment.__class__.__name__ != 'MoreComments':
user_dict[comment.author] = (0,0)
size = len(user_dict)
theta = 2*pi/size
for i, user in enumerate(user_dict):
user_dict[user] = (cos(i*theta), sin(i*theta))
def render_user(user_pos, scaler):
c = (int(cx+scaler*user_pos[0]), int(cy+scaler*user_pos[1]))
pygame.draw.circle(screen, (0,0,0), c, 6)
pygame.draw.circle(screen, (240,46,113), c, 5)
init_users(thread, tracked_users)
### -----MAIN LOOP-----
while done == False:
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == KEYUP:
if event.key == K_ESCAPE:
done = True
increment_users(thread, thread.comments, tracked_users)
# this line of code below clears the screen, but I'm commenting it
# out in order to create a ghosting effect to show the movement
# of the nodes.
# screen.fill((255,255,255))
for user in tracked_users:
render_user(tracked_users[user], scale)
pygame.display.flip()
## eventually need to time API calls here ---> time.sleep(30)
pygame.quit()