Skip to content

Commit

Permalink
Merge pull request #733 from nmtake/live-now
Browse files Browse the repository at this point in the history
Add LiveHelper.now
  • Loading branch information
bboe authored Feb 16, 2017
2 parents fcc5d9e + bf57b67 commit 66b6683
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Unreleased
invite permissions for a redditor.
* :meth:`.LiveThread.discussions` to get submissions linking to the thread.
* :meth:`.LiveThread.report` to report the thread violating the Reddit rules.
* :meth:`.LiveHelper.now` to get the currently featured live thread.

**Fixed**

Expand Down
1 change: 1 addition & 0 deletions praw/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
'live_discussions': 'live/{id}/discussions',
'live_invite': 'api/live/{id}/invite_contributor',
'live_leave': 'api/live/{id}/leave_contributor',
'live_now': 'api/live/happening_now',
'live_remove_update': 'api/live/{id}/delete_update',
'live_remove_contrib': 'api/live/{id}/rm_contributor',
'live_remove_invite': 'api/live/{id}/rm_contributor_invite',
Expand Down
15 changes: 15 additions & 0 deletions praw/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ def create(self, title, description=None, nsfw=False, resources=None):
'description': description, 'nsfw': nsfw, 'resources': resources,
'title': title})

def now(self):
"""Get the currently featured live thread.
:returns: The :class:`.LiveThread` object, or ``None`` if there is
no currently featured live thread.
Usage:
.. code-block:: python
thread = reddit.live.now() # LiveThread object or None
"""
return self._reddit.get(API_PATH['live_now'])


class MultiredditHelper(PRAWBase):
"""Provide a set of functions to interact with Multireddits."""
Expand Down
5 changes: 5 additions & 0 deletions praw/objector.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ def objectify(self, data):
"""Create RedditBase objects from data.
:param data: The structured data.
:returns: An instance of :class:`~.RedditBase`, or ``None`` if
given ``data`` is ``None``.
"""
# pylint: disable=too-many-return-statements
if data is None: # 204 no content
return
if isinstance(data, list):
return [self.objectify(item) for item in data]
if 'kind' in data and data['kind'] in self.parsers:
Expand Down
108 changes: 108 additions & 0 deletions tests/integration/cassettes/TestReddit.test_live_now__no_featured.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"http_interactions": [
{
"recorded_at": "2017-02-16T16:11:04",
"request": {
"body": {
"encoding": "utf-8",
"string": "grant_type=client_credentials"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "identity",
"Authorization": "Basic <BASIC_AUTH>",
"Connection": "keep-alive",
"Content-Length": "29",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "<USER_AGENT> PRAW/4.3.1.dev0 prawcore/0.8.0"
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": "bytes",
"Connection": "keep-alive",
"Content-Length": "105",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Thu, 16 Feb 2017 16:11:07 GMT",
"Server": "snooserv",
"Set-Cookie": "loid=dzEJwqV1Xbmbp1TxVQ; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Moose": "majestic",
"X-Served-By": "cache-nrt6129-NRT",
"X-Timer": "S1487261467.715573,VS0,VE198",
"cache-control": "max-age=0, must-revalidate",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"recorded_at": "2017-02-16T16:11:04",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "identity",
"Authorization": "bearer <ACCESS_TOKEN>",
"Connection": "keep-alive",
"Cookie": "loid=dzEJwqV1Xbmbp1TxVQ; loidcreated=1487261467000",
"User-Agent": "<USER_AGENT> PRAW/4.3.1.dev0 prawcore/0.8.0"
},
"method": "GET",
"uri": "https://oauth.reddit.com/api/live/happening_now?raw_json=1"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": ""
},
"headers": {
"Accept-Ranges": "bytes",
"Connection": "keep-alive",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Thu, 16 Feb 2017 16:11:08 GMT",
"Server": "snooserv",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Moose": "majestic",
"X-Served-By": "cache-nrt6134-NRT",
"X-Timer": "S1487261468.054475,VS0,VE182",
"access-control-allow-origin": "*",
"access-control-expose-headers": "X-Reddit-Tracking, X-Moose",
"cache-control": "max-age=0, must-revalidate",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-reddit-tracking": "https://pixel.redditmedia.com/pixel/of_destiny.png?v=YcwQRLyZ%2FuT7JwqZptyf4PztjeDdfFxNougyY9av%2FH%2B26sw7qFkqskiV4emqCNcSfDoOKBcPHnOFNkTAa4tO9giGNShdTHde",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 204,
"message": "No Content"
},
"url": "https://oauth.reddit.com/api/live/happening_now?raw_json=1"
}
}
],
"recorded_with": "betamax/0.8.0"
}
5 changes: 5 additions & 0 deletions tests/integration/test_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def test_live_create(self, _):
assert isinstance(live, LiveThread)
assert live.title == 'PRAW Create Test'

def test_live_now__no_featured(self):
with self.recorder.use_cassette('TestReddit.test_live_now'
'__no_featured'):
assert self.reddit.live.now() is None

def test_random_subreddit(self):
names = set()
with self.recorder.use_cassette(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_objector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import UnitTest


class TestObjector(UnitTest):

def test_objectify_returns_None_for_None(self):
assert self.reddit._objector.objectify(None) is None

0 comments on commit 66b6683

Please sign in to comment.