GithubHelp home page GithubHelp logo

Comments (2)

jadm333 avatar jadm333 commented on July 18, 2024 1

This is because the code is written for a deprecated API.
You have two choices in order to make this to work.

  1. Modify the reddit.py script. You need to change a couple of function name and add a client_id and a client_secret. This two tokens are generated in the reddit app page (https://github.com/reddit-archive/reddit/wiki/OAuth2). This is the one I used.
  2. This solutions it's the easiest one. You can copy the reddit.py script from another folder of the repo, because that problem i'ts already fixed. I notice this when I advance the book.

Hope it helps!
PD: I attach the script I used.
reddit.py.txt

from practical-docker-with-python.

zoldaten avatar zoldaten commented on July 18, 2024
#file reddit.py
#make app on reddit first to get keys

import praw
from states import log

#https://www.reddit.com/wiki/api
#https://www.reddit.com/prefs/apps
#https://stackoverflow.com/questions/39521621/raise-clientexceptionrequired-message-formatattribute-praw-exceptions-client

def get_latest_news(sub_reddits):
    log.debug('Fetching news from reddit')
    #r = praw.Reddit(user_agent='Practical Docker With Python tutorial')
    r = praw.Reddit(
        username="user_name_on_reddit",
        password="password_on_rediit",
        client_id="see stackoverflow at the beginning",
        client_secret="see stackoverflow at the beginning",
        user_agent="Karma breakdown 1.0 by /u/zoldaten_",
        redirect_uri='http://127.0.0.1:65010/',
    )
    
    sub_reddits = clean_up_subreddits(sub_reddits)
    log.debug(f"Fetching subreddits: {sub_reddits}")    
    submissions = r.subreddit(sub_reddits).top(limit=5)
    submission_content = ''
    try:
        for post in submissions:
            submission_content += f"{post.title} - {post.url} \n\n"
    except praw.errors.Forbidden:
            log.info(f"subreddit {sub_reddits} is private".format())
            submission_content = "Sorry couldn't fetch; subreddit is private"
    except praw.errors.InvalidSubreddit:
            log.info(f"Subreddit {sub_reddits} is invalid or doesn''t exist.")
            submission_content = "Sorry couldn't fetch; subreddit doesn't seem to exist"
    except praw.errors.NotFound :
            log.info(f"Subreddit {sub_reddits} is invalid or doesn''t exist.")
            submission_content = "Sorry couldn't fetch; something went wrong, please do send a report to @sathyabhat"
    return submission_content
    
def clean_up_subreddits(sub_reddits):
    log.debug('Got subreddits to clean: {0}'.format(sub_reddits))
    return sub_reddits.strip().replace(" ", "").replace(',', '+')

from practical-docker-with-python.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.