kungming2

r/BedroomBands Aggregator

Jan 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. import praw
  2. import time
  3. import datetime
  4.  
  5. '''USER CONFIGURATION'''
  6. USERNAME = ''
  7. PASSWORD = ''
  8. APP_ID = ""
  9. APP_SECRET = ""
  10.  
  11. SUBREDDIT = 'BedroomBands'
  12.  
  13. BOT_NAME = 'r/BedroomBands Aggregator'
  14. VERSION_NUMBER = '0.15'
  15. USER_AGENT = BOT_NAME + ' ' + VERSION_NUMBER + ',  a script to aggregate r/BedroomBands posts. Written by u/kungming2.'
  16.  
  17. BRACKETED_TAGS = ['[COMPLETE]', '[DISCUSSION]', '[LFG]', '[IN PROGRESS]', '[START]', '[QUESTION]']
  18. TABLE_HEADER = "Date | Post | Author\n-----|------|-------\n"
  19. TABLE_LINE = "{} | [{}]({}) | {}"
  20.  
  21. print('\n|======================Logging in as u/{}...======================|'.format(USERNAME))
  22.  
  23. reddit = praw.Reddit(client_id=APP_ID,
  24.                      client_secret=APP_SECRET, password=PASSWORD,
  25.                      user_agent=USER_AGENT, username=USERNAME)
  26. r = reddit.subreddit(SUBREDDIT)
  27.  
  28.  
  29. def main_aggregator():  # Main runtime for the bot
  30.  
  31.     print('\nFetching r/%s posts...' % SUBREDDIT)
  32.     current_time = int(time.time())  # This is the current time.
  33.     current_boundary = current_time - 2592000  # Get stuff from the last 30 days
  34.  
  35.     to_return = ""
  36.     posts = []
  37.     posts_formatted = {}
  38.     posts += list(r.submissions(start=current_boundary))
  39.    
  40.     for tag in BRACKETED_TAGS:
  41.         posts_formatted[tag] = []
  42.    
  43.     for post in posts:
  44.        
  45.         try:
  46.             pauthor = post.author.name
  47.             pauthor = "u/" + pauthor
  48.         except AttributeError:
  49.             # Author is deleted. We don't care about this post.
  50.             continue
  51.    
  52.         ptitle = post.title
  53.         ppermalink = post.permalink
  54.         pcreated = post.created_utc
  55.         format_date = datetime.datetime.fromtimestamp(int(pcreated)).strftime('%Y-%m-%d')
  56.        
  57.         for tag in BRACKETED_TAGS:
  58.             if tag in ptitle.upper():
  59.                 match_tag = tag
  60.                 posts_formatted[match_tag].append(TABLE_LINE.format(format_date, ptitle, ppermalink, pauthor))
  61.    
  62.     for key, value in posts_formatted.items():
  63.         if len(value) != 0:  # Make sure there are actually posts in this category
  64.             to_return += "## {}\n\n".format(key)
  65.             to_return += TABLE_HEADER
  66.             to_return += "\n".join(value)
  67.             to_return += "\n\n"
  68.    
  69.     return to_return
  70.  
  71.  
  72. def user_input():
  73.     returned_data = main_aggregator()
  74.    
  75.     # Write the info to a text file.
  76.     permission = input("\n> Would you like to write the retrieved titles to a text file? Type 'y' or 'n': ")
  77.  
  78.     if permission == "y":
  79.         f = open('BedroomBandsOutput.md', 'w', encoding="utf-8")  # Write data to a Markdown file.
  80.         f.write(returned_data)
  81.         f.close()
  82.         print(">> Saved.")
  83.     else:
  84.         print(">> Did not write to text file.")
  85.    
  86.     permission = input("\n> Would you like to post the data to r/BedroomBands? Type 'y' or 'n': ")
  87.    
  88.     if permission == "y":
  89.         post_title = input("\n> Please enter the title for the submitted post: ")
  90.         r.submit(title=post_title, selftext=returned_data, send_replies=True)  # Submits a text post to the subreddit
  91.         print(">> Posted a text post.")
  92.     else:
  93.         print(">> Did not post anything.")
  94.  
  95.  
  96. user_input()
Add Comment
Please, Sign In to add comment