joubin jabbari
  • blog
  • contact
  • resume

I got banned; doesn't matter still had [fun]

A few days ago, I saw a post on /r/AskReddit where the poster asked which sub has the most toxic community. I really didn't care about the answer but was happy to learn about new communities I didn't know anything about.

One of the posts mentioned /r/redditgetsdrawn which I had never heard of. Then I saw that people are providing pretty good drawings, you know - a few here and there, in between thousands of "good old college try" ones that don't look all that great.

So I thought to myself, this is something a bot can do. Take those drawings and add a pencil filter to it. ImageMagick has been a tool I've used before so I started.

# Bot Code

import praw
import imghdr, urllib, cStringIO
import uuid
import urllib2
import os, sys
from imgurpython import ImgurClient
from subprocess import call
import time
def buildCommand(submission):
	# print data
	# Don't post if you have even posted here
	if hasPostedHere(submission):
		return

	# print submission.title
	submission.upvote # upvote the current post to be nice
	data = submission.url # get the url of a post and check to see if its a link to outside of reddit
	if "reddit" in data: 
		# print "reddit link"
		return

	comment = "Hello, I'm a bot written by ichillax. In the event that my human counterparts can't generate anything that you'd like, I would like to gift you my own drawing <3\n\n"
	
	response = urllib2.urlopen(data)
	# Save the file 
	with open('workfile', 'wb') as f:
		f.write(response.read())
	# Check to see if the file is an image file and rename it properly if it is
	if imghdr.what('workfile') != None:
		newFileName = str(uuid.uuid1())+"."+imghdr.what('workfile')
		os.rename('workfile', newFileName)
		# Turn the file into a sketch
		runCommand(newFileName)
		# Upload the file to imgur
		imagurPath = client.upload_from_path("prime_"+newFileName, config=None, anon=True)['link']
		# delete the file locally from the machine
		os.remove(newFileName)
		os.remove("prime_"+newFileName)
		# add the image to comment text
		comment += imagurPath
		# print "uploaded", imagurPath
		print "Commented", submission.title
		submission.add_comment(comment)

	else:
		os.remove('workfile')


def runCommand(filePath):
	#print "Drawing..."
	os.system("./Draw.sh "+filePath)
	# print "Done drawing"

def hasPostedHere(submission):
	submission.replace_more_comments(limit=None, threshold=0)
	flat_comments = praw.helpers.flatten_tree(submission.comments)
	for comment in flat_comments:
		if str(comment.author) == str(BOT_USER_NAME):
			print "Already commented here"
			return True
	return False
			

###### ####################
#		IMGUR IDS		 #
###### ####################
client_id_imgur = 'someid'
client_secret_imgur = 'somesecret'
client = ImgurClient(client_id_imgur, client_secret_imgur)
###### ####################
#		IMGUR IDS		 #
###### ####################

###### ####################
#		RedditOauth		 #
###### ####################
client_id_reddit = 'someid'
client_secret_reddit = 'somesecret'
###### ####################
#		RedditOauth		 #
###### ####################




r = praw.Reddit('OAuth testing example by u/_Daimon_ ver 0.1 see '
                'https://praw.readthedocs.org/en/latest/'
                'pages/oauth.html for source')

r.set_oauth_app_info(client_id='client_id_reddit',
                     client_secret='client_seceret_reddit',
                     redirect_uri='https://jabbari.io/'
                                  'authorize_callback')
Username = "someuser"
Password = "A good passw0rd"
BOT_USER_NAME=Username
r.login(BOT_USER_NAME, Password)
for submission in r.get_subreddit('RedditGetsDrawn').get_new(limit=None):
		# buildCommand(submission)
		try:
			buildCommand(submission)
			time.sleep(1)
		except Exception, e:
			print e
		

# Filter Code

convert $1 -colorspace gray \
          \( +clone -tile pencil_tile.gif -draw "color 0,0 reset" \
             +clone +swap -compose color_dodge -composite \) \
          -fx 'u*.2+v*.8' "prime_$1"

# helper code

convert -size 256x256 xc:  +noise Random  -virtual-pixel tile \
             -motion-blur 0x20+135 -charcoal 1 -resize 50% pencil_tile.gif

# Results


August 09 2015

Joubin Jabbari | Github | Twitter