# Reddit-scraping API bot

I would like to improve my Reddit-scraping code to make it faster, but I don't know how. I am using deque instead of list to improve append performance. Otherwise my code calls the PRAW API and type checks one class attribute (I don't know why this is necessary but otherwise I get type errors). Is there a faster way to do this? These functions are surprisingly slow, and performance time is also highly variable in addition to having a high mean execution time.

For a given user, I'm trying to retrieve their most recent comments and posts. I've pasted the code at the bottom of this post, but I have two problems. First, the code seems to run awfully slow considering how little information I'm retrieving. Second, the variance in execution time from one function call to the next is surprisingly high. You'll see I have two sleep commands for 1 second each, so the minimum time for get_user_comments_and_posts() to execute is 2 seconds. Sometimes I see this, but sometimes I see 14 seconds! And when I print out the output for a given function call, there doesn't seem to be anything special about the output for a short vs. long execution time.

COMMENT_LIMIT = 2

@timeit
time.sleep(TIME_SLEEP)
time.sleep(TIME_SLEEP)
self.get_submissions()

@timeit
if isinstance(c.subreddit, basestring):
subreddit_name = c.subreddit
else:
subreddit_name =  c.subreddit.display_name
new_comment = comment(c.created_utc, c.ups, subreddit_name)
print commentD

@timeit
def get_c_praw_call(self):

@timeit
def get_submissions(self):
submissions_list =  deque([])
submissions = self.get_s_praw_call()
for s in submissions:
if isinstance(s.subreddit, basestring):
subreddit_name = s.subreddit
else:
subreddit_name =  s.subreddit.display_name

@timeit
def get_s_praw_call(self):


Here's the output from the timeit decorator:

this comment has 0 votes and belongs in the todayilearned subreddit where it was made at 1434032968
this comment has 1 votes and belongs in the politics subreddit where it was made at 1434026834
'get_s_praw_call'  0.00 sec
'__init__'  0.00 sec
'__init__'  0.00 sec
this has title: Rupert Murdoch stepping down from 21st Century Fox and came from subreddit: news
this has title: Top athletics coach Alberto Salazar faces doping claims and came from subreddit: news
'get_submissions'  0.62 sec
'get_c_praw_call'  0.09 sec
'__init__'  0.00 sec
'__init__'  0.00 sec
this comment has 1 votes and belongs in the todayilearned subreddit where it was made at 1434044423
this comment has 1 votes and belongs in the todayilearned subreddit where it was made at 1434044125
'get_s_praw_call'  0.00 sec
'__init__'  0.00 sec
'__init__'  0.00 sec
this has title: [SunTuThursday Forum] Halodrol - DAA - Sub-Q Injections - Squid vs. Octopus vs. Cuttlefish: Which is the ultimate
cephalod? - Ask Anything! and came from subreddit: steroids
this has title: [Off-Topic Discussion] "Everyone has an opinion, and the guy screaming for censorship may be the next guy to have his
ideas cut off." - Richard King and came from subreddit: steroids
'get_submissions'  0.86 sec
'get_c_praw_call'  0.05 sec
'__init__'  0.00 sec
'__init__'  0.00 sec
this comment has 0 votes and belongs in the PaoMustResign subreddit where it was made at 1434040381
this comment has 1 votes and belongs in the Art subreddit where it was made at 1434038905
'get_s_praw_call'  0.00 sec
'__init__'  0.00 sec
'__init__'  0.00 sec
this has title: How much lower of an iq is the average FPH poster? and came from subreddit: circlebroke
this has title: When will GallowBoob reach Bill Brasky status? and came from subreddit: circlebroke
'get_submissions'  0.99 sec


So it is often taking upwards of 3 seconds to process the API calls. Is this normal or am I making some really stupid blunder?

• It looks like you're using PRAW library. This library enforces Reddit's restriction of only permitting 30 API requests per minute (60 for OAuth sessions). "PRAW was designed to make following the API guidelines simple. It will not send a request more often than every 2 seconds and it caches every page for 30 seconds." See here – Dustin Oct 4 '15 at 23:53