import praw, json, textwrap from time import sleep from prawcore import NotFound reddit = praw.Reddit() def sub_exists(sub): exists = True try: reddit.subreddits.search_by_name(sub, exact=True) except NotFound: exists = False return exists def formatstr(s, width): return textwrap.fill(s.encode('ascii', errors='ignore').decode(), width) def bar(amount, total, width, unit=''): left = int(width/total*amount) right = width-left-1 if (left >= width): print('\r['+'='*width+'] '+str(amount)+'/'+str(total)+('' if unit == '' else ' '+unit)+' ('+str(round(amount/total*100, 1))+'%)', end='') return print('\r['+'='*left+'>'+' '*right+'] '+str(amount)+'/'+str(total)+('' if unit == '' else ' '+unit)+' ('+str(round(amount/total*100, 1))+'%)', end='') print('Subreddit Scrapper by Brendan Westley') subname = input('Subreddit: ') if False:#sub_exists(subname): print('Sub', subname, 'does not exist.') else: numitems = int(input('Number of items: ')) width = int(input('Characters per line (default 130): ') or 130) fn = input('Filename (default dump.txt): ') or 'dump.txt' subreddit = reddit.subreddit(subname) f = open(fn, 'w') i = 0 print('Retreving') for submission in subreddit.hot(limit=numitems): bar(i, numitems, 30, 'posts') i += 1 f.write(formatstr('{' + str(submission.score) + ' points} ' + submission.title, width)) f.write('\n\n---------------------------------\n\n') f.write(formatstr(submission.selftext, width)) f.write('\n\n=================================\n\n') f.close()