r/Python • u/Krukerfluk • Jul 10 '20
I Made This This post has:
9777 upvotes,
967 downvotes
and 452 comments!
115
Jul 10 '20
Cool! Could you share it?
321
u/Krukerfluk Jul 10 '20
import praw reddit = praw.Reddit( client_id='***', client_secret='***', username='***', password='***', user_agent='***') while True: submission = reddit.submission(id='***') ratio = submission.upvote_ratio ups = round((ratio * submission.score) / (2 * ratio - 1)) if ratio != 0.5 else round(submission.score / 2) downs = ups - submission.score edited_body = str(ups) + ' upvotes,' + '\n\n' + str(downs) + ' downvotes' + "\n\n" "and " + \ str(submission.num_comments) + ' comments!' submission.edit(edited_body)I'm new to python so there is probably a better way to do this
158
u/Holek Jul 10 '20
add a sleep there for a minute or two just not to kill your API access
115
u/Krukerfluk Jul 10 '20
Just added an 90sec delay
thanks for the tip!
88
u/SpontaneousAge Jul 10 '20
90s isn't even necessary. 5s or something is fine as well, just continuously is bad. Reddit is pretty lean, but if you're too hardcore they will block you too.
→ More replies (4)19
33
u/throwaway_the_fourth Jul 10 '20
PRAW takes care of following the rate limit for you, so no need to add extra sleeps.
68
u/ManvilleJ Jul 10 '20 edited Jul 10 '20
github.
also replace that string concatenation with an f string and you don't need all string casting with this method and its the fastest & most readable way to do it
edited_body = f'{ups} upvotes, \n\n {downs} downvotes \n\n and {submission.num_comments} comments!'edit: fstrings are the fastest: https://realpython.com/python-f-strings/
34
u/__ah Jul 10 '20
f strings deserve to be wayyy more popular. Shame they only became a thing very recently in 3.6, so many tutorials won't have had it.
→ More replies (5)17
Jul 10 '20 edited Jul 10 '20
Kudos for the script. It's always fun to see live data :)
Here's my proposal. Didn't test everything since I don't have the credentials and stuff but it will give you the gist on how the design to transform it into a reusable CLI.
Thanks for sharing the source.
import os import argparse import praw CLIENT_ID = os.environ.get('CLIENT_ID') CLIENT_SECRET = os.environ.get('CLIENT_SECRET') USER_AGENT = os.environ.get('USER_AGENT') def get_reddit_client( username, password, client_id=None, client_secret=None, user_agent=None, ): if not client_id: client_id = CLIENT_ID if not client_secret: client_secret = CLIENT_SECRET if not user_agent: user_agent = USER_AGENT reddit = praw.Reddit( client_id=client_id, client_secret=client_secret, username=username, password=password, user_agent=user_agent) return reddit def main(args): args.username args.password reddit = get_reddit_client( args.username, args.password, args.client_id, args.client_secret, args.user_agent, ) while True: subm = reddit.submission(id=args.id) if subm.upvote_ratio != 0.5: ups = round( (subm.upvote_ratio * subm.score) / (2 * subm.upvote_ratio - 1)) else: ups = round(subm.score / 2) downs = ups - subm.score edited_body = ( '{} upvotes\n\n' '{} downvotes\n\n' '{} comments\n\n' ) edited_body = edited_body.format(ups, downs, subm.num_comments) subm.edit(edited_body) if __name__ == '__main__': parser = argparse.ArgumentParser( prog='reddit_stats', description='Track and Post reddit stats') parser.add_argument( 'id', type=str, help="reddit post's id") parser.add_argument( 'username', type=str, help="reddit's account username") parser.add_argument( 'password', type=str, help="reddit's account password") # Let user override values source from the environment variables parser.add_argument( '-ci', '--client_id', type=str, help="reddit's api client_id") parser.add_argument( '-cs', '--client_secret', type=str, help="reddit's api client_secret") parser.add_argument( '-ua', '--user_agent', type=str, help="custom user agent") args = parser.parse_args() main(args)Edit: Typo
→ More replies (13)9
u/CompSciSelfLearning Jul 10 '20
client_id='hunter2', client_secret='hunter2',username='hunter2', password='hunter2', user_agent='hunter2'
Am I doing this right?
→ More replies (15)12
Jul 10 '20
I would've used string formatting for the edited_body as it's more robust and looks cleaner but other that great! Thanks for sharing!
45
406
u/SpontaneousAge Jul 10 '20
Good job. But honestly, add a sleep timer of a few seconds. This will eventually get your IP banned on reddit if you bombard them with too many requests.
165
u/throwaway_the_fourth Jul 10 '20
The API wrapper (PRAW) takes care of that. No need to add additional sleeps.
36
u/keeldude Jul 10 '20
Unless the throttling algorithm is outlined in praw docs, my own personal style would be to manually set a reasonable wait period just to have more control. If the reddit api ever changes, there will be a delay to updated praw code getting pushed.
98
u/throwaway_the_fourth Jul 10 '20
Here's the code: https://github.com/praw-dev/prawcore/blob/34c153ec6706a87782898d404042ddbd5a847b57/prawcore/rate_limit.py
Reddit's API lets you know how many requests you have left and how much time you have until the limit resets. PRAW pretty much just divides time left by requests left and sleeps that amount. It does a little extra magic for detecting if multiple clients are running, but that's the jist of it.
→ More replies (1)18
u/UtilizedFestival Jul 10 '20
Client libraries that handle rate limits based on exponential back off or HTTP headers are super common these days. I wouldn't think twice of relying on these and not building my own
46
u/tunisia3507 Jul 10 '20
Next up: broadcast the ascii version of star wars by editing a reddit post at 30Hz.
7
u/SolarFlareWebDesign Jul 11 '20
That sounds amazing but I'm sure the api limiter is well below that, maybe 1 request per second
14
u/PyTec-Ari Jul 10 '20
Its editing a submission not posting. Thats what I wondered too though, would it eventually get rate limited. But I can't imagine editing a submission would trigger a rate limit. I don't actually know.
40
u/SpontaneousAge Jul 10 '20
That's still a request. Yes, it will get rate limited.
21
u/PyTec-Ari Jul 10 '20
Did some digging and apparently PRAW has inbuilt throttling to mitigate getting hit with rate limiting on requests. apparently.
→ More replies (3)→ More replies (2)5
u/Wilfred-kun Jul 10 '20
submission = reddit.submission(id='***')This line looks like a request to me. How else is he going to get the up-to-date stats? Also as the other poster said, submitting is still a request.→ More replies (4)
27
u/tacos2k Jul 10 '20
This is awesome. I want to just upvote it but I can't resist upvoting, refreshing, downvoting, refreshing ad infinitum.
14
u/unpeeledpotatoes Jul 10 '20
how often does it refresh? seems like it's changing quite a bit!
10
u/epicmindwarp Jul 10 '20
It runs a pull every X seconds then updates the message.
OP posted the code higher up.
106
u/vidazinho Jul 10 '20 edited Oct 23 '20
This comment has:
42 characters
1 colon
101 upvotes
→ More replies (7)
5
4
4
1
u/JanStreams Jul 10 '20 edited Jul 10 '20
Wait if you create a statistic from the amount of karma and the amount of upvotes/downvotes, over time maybe you could find out the sacred karma algorithm..
2
u/Krukerfluk Jul 10 '20
Somebody already done it here
3
Jul 10 '20
Joke's on you, I middle-clicked it so I saw the title of the page before actually going on it haha
I still listened to it out of respect
3
2
2
u/AluminiumChopsticks Jul 10 '20
I wonder why the real upvote net total is 1 more than the displayed message. Did you round up or down somewhere?
2
Jul 10 '20
I am gonna be that guy but Is this open sourced? I wanna see how other people make reddit bots
→ More replies (1)
2
u/whaiy Oct 18 '20
Just checking if the comment number goes up
Edit: this is me 2 mins after posting original comment and it's still didn't go up.
3
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
u/-_-qarmah-_- Jul 10 '20
I was gonna do this but couldn't figure out how to make a bot take over my acc, could you share the code?
→ More replies (1)
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
u/manlyman1417 Jul 10 '20
Here I am toggling the upvote and downvote buttons to see it change. I love this
1
u/l33tIsSuperpower Jul 10 '20
yo who are the 300-350 ish people downvoting this? this is fucking cool
1
u/spazz_monkey Jul 10 '20
Are you just scraping the page every so often and edit the post? Can you edit posts.....I dont think you can.
1
1
1
u/T_house92 Jul 10 '20
I hate that the tester in me had to upvote and down vote multiple times to watch the counts. Definitely not commenting to see the count jump to 150 either.
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
u/xxsurajbxx Jul 10 '20 edited Jul 10 '20
am i the only one who sees a higher number of upvotes in the post than the amount that the reddit app shows me edit: forget is, i forgot to subtract downvotes
1
1
1
1
1
u/Jehovahswetnips Jul 10 '20
This feel oddly simular to braking the 4th wall, but this situation is not called that... unless it is? Cyber wall? breaking the Cyber wall? I don't know. It's cool though.
1
1
1
1
1
1
1
1
1
1
1
1
u/rhinosyphilis Jul 10 '20
So, my app is showing 4.2k upvotes, but your script has 4762 upvotes and 589 down. Are you counting total votes instead of upvotes?
2
u/Mr_Mustache21 Jul 10 '20
The 4.2k was them added together. Separately it has 4762 upvotes and 589 downvotes.
→ More replies (1)
1
1
1
1
1
1
1
1
1
Jul 10 '20
Hey dude, some asshole stole this and put it in r/teenagers. But I like this idea, and this is awesome!
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
u/StarkillerX42 Jul 10 '20
Here I am, trying to figure out if I should update cuz it's cool, or downvote to test it
1
1
u/I_Say_Fool_Of_A_Took Jul 10 '20
I thought that reddit didnt disclose the ratio and obfuscated the karma number?
1
1
1
u/Emotional-Newspaper Jul 10 '20
Reddit should start showing number of downvotes it feels Facebook type platform without that
1.3k
u/[deleted] Jul 10 '20 edited Sep 22 '23
scale tap piquant quiet advise salt languid abundant dolls long -- mass edited with redact.dev