r/Python • • Jul 10 '20

I Made This This post has:

9777 upvotes,

967 downvotes

and 452 comments!

9.2k Upvotes

429 comments sorted by

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

486

u/Krukerfluk Jul 10 '20

yep

86

u/Jac0b_0 Jul 10 '20

Adding number of cross posts would be cool

46

u/steampunkgibbon Jul 10 '20

would be levels more challenging too

→ More replies (4)
→ More replies (1)

68

u/Belzedan Jul 10 '20

Also carykh's video where the thumbnail is the most upvoted comment of the last 24 hours!

29

u/ahmed3618 Jul 10 '20

This reminds me of another Tom Scott video where the likes on a tweet keep going up and down, because thats what's happening.

https://youtu.be/RY_2gElt3SA

7

u/Folaefolc Jul 10 '20

But aren't very big systems built with a reliable way to avoid that?

29

u/notquiteaplant Jul 10 '20

What makes a system "very big" is that it's spread across hundreds of machines. If 500 machines each process a sliver of the likes on a video, and you want to get a 100% accurate tally, you'd have to stop all 500 of them to ask them how many likes they saw, which defeats the purpose of having many machines in the first place. Instead they use eventual consistency: the answer is always close enough, and once everything calms down it'll be exactly correct.

For a less simplified answer, watch the video

→ More replies (1)
→ More replies (1)
→ More replies (1)

115

u/[deleted] 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

u/Turtvaiz Jul 10 '20

PRAW throttles requests based on response headers

→ More replies (2)

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

u/[deleted] 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?

12

u/[deleted] 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

u/discobrisco Jul 10 '20

Nah f strings are the only way to go.

→ More replies (6)
→ More replies (15)

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)

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)
→ More replies (2)

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

u/Epicduck_ Jul 10 '20

I just want to make to comments go up by one with this comment. Nice post

4

u/ItsBarney01 Jul 11 '20

I think it broke

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

u/[deleted] 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

u/[deleted] Jul 10 '20

I wonder how many refreshes it has...

2

u/D3vilM4yCry Jul 10 '20

Testing for 54 comments,,,

EDIT: It Worked!

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

u/[deleted] 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.

2

u/nobody01810 Jul 10 '20

Wow! Good job.

1

u/[deleted] Jul 10 '20

13

1

u/[deleted] Jul 10 '20

Add one

1

u/ItsLoren Jul 10 '20

Very cool thing, nice job

1

u/[deleted] Jul 10 '20

Well done

1

u/[deleted] Jul 10 '20

this is dope!

1

u/RheingoldRiver Jul 10 '20

haha this is cute! nice job :)

1

u/[deleted] Jul 10 '20

Test

1

u/[deleted] Jul 10 '20

35?

1

u/RetroPenguin_ Jul 10 '20

It’s super wrong now :)

1

u/crazy_bean Jul 10 '20

So cool OP! How long did this take you to build?

1

u/kokoseij Jul 10 '20

You're lying to me, this post has 697 upvotes!!

/s

1

u/[deleted] Jul 10 '20

+1 to the comment count!

1

u/[deleted] Jul 10 '20

V.Nice

1

u/MahdeenSky Jul 10 '20

Hmm +1 comment

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

u/nikkhil04 Jul 10 '20

Hi ,what does praw do?

2

u/bcgroom Jul 10 '20

Python reddit api wrapper

1

u/[deleted] Jul 10 '20

84th comment. Let's see if the post shows this...

1

u/Drogen24 Jul 10 '20

I down voted to see the number but it didn't change so keep my down vote

1

u/[deleted] Jul 10 '20

Comment

1

u/warhawk128 Jul 10 '20

Wow dude! Great job. Keep up the creativity and good work

1

u/[deleted] Jul 10 '20

Commenting just to test it

1

u/KC1MML Jul 10 '20

Testing

1

u/yabiggle Jul 10 '20

Guessing this is a python script also commenting to see how fast it is

1

u/Frasse04 Jul 10 '20

How did you do this?

1

u/PsychedelicPistachio Jul 10 '20

I just wanna see the comment count go up

1

u/NemPlayer Jul 10 '20

141 comments*

1

u/[deleted] Jul 10 '20

neato

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

u/[deleted] Jul 10 '20

Yes

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

u/mightymander Jul 10 '20

This is sick

1

u/8bitbiochemist Jul 10 '20

Okay this is pretty cool

1

u/[deleted] Jul 10 '20

Nice

1

u/[deleted] Jul 10 '20

lol

1

u/[deleted] Jul 10 '20

Fancy

1

u/ahmetkedira Jul 10 '20

No there isnt

1

u/anewman513 Jul 10 '20

Who would downvote this!? Seriously, explain yourselves downvoters

1

u/SirRupertt Jul 10 '20

How do you go about doing this? I want to Learn a little :)

1

u/RecycledCap Jul 10 '20

Nice, I wanna see the counter go up

1

u/ninjadude251 Jul 10 '20

What happens if you get an award?

1

u/Soofadalooka Jul 10 '20

178th comment gang

1

u/Sr_McSpank Jul 10 '20

Adding a comment to make sure it changes

→ More replies (1)

1

u/[deleted] Jul 10 '20

Wow

1

u/[deleted] Jul 10 '20

187th comment

1

u/swimshadyfsoc Jul 10 '20

lets check how fast it updates itself

1

u/swimshadyfsoc Jul 10 '20

The question is can it detect fake downvotes?

1

u/dolotasinfinity Jul 10 '20

Can I do this on facebook?

1

u/CotoCoutan Jul 10 '20

hohoho!!! This is amazing! Bringing up the comment count to 199!

1

u/[deleted] Jul 10 '20

Let me test something

1

u/galian99 Jul 10 '20

A coment to see if the counter change

1

u/SaskuAc3 Jul 10 '20

This is very nice.

1

u/[deleted] Jul 10 '20

210 comment

1

u/[deleted] Jul 10 '20

test comment

1

u/Zin_Rein Jul 10 '20

Numbers!!!

1

u/[deleted] Jul 10 '20

This is really ducking cool.

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

u/sfasu77 Jul 10 '20

222 ya punk

1

u/[deleted] Jul 10 '20

No it doesn't

1

u/[deleted] Jul 10 '20

THAT IS SO COOL

But how does it work... It doesn't say edited on it...

→ More replies (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

u/[deleted] Jul 10 '20

#

1

u/Professionalname28 Jul 10 '20

Wow! Very cool but uh the upvotes are wrong

→ More replies (1)

1

u/thekalmanfilter Jul 10 '20

Is this live??

1

u/[deleted] Jul 10 '20

Comment

1

u/vivek_seth Jul 10 '20

Just a test

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

u/CozyThurifer Jul 10 '20

Who would’ve thought

1

u/[deleted] Jul 10 '20

how do you upload this to reddit

1

u/[deleted] Jul 10 '20

u/Visfire stole this and put it on r/teenagers

1

u/[deleted] Jul 10 '20

261 comments now.

1

u/[deleted] 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

u/Almoondddd Jul 10 '20

This is epic

1

u/ProRacer27156 Jul 10 '20

This is cool

1

u/1920sBusinessMan Jul 10 '20

Dat awkward moment when I leave a comment so the post is inaccurate

1

u/Not_owo Jul 10 '20

Cool bot dude!

1

u/Saad-Ali Jul 10 '20

278 now?

1

u/[deleted] Jul 10 '20

Test

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

u/BallFlavin Jul 10 '20

That actually really cool. We can see how upvotes are fudged too

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

u/Emotional-Newspaper Jul 10 '20

Reddit should start showing number of downvotes it feels Facebook type platform without that