Skip to content

Instantly share code, notes, and snippets.

@13steinj
Last active September 6, 2015 23:50
Show Gist options
  • Save 13steinj/7d3e5394f4cc67e0ae22 to your computer and use it in GitHub Desktop.
Save 13steinj/7d3e5394f4cc67e0ae22 to your computer and use it in GitHub Desktop.
WowThisSubExists Self Promo Bot
scope=identity,account,modconfig,modflair,modlog,modothers,modposts,modself,modwiki,read,report,save,submit,subscribe
refreshable=True
app_key=appid
app_secret=appsecret
token=None
refresh_token=None
import praw
import OAuth2Util
from bs4 import BeautifulSoup
import requests
import sqlite3
import time
USERAGENT = 'WowThisSubExists Self Promotion Removal by /u/13steinj in use by /u/KarmaNeutrino'
SUBREDDIT = 'WowThisSubExists'
DBNAME = 'WTSESPB_SQL_DATABASE'
WEIRD_URL = 'Investigate, this post was not checked.'
BOTCOMMENT = """
Your post has been removed for breaking Rule 1: 'No Self Promotion'
*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](https://www.reddit.com/message/compose/?to=/r/{0}) if you have any questions or concerns.*
""".format(SUBREDDIT)
MAX_AT_ONCE = 25
CLEANCYCLES = 20
WAIT = 5
def main():
cur.execute('CREATE TABLE IF NOT EXISTS doneposts(id TEXT)')
print('Database Opened.')
sub = r.get_subreddit(SUBREDDIT)
submissions = sub.get_new(limit=MAX_AT_ONCE)
print('Retrieved new listing for /r/{0}'.format(SUBREDDIT))
for submission in submissions:
print('Getting Post...')
print('Checking if post is in the database.')
cur.execute('SELECT * FROM doneposts WHERE ID=?', [submission.id])
if cur.fetchone() == None:
print('Post is not already in the database')
cur.execute('INSERT INTO doneposts VALUES(?)', [submission.id])
sql.commit()
print('Post has been entered into the database')
if not submission.is_self:
POST_URL = submission.url
PUS = POST_URL.split('/')
if len(PUS) == 6 or len(PUS) == 5:
if PUS[3] != 'r':
submission.report(WEIRD_URL)
SELF_PROMO_SUB = None
else:
SELF_PROMO_SUB = '/r/' + PUS[4]
elif len(PUS) == 4 or len(PUS) == 3:
PUSS = PUS[2].split('.')[0]
if PUSS == 'www' or len(PUSS) < 3 or PUSS == 'reddit':
submission.report(WEIRD_URL)
SELF_PROMO_SUB = None
else:
SELF_PROMO_SUB = '/r/' + PUSS
if SELF_PROMO_SUB != None:
user_page = 'https://www.reddit.com/user/{0}'.format(submission.author)
headers = {'User-Agent':USERAGENT}
r1 = requests.get(user_page, headers=headers)
authors_subs = parse_mod_list(r1.content)
if SELF_PROMO_SUB in authors_subs:
print('Dirty Self-Promo found!')
submission_comment = submission.add_comment(BOTCOMMENT)
submission_comment.distinguish()
submission.remove()
def parse_mod_list(html_doc):
parsed_html = BeautifulSoup(html_doc, 'html.parser')
mod_list = parsed_html.find(id='side-mod-list')
try:
mod_list = mod_list.find_all('a')
except AttributeError:
return []
return [modsub.get_text() for modsub in mod_list]
if __name__ == '__main__':
r = praw.Reddit(USERAGENT)
o = OAuth2Util.OAuth2Util(r)
o.refresh(force=True)
sql = sqlite3.connect('{0}.db'.format(DBNAME))
cur = sql.cursor()
cycles = 0
while True:
try:
main()
cycles += 1
except Exception as e:
print("ERROR: {0}".format(e))
if cycles >= CLEANCYCLES:
print('Cleaning database')
cur.execute('DELETE FROM doneposts WHERE id NOT IN (SELECT id FROM doneposts ORDER BY id DESC LIMIT ?)', [MAX_AT_ONCE * 2])
sql.commit()
cycles = 0
print('Running again in %d seconds \n' % WAIT)
time.sleep(WAIT)

Place both the python script and the oauth.txt file in the same folder.

This uses praw-OAuth2Util version 0.2.2 ONLY. It will not work on a later version, though when my PC works with said later versions I will update the file and this readme.

Variables

  • USERAGENT - Your useragent. A change to this is not recommended.
  • SUBREDDIT - The subreddit for the script to work on.
  • DBNAME - the name of the SQL database made.
  • WEIRD_URL - the report made on a post that is a link that was not expected.
  • BOTCOMMENT - the comment the bot makes. Every instance of {<anychar>} will be converted to the subreddit name.
  • MAX_AT_ONCE - the amount posts on the new queue to check at once. Can be 1-100. Change depending on the speed of your subreddit. Note: Making it None will make it get all new queue posts (max 1k by reddit API limitation), even if it's not intended. Don't do this unless you are on a recently created sub.
  • WAIT - time to wait between cycles

Instructions:

  1. Go to http://reddit.com/prefs. Press apps. Press create/develop an app. Fill the info in as so
  2. Fill in the oauth.txt file with the info you get after you press create

Prerequisites:

  1. Python 3.4
  2. bs4. If you don't have it, run pip3.4 install beautifulsoup4 as an administrator / sudo.
  3. praw. If you don't have it, run pip3.4 install praw as an administrator / sudo.
  4. OAuth2Util 0.2.2. If you don't have it, run pip3.4 install praw-oauth2util==0.2.2 as an administrator / sudo.
  5. All other prerequisites are installed alongside praw / are in Python 3.4 by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment