Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import time
- import datetime
- '''USER CONFIGURATION'''
- USERNAME = ''
- PASSWORD = ''
- APP_ID = ""
- APP_SECRET = ""
- SUBREDDIT = 'BedroomBands'
- BOT_NAME = 'r/BedroomBands Aggregator'
- VERSION_NUMBER = '0.15'
- USER_AGENT = BOT_NAME + ' ' + VERSION_NUMBER + ', a script to aggregate r/BedroomBands posts. Written by u/kungming2.'
- BRACKETED_TAGS = ['[COMPLETE]', '[DISCUSSION]', '[LFG]', '[IN PROGRESS]', '[START]', '[QUESTION]']
- TABLE_HEADER = "Date | Post | Author\n-----|------|-------\n"
- TABLE_LINE = "{} | [{}]({}) | {}"
- print('\n|======================Logging in as u/{}...======================|'.format(USERNAME))
- reddit = praw.Reddit(client_id=APP_ID,
- client_secret=APP_SECRET, password=PASSWORD,
- user_agent=USER_AGENT, username=USERNAME)
- r = reddit.subreddit(SUBREDDIT)
- def main_aggregator(): # Main runtime for the bot
- print('\nFetching r/%s posts...' % SUBREDDIT)
- current_time = int(time.time()) # This is the current time.
- current_boundary = current_time - 2592000 # Get stuff from the last 30 days
- to_return = ""
- posts = []
- posts_formatted = {}
- posts += list(r.submissions(start=current_boundary))
- for tag in BRACKETED_TAGS:
- posts_formatted[tag] = []
- for post in posts:
- try:
- pauthor = post.author.name
- pauthor = "u/" + pauthor
- except AttributeError:
- # Author is deleted. We don't care about this post.
- continue
- ptitle = post.title
- ppermalink = post.permalink
- pcreated = post.created_utc
- format_date = datetime.datetime.fromtimestamp(int(pcreated)).strftime('%Y-%m-%d')
- for tag in BRACKETED_TAGS:
- if tag in ptitle.upper():
- match_tag = tag
- posts_formatted[match_tag].append(TABLE_LINE.format(format_date, ptitle, ppermalink, pauthor))
- for key, value in posts_formatted.items():
- if len(value) != 0: # Make sure there are actually posts in this category
- to_return += "## {}\n\n".format(key)
- to_return += TABLE_HEADER
- to_return += "\n".join(value)
- to_return += "\n\n"
- return to_return
- def user_input():
- returned_data = main_aggregator()
- # Write the info to a text file.
- permission = input("\n> Would you like to write the retrieved titles to a text file? Type 'y' or 'n': ")
- if permission == "y":
- f = open('BedroomBandsOutput.md', 'w', encoding="utf-8") # Write data to a Markdown file.
- f.write(returned_data)
- f.close()
- print(">> Saved.")
- else:
- print(">> Did not write to text file.")
- permission = input("\n> Would you like to post the data to r/BedroomBands? Type 'y' or 'n': ")
- if permission == "y":
- post_title = input("\n> Please enter the title for the submitted post: ")
- r.submit(title=post_title, selftext=returned_data, send_replies=True) # Submits a text post to the subreddit
- print(">> Posted a text post.")
- else:
- print(">> Did not post anything.")
- user_input()
Add Comment
Please, Sign In to add comment