Probabilistic-programming-and-bayesian-methods-for-hackers: `praw` ๊ตฌ์„ฑ ๋˜๋Š” ๋ฒ„์ „ ๋ฌธ์ œ

์— ๋งŒ๋“  2017๋…„ 03์›” 20์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

praw ๋˜๋Š” ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์„œํ™”๋˜์ง€ ์•Š์€ ๊ตฌ์„ฑ ํŒŒ์ผ๊ณผ์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์…€์„ ์‹คํ–‰ํ•  ๋•Œ:

#adding a number to the end of the %run call with get the ith top post.
%run top_showerthoughts_submissions.py 2

print("Post contents: \n")
print(top_post)

์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
    842         try:
--> 843             d.update(self._sections[section])
    844         except KeyError:

KeyError: 'BayesianMethodsForHackers'

During handling of the above exception, another exception occurred:

NoSectionError                            Traceback (most recent call last)
/opt/notebooks/Chapter4_TheGreatestTheoremNeverTold/top_showerthoughts_submissions.py in <module>()
      7 
      8 
----> 9 reddit = praw.Reddit("BayesianMethodsForHackers")
     10 subreddit  = reddit.get_subreddit("showerthoughts")
     11 

/opt/conda/lib/python3.6/site-packages/praw/reddit.py in __init__(self, site_name, **config_settings)
    101         self._unique_counter = 0
    102         self.config = Config(site_name or os.getenv('praw_site') or 'DEFAULT',
--> 103                              **config_settings)
    104 
    105         required_message = ('Required configuration setting {!r} missing. \n'

/opt/conda/lib/python3.6/site-packages/praw/config.py in __init__(self, site_name, **settings)
     64 
     65         self._settings = settings
---> 66         self.custom = dict(Config.CONFIG.items(site_name), **settings)
     67 
     68         self.client_id = self.client_secret = self.oauth_url = None

/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
    844         except KeyError:
    845             if section != self.default_section:
--> 846                 raise NoSectionError(section)
    847         # Update with the entry specific variables
    848         if vars:

NoSectionError: No section: 'BayesianMethodsForHackers'

Post contents: 

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-be2145cbdc50> in <module>()
      3 
      4 print("Post contents: \n")
----> 5 print(top_post)

NameError: name 'top_post' is not defined

praw==4.4.0 ๋ฒ„์ „์„ ์‹คํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋„ค, ์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด PRAW์— ๋” ๋งŽ์€ ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์ด๊ฒƒ์„ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ๊ด‘์‚ฐ์€ ์ž˜ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

reddit = praw.Reddit(client_id=[my id],
                     client_secret=[my secret],
                     user_agent=[a random string explaining my 'app'],
                     username=[my reddit username],
                     password=[my reddit password)

๋” ๋งŽ์€ ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒƒ ์™ธ์—๋„ PRAW ๊ฐœ๋ฐœ์ž๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์ค‘ ์ผ๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์ผ๋ถ€ ํ•ญ๋ชฉ์„ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค(์ฆ‰, ์ด์ œ ์ œ์ถœ ๊ฐœ์ฒด์—์„œ upvote_ratio ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Œ).

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
        upvotes.append(sub.ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        raise e

๋ชจ๋“  4 ๋Œ“๊ธ€

๋‚˜๋Š” ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. praw API๊ฐ€ v4์—์„œ ํฌ๊ฒŒ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. praw 3.6.1์„ ์„ค์น˜ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ ์ตœ์‹  ๋ฒ„์ „์—์„œ ์ž‘๋™ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋„ค, ์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด PRAW์— ๋” ๋งŽ์€ ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์ด๊ฒƒ์„ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ๊ด‘์‚ฐ์€ ์ž˜ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

reddit = praw.Reddit(client_id=[my id],
                     client_secret=[my secret],
                     user_agent=[a random string explaining my 'app'],
                     username=[my reddit username],
                     password=[my reddit password)

๋” ๋งŽ์€ ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒƒ ์™ธ์—๋„ PRAW ๊ฐœ๋ฐœ์ž๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์ค‘ ์ผ๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์ผ๋ถ€ ํ•ญ๋ชฉ์„ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค(์ฆ‰, ์ด์ œ ์ œ์ถœ ๊ฐœ์ฒด์—์„œ upvote_ratio ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Œ).

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
        upvotes.append(sub.ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        raise e

PRAW 6์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๊ฒƒ์„ ์ž‘๋™์‹œํ‚ค๋ ค๋ฉด ๋ช‡ ๊ฐ€์ง€ ์กฐ์ •์„ ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํžŒํŠธ๋ฅผ ์ฃผ์‹  ์ด์ „ ๋Œ“๊ธ€ ์ž‘์„ฑ์ž์—๊ฒŒ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

reddit = praw.Reddit(
    client_id=client_id,
    client_secret=client_secret,
    user_agent=user_agent,
    username=username,
    password=password,
)
subreddit = reddit.subreddit("showerthoughts")

# go by timespan - 'hour', 'day', 'week', 'month', 'year', 'all'
# might need to go longer than an hour to get entries...
top_submissions = subreddit.top('hour')

n_sub = int(sys.argv[1]) if len(sys.argv) > 1 else 1

i = 0
while i < n_sub:
    top_submission = next(top_submissions)
    i += 1

top_post = top_submission.title

upvotes = []
downvotes = []
contents = []

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1))
                  if ratio != 0.5 else round(sub.score/2))
        upvotes.append(ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        continue

limit arg ๋Š” top() ์—์„œ ์ž‘๋™ํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด

top_submissions = subreddit.top('hour', limit=5)
top_submissions = subreddit.top('all', limit=20)
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰

๊ด€๋ จ ๋ฌธ์ œ

cledoux picture cledoux  ยท  13์ฝ”๋ฉ˜ํŠธ

Anjum48 picture Anjum48  ยท  8์ฝ”๋ฉ˜ํŠธ

jjisnow picture jjisnow  ยท  3์ฝ”๋ฉ˜ํŠธ

iamsiva11 picture iamsiva11  ยท  3์ฝ”๋ฉ˜ํŠธ

juntingzh picture juntingzh  ยท  3์ฝ”๋ฉ˜ํŠธ