Skip to content

Instantly share code, notes, and snippets.

@kungming2
Last active November 21, 2019 06:58
Show Gist options
  • Save kungming2/f22b4368c15ca418149420e3f0c8c465 to your computer and use it in GitHub Desktop.
Save kungming2/f22b4368c15ca418149420e3f0c8c465 to your computer and use it in GitHub Desktop.
import requests
import datetime
subreddits = ["livepd", "liverescue"] # ADD TO THIS
amount_of_results = 15
header = "| # Comments | Post & Link | Date |\n|------------|-----------|------|\n"
line_format = '| {:,} | [{}]({}) | {} |'
def get_most_commented(subreddit_name):
total_lines = []
query_string = "https://api.pushshift.io/reddit/search/comment/?subreddit={}&aggs=link_id&sort=asc&size={}".format(subreddit_name, amount_of_results)
returned_data = requests.get(query_string)
returned_data = returned_data.json()['aggs']['link_id']
for item in returned_data:
item_data = item['data']
post_title = item_data['title']
post_link = item_data['permalink']
num_comments = item_data['num_comments']
date = datetime.datetime.utcfromtimestamp(item_data['created_utc']).strftime("%Y-%m-%d")
formatted_line = line_format.format(num_comments, post_title, post_link, date)
total_lines.append(formatted_line)
body = header + '\n'.join(total_lines)
return body
for sub in subreddits:
print(get_most_commented(sub))
print('\n\n-----\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment