#!/usr/bin/env python3

# -*- coding: UTF-8 -*-

import datetime

import time

import traceback

import praw

BOT_NAME = 'Day Flair Limiter'

VERSION_NUMBER = '0.1'

USER_AGENT = '{} v{} a service bot written by u/kungming2.' . format ( BOT_NAME , VERSION_NUMBER )

SUBREDDIT = "" # CHANGEME The name of the subreddit this is active on.

USER_NAME = "" # CHANGEME The username of the account the bot is using.

WAIT = 60 # CHANGEME How many seconds the bot sleeps between cycles.

# CHANGEME This bot will only allow the following flairs to be posted on their equivalent days. Edit it however you like.

# This is a dictionary. Keep each item in this format: "POST FLAIR TEXT": "WEEKDAY"

RESTRICTED_DAYS = { "Merchandise" : "Monday" , "Screenshot" : "Saturday" }

# CHANGEME Customize the automated comment you would like this bot to make on non-compliant posts.

RESTRICTED_REPLY = """

Hey there u/{},

We apologize for the inconvenience, but "{}" posts are only allowed to be posted on **{}s** (UTC).

Please re-submit your post then!

---

^u/{}: ^an ^assistant ^bot ^for ^r/{}

"""

print ( '

Logging in as u/{}...' . format ( USER_NAME ) )

# CHANGEME Fill in the client ID, client secret, and account password that the bot uses.

reddit = praw. Reddit ( client_id = "" ,

client_secret = "" , password = '' ,

user_agent = USER_AGENT , username = USER_NAME )

def fetch_posts ( ) :

"""

The main routine that checks the subreddit for posts.

"""

# Get the current day as a string (e.g. Monday)

date_string = datetime . datetime . utcfromtimestamp ( time . time ( ) ) . strftime ( "%Y-%m-%d" )

posts = [ ]

posts + = list ( reddit. subreddit ( SUBREDDIT ) . new ( limit = 50 ) )

# Go through posts to analyze them.

for post in posts:

post_flair = post. link_flair_text

try :

post_author = post. author . name

except AttributeError : # Author is deleted.

continue

# There is no link flair (this should be taken care of by Artemis)

if post_flair is None :

continue

# If the post is saved, that means I've acted upon it already.

if post. saved :

continue

# The post flair of this post is restricted to certain days only. Let's check.

if post_flair in RESTRICTED_DAYS:

allowable_day = RESTRICTED_DAYS [ post_flair ]

# The day does not match the allowable day. Sticky a comment about that, and remove the post.

if allowable_day != date_string:

print ( "

>> Post is a type that should not be posted today.

" )

my_reply = post. reply ( RESTRICTED_REPLY. format ( post_author , post_flair , allowable_day , USER_NAME ,

SUBREDDIT ) ) # Reply to the post about our rules.

my_reply. mod . distinguish ( sticky = True ) # Distinguish the bot's comment.

post. mod . remove ( ) # Remove the post.

post. save ( ) # Save the post so we know we've acted on it.

while True :

# noinspection PyBroadException

try :

fetch_posts ( )

except Exception as e:

traceback . print_exc ( )

print ( 'Cycle completed. I will restart in {} seconds.

' . format ( WAIT ) )