Skip to content

Instantly share code, notes, and snippets.

@madbook
Created November 11, 2016 21:11
Show Gist options
  • Save madbook/4eb0027ad358cccca1127e1b9679aa49 to your computer and use it in GitHub Desktop.
Save madbook/4eb0027ad358cccca1127e1b9679aa49 to your computer and use it in GitHub Desktop.
test-live-comments.py
import random
import time
from r2.lib import websockets
def spew_comments_on_link(link_id36, num_comments=10, delay=.1, start_at_id=1):
"""
Generate a series of fake websocket messages to test live comments
link_id36: id36 of a link, used to namespace the websocket messages
num_comments: how many comments to broadcast
delay: number of seconds to sleep between broadcasts
start_at_id: fake comment id to start at
To use from the command line
$ reddit-run ./test-live-comments.py -c "spew_comments_on_link(24, n=100, delay=1)"
"""
current_comment_id = start_at_id
parent_id36 = None
parent_fullname = None
for i in range(num_comments):
payload = {
'attribs': [],
'subreddit_fullname': 't5_2qh1i',
'body_html': '<!-- SC_OFF --><div class="md"><p>This is a silly *fake* comment to test load (number %(number)s)</p>\n</div><!-- SC_ON -->' % {'number':current_comment_id},
'author_name': 'fakecommenter12345'[random.randint(3,17):],
'subreddit': 'AskReddit',
'permalink': u'/r/AskReddit/comments/59h807/what_did_you_find_out_about_your_wifehusband_only/d98tdku',
'date': 1477505261,
'author_fullname': 't2_7goqv',
'parent_fullname': parent_fullname,
'total_comment_count': current_comment_id,
'_fullname': 't1_%s' % current_comment_id,
'flair_position': 'right',
'_id36': current_comment_id+1,
'flair_text': None,
'full_date': '2016-10-26T18:07:41+00:00',
'parent_id36': parent_id36,
'score': 1,
'flair_css_class': None,
'author_id': 12535879L,
}
websockets.send_broadcast(
namespace="/link/%s" % link_id36,
type="new_comment",
payload=payload,
)
time.sleep(delay)
current_comment_id += 1
if random.random() < .6:
parent_id36 = random.randint(min(int(current_comment_id*.9), 1), current_comment_id-1)
parent_fullname = 't1_%s' % random.randint(min(int(current_comment_id*.9), 1), current_comment_id-1)
else:
parent_id36 = None
parent_fullname = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment