Created
March 13, 2019 12:31
-
-
Save bicubic/774bf7ae25c29d78acb39d6d2b07849c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
import datetime | |
def get_comments(): | |
url = "https://www.reddit.com/r/all/comments.json?limit=100" | |
hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' } | |
req = urllib.request.Request(url, headers=hdr) | |
response = urllib.request.urlopen(req) | |
return json.loads(response.read()) | |
comment_buffer = {} | |
new_comments = 0 | |
last_stats = datetime.datetime.now() | |
def stream_tick(): | |
global comment_buffer, new_comments | |
j = get_comments() | |
comments = j['data']['children'] | |
for c in comments: | |
comment = c['data'] | |
comment_id = comment['id'] | |
if comment_id not in comment_buffer: | |
new_comments+=1 | |
comment_buffer[comment_id] = comment | |
now = datetime.datetime.now() | |
if (now - last_stats).total_seconds() >= 1: | |
print(new_comments) | |
new_comments = 0 | |
while(True): | |
stream_tick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment