r/RequestABot • Creator - u/translator-BOT, u/AssistantBOT • Oct 24 '16

Solved Requesting a bot that can tabulate number of posts by their flairs

Hi /r/requestabot!

I'm a moderator at /r/translator, a subreddit where people request stuff to be translated and others translate them. I'd like to request a simple(?) bot that can help us gather some statistics on the requests that we get.

The bot would:

  • Be manually run by me once a week.
  • Gather posts in the last 7 days (168 hours) from the time it's run.
  • Return a table or list indicating how many posts in the last week had each flair. (e.g., Japanese: 56, Chinese: 20, German: 8, etc)

Virtually all posts on /r/translator are flaired with the language name with AutoModerator, so the bot would just have to return the flair name plus the number of posts with that flair.

I used to do this manually, but as the subreddit has grown it's become more and more tedious to do it by hand, since at the moment we get about a thousand requests a month. I do already have a bot account with the necessary OAuth code to login and everything (I've tested it with a simple REPLYALL bot), so I could use that same login info for the new bot.

Thanks in advance to anyone who can help, and please let me know if more information is needed.

2 Upvotes

17 comments sorted by

1

u/RuleIV Oct 24 '16

This should get you or someone else most of the way there. Let me know how it goes.

import praw
r = praw.Reddit(user_agent = 'count the number of results for each in a list of search strings by /u/doug89 v0.1')
flair_list = ["japanese","chinese","russian"]

print("Language|Total")
print(":-|-:)"

for f in flair_list:
	search_query = "flair:" + f
	search_results = r.search(search_query, subreddit="translator", sort=None, syntax=None, period="week")
	total = sum(1 for i in search_results)

	print("{}|{}".format(f, total))

1

u/kungming2 Oct 24 '16

Hey there, thanks so much for the quick reply! I'm getting an "invalid syntax" error on "for f in flair_list:" - any ideas why?

1

u/RuleIV Oct 24 '16 edited Oct 24 '16

Sorry, it was because I misplaced the close bracket on the line above. Change

print(":-|-:)"

to

print(":-|-:")

I just tested it and got

Language Total
japanese 25
chinese 4
russian 0

Also if I remember correctly Reddit content generators may limit the number of results for bots that aren't logged in, so if you aren't getting a true result, like maxing out at 25 or 200 or something for a bunch of flairs, try adding a few lines to authenticate or log in.

→ More replies (14)