Open kjschiroo opened 7 years ago
I had the same issue. It appears the praw API has changed significantly as of v4. I was able to get the code working by installing praw 3.6.1, but it would be good if the code could be updated to work on the more recent versions.
Yep, same here. Looks like the new PRAW needs more authentication. Mine ran fine when I added this:
reddit = praw.Reddit(client_id=[my id],
client_secret=[my secret],
user_agent=[a random string explaining my 'app'],
username=[my reddit username],
password=[my reddit password)
In addition to more authentication being needed, the PRAW devs changed some of their method names and moved some stuff around (namely, you can access upvote_ratio
in the submission object now):
for sub in top_submissions:
try:
ratio = sub.upvote_ratio
ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
upvotes.append(sub.ups)
downvotes.append(ups - sub.score)
contents.append(sub.title)
except Exception as e:
raise e
With PRAW 6 I had to make a couple of tweaks to get this to work. Thanks a lot to previous commenters for clues.
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent,
username=username,
password=password,
)
subreddit = reddit.subreddit("showerthoughts")
# go by timespan - 'hour', 'day', 'week', 'month', 'year', 'all'
# might need to go longer than an hour to get entries...
top_submissions = subreddit.top('hour')
n_sub = int(sys.argv[1]) if len(sys.argv) > 1 else 1
i = 0
while i < n_sub:
top_submission = next(top_submissions)
i += 1
top_post = top_submission.title
upvotes = []
downvotes = []
contents = []
for sub in top_submissions:
try:
ratio = sub.upvote_ratio
ups = int(round((ratio*sub.score)/(2*ratio - 1))
if ratio != 0.5 else round(sub.score/2))
upvotes.append(ups)
downvotes.append(ups - sub.score)
contents.append(sub.title)
except Exception as e:
continue
I should have mentioned that the limit
arg works in top()
, e.g.
top_submissions = subreddit.top('hour', limit=5)
top_submissions = subreddit.top('all', limit=20)
There appears to be a compatibility issue with
praw
or an undocumented config file that needs to get setup. When executing this cell:It gives this error.
I'm running version
praw==4.4.0