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.
Hmm, but if the rate limit is, let's say, 100 calls in 15m, then praw will probably let you do 100 calls in 30s, and then lock you out for the remaining 14m, right?
Still good to have reasonable sleep regardless. There's no point in updated every second.
It's actually pretty smart! The rate limit is 600 requests in 10 minutes, and PRAW chooses how long to sleep such that the requests will be evenly spread out across the timeframe.
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
I'm finishing a Codecademy course, and learned fstring outside of it. I've bashed my head against their interfaces a few times thinking something was wrong with my fstring, when in reality they are running a lower python version. : (
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)
It is a nice project that makes adding CLI capabilities simple and easy. I prefer the developer efficiency. Should I use urllib instead of requests? maybe, but if it works and I don't have to think about it, good.
is a package that collects several modules for working with URLs
You will notice that is not their goal to help you consume web services. urllibs consume you manipulate url and web requests and a "raw" way. Requests project had a different goal. Two projects, same scenario, different scopes and goals.
321
u/Krukerfluk Jul 10 '20
I'm new to python so there is probably a better way to do this