Skip to content

Commit

Permalink
Merge pull request #8 from praw-dev/trophy_example
Browse files Browse the repository at this point in the history
Simplify trophies example in README.rst.
  • Loading branch information
bboe committed Feb 14, 2016
2 parents 24d4fc2 + 764ace9 commit 3b8b042
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
40 changes: 14 additions & 26 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,27 @@ appropriate values for your application.
#!/usr/bin/env python
import os
import pprint
import prawcore
import sys
from pprint import pprint
authenticator = prawcore.Authenticator(
os.environ.get('PRAWCORE_CLIENT_ID'),
os.environ.get('PRAWCORE_CLIENT_SECRET'))
authorizer = prawcore.ReadOnlyAuthorizer(authenticator)
authorizer.refresh()
def main():
if len(sys.argv) != 2:
print('Usage: {} USERNAME'.format(sys.argv[0]))
return 1
user = sys.argv[1]
url = 'https://oauth.reddit.com/api/v1/user/bboe/trophies'
with prawcore.session(authorizer) as session:
pprint.pprint(session.request('GET', url))
authenticator = prawcore.Authenticator(
os.environ.get('PRAWCORE_CLIENT_ID'),
os.environ.get('PRAWCORE_CLIENT_SECRET'))
url = 'https://oauth.reddit.com/api/v1/user/{}/trophies'.format(user)
authorizer = prawcore.ReadOnlyAuthorizer(authenticator)
authorizer.refresh()
with prawcore.session(authorizer) as session:
pprint(session.request('GET', url))
return 0
if __name__ == '__main__':
sys.exit(main())
Save the above as ``trophies.py`` and then run via:
Save the above as ``trophies.py`` and then execute via:

.. code-block:: bashsession
python trophies.py USERNAME
python trophies.py
Additional examples can be found at:
https://github.com/praw-dev/prawcore/tree/master/examples


Depending on prawcore
Expand Down
40 changes: 40 additions & 0 deletions examples/trophies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

"""trophies.py is a simple program to obtain a user's list of trophies.
This program demonstrates the use of ``prawcore.ReadOnlyAuthorizer`` that does
not require an access token to make authenticated requests to reddit.
"""
import os
import prawcore
import sys


def main():
"""main is the program's entry point when directly executed."""
if len(sys.argv) != 2:
print('Usage: {} USERNAME'.format(sys.argv[0]))
return 1

authenticator = prawcore.Authenticator(
os.environ.get('PRAWCORE_CLIENT_ID'),
os.environ.get('PRAWCORE_CLIENT_SECRET'))
authorizer = prawcore.ReadOnlyAuthorizer(authenticator)
authorizer.refresh()

user = sys.argv[1]
url = 'https://oauth.reddit.com/api/v1/user/{}/trophies'.format(user)
with prawcore.session(authorizer) as session:
data = session.request('GET', url)

for trophy in data['data']['trophies']:
description = trophy['data']['description']
print(trophy['data']['name'] +
(' ({})'.format(description) if description else ''))

return 0


if __name__ == '__main__':
sys.exit(main())

0 comments on commit 3b8b042

Please sign in to comment.