def handle_stream(message): print("Got the stream connection") print("starting stream") reddit = get_reddit() print(message) c = next(reddit.subreddit(message).stream.comments()) print(c) emit('comment', {'data': c.body})
def index(subreddit=None): reddit_client = get_reddit() top_titles = None message = None if subreddit: try: top_posts = reddit_client.subreddit(subreddit).hot(limit=10) top_titles = [post.title for post in top_posts] except: message = 'Could not get top posts for {0}'.format(subreddit) else: message = 'Welcome' return render_template('index.html', top_titles=top_titles, message=message)
def post_totd(): """ Post the Top of the Day post. """ submission = get_reddit().subreddit(get_subreddit()).submit( f"Top of the Day - {current_date()}", selftext=get_totd_text()) submission.flair.select(flair_template_id=get_flairs()["totd"]) post_submissions_embed({ "title": submission.title, "description": "There's a new TOTD post!", "url": f"https://reddit.com{submission.permalink}", "timestamp": format_time_iso(submission.created_utc), })
def get_totd_text(): """ Get the text for the Top of the Day post. :return: The body for the post. """ sections = [] # Most Upvoted Posts top_submissions = sorted([ submission for submission in get_reddit().subreddit("all").top("day", limit=5) ], key=lambda x: x.score, reverse=True) items = [format_item(i, item) for i, item in enumerate(top_submissions)] sections.append(get_templates()["section_template"].format( section_title="Most Upvoted Posts of the Day", section_note="", title_body="Title", items="\n".join(items), )) # Most Upvoted Comments comments = [] for submission in top_submissions: comments.extend([[comment, comment.score] for comment in submission.comments if isinstance(comment, Comment)]) top_comments = sorted(comments, key=lambda x: x[1], reverse=True) items = [ format_item(i, item) for i, item in enumerate( [comment_info[0] for comment_info in top_comments[:5]]) ] sections.append(get_templates()["section_template"].format( section_title="Most Upvoted Comments of the Day", section_note= "\n\n^(Note: These may not be entirely accurate. Currently these are out of the comments taken from the top 5 submissions.)", title_body="Body", items="\n".join(items), )) submission_text = get_templates()["main"].format( date=current_date(), sections="\n\n".join(sections)) return submission_text
import re from datetime import datetime from sqlalchemy.orm.exc import NoResultFound from models import Listing from db import Session from reddit import get_reddit reddit = get_reddit() def find_details(submission): for comment in submission.comments: if comment.is_submitter and comment.body.strip().startswith('[DETAILS]') or comment.body.strip().startswith('\[DETAILS\]') or comment.body.strip().startswith('DETAILS'): return comment return def add_listing(comment): submission = comment.submission try: listing = Session.query(Listing).filter(Listing.submission_id == submission.id).one() except NoResultFound: listing = Listing() listing.submission_id = submission.id listing.title = submission.title listing.permalink = submission.permalink listing.flair_text = submission.link_flair_text listing.url = submission.url listing.redditor = submission.author.name listing.created_utc = datetime.utcfromtimestamp(submission.created_utc) listing.updated_utc = datetime.utcnow() if submission.thumbnail.startswith('https:'): listing.thumbnail_url = submission.thumbnail