redditwarp


Nameredditwarp JSON
Version 1.2.0 PyPI version JSON
download
home_page
SummaryA library for interacting with the Reddit API.
upload_time2023-08-29 13:18:55
maintainer
docs_urlNone
authorPyprohly
requires_python>=3.8,<4.0
licenseMIT
keywords reddit api wrapper
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# RedditWarp

[![Latest version](https://img.shields.io/pypi/v/redditwarp)](https://pypi.org/p/redditwarp/)
![Supported Python versions](https://img.shields.io/pypi/pyversions/redditwarp)
[![Discord guild](https://img.shields.io/discord/1084793643673600062?logo=discord&logoColor=white&logoWidth=20&label=&labelColor=5865F2&color=7289DA)](http://pyprohly.github.io/redditwarp/discord-invite)

A comprehensive, type-complete, easy-to-learn Python Reddit API wrapper.

RedditWarp is a Python library that simplifies working with the Reddit API.
It handles the complexities of the Reddit API in a way that is comprehensive,
highly discoverable, and static-type conscious. It includes a variety of useful
tools to facilitate a wide range of use cases, and even provides tools for
accessing the API in ways previously not possible.

Look how easy it is to use:

```python
import redditwarp.SYNC

client = redditwarp.SYNC.Client()

it = client.p.front.pull.hot(5)
l = list(it)
for subm in l:
    print("r/{0.subreddit.name} | {0.id36}+ ^{0.score} | {0.title!r:.80}".format(subm))
```

## Features

* A consistent and easy-to-use programming interface.
* Modern type-complete codebase.
* Sync and asynchronous IO support.
* Automatic rate limit handling.
* A comprehensive and discoverable index of API procedures.
* Model classes that are fully typed and sensibly wrap API structures.
* Formal data structures to facilitate comment tree traversals and pagination.
* HTTP transport library agnosticism.
* OAuth2 tooling and CLI utilities to help manage auth tokens.
* A simple event-based listing endpoint streaming framework.

## Installation

**Requires Python 3.8+.**
Type annotations may use 3.9 features.
Code examples will assume 3.10.

Install/update:

    pip install -U redditwarp

## Demonstration

<details open>
  <summary>Examples</summary>

```python
import redditwarp.SYNC

client = redditwarp.SYNC.Client()

# Display the first 5 submissions on the Reddit frontpage.
it = client.p.front.pull.hot(5)
l = list(it)
for subm in l:
    print("r/{0.subreddit.name} | {0.id36}+ ^{0.score} | {0.title!r:.80}".format(subm))

# How many subscribers does r/Python have?
subr = client.p.subreddit.fetch_by_name('Python')
print(subr.subscriber_count)

# Display the top submission of the week in the r/YouShouldKnow subreddit.
m = next(client.p.subreddit.pull.top('YouShouldKnow', amount=1, time='week'))
print(f'''\
{m.permalink}
{m.id36}+ ^{m.score} | {m.title}
Submitted {m.created_at.astimezone().ctime()}{' *' if m.is_edited else ''} \
by u/{m.author_display_name} to r/{m.subreddit.name}
''')

# Get the first comment of a submission.
tree_node = client.p.comment_tree.fetch('uc8i1g', sort='top', limit=1)
c = tree_node.children[0].value
print(f'''\
{c.submission.id36}+{c.id36} ^{c.score}
u/{c.author_display_name} says:
{c.body}
''')

# List the moderators of r/redditdev.
it1 = client.p.moderation.pull_users.moderators('redditdev')
for mod in it1:
    print(mod.name)
```

</details>

<details>
  <summary>More examples</summary>

```python
# ...

# Need credentials for these next few API calls.
CLIENT_ID = '...'
CLIENT_SECRET = '...'
REFRESH_TOKEN = '...'
client1 = redditwarp.SYNC.Client(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)

# Who am I?
me = client1.p.account.fetch()
print(f"Hello u/{me.name}!")

# Show my last 5 comments.
it2 = client.p.user.pull.comments(me.name, 5)
for comm in it2:
    print('###')
    print(comm.body)

# Show my last 10 saved items.
from redditwarp.models.submission_SYNC import Submission
from redditwarp.models.comment_SYNC import Comment
it3 = client1.p.user.pull.saved(me.name, 10)
l = list(it3)
for obj in l:
    print('###')
    match obj:
        case Submission() as m:
            print(f'''\
{m.permalink}
{m.id36}+ ^{m.score} | {m.title}
Submitted {m.created_at.astimezone().ctime()}{' *' if m.is_edited else ''} \
by u/{m.author_display_name} to r/{m.subreddit.name}
''')
        case Comment() as c:
            print(f'''\
{c.permalink}
{c.submission.id36}+{c.id36} ^{c.score}
u/{c.author_display_name} says:
{c.body}
''')

# Submit a link post to r/test.
subm_id = client1.p.submission.create_link_post('test',
        "Check out this cool website", "https://www.reddit.com")

# Reply to a submission.
from redditwarp.util.extract_id_from_url import extract_submission_id_from_url
idn = extract_submission_id_from_url("https://www.reddit.com/comments/5e1az9")
comm1 = client1.p.submission.reply(idn, "Pretty cool stuff!")

# Delete the post and the comment reply.
client1.p.submission.delete(subm_id)
client1.p.comment.delete(comm1.id)
```

</details>

<details>
  <summary>Streaming example</summary>

```python
#!/usr/bin/env python
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from redditwarp.models.submission_ASYNC import Submission

import asyncio

import redditwarp.ASYNC
from redditwarp.streaming.makers.subreddit_ASYNC import create_submission_stream
from redditwarp.streaming.ASYNC import flow

async def main() -> None:
    client = redditwarp.ASYNC.Client()
    async with client:
        submission_stream = create_submission_stream(client, 'AskReddit')

        @submission_stream.output.attach
        async def _(subm: Submission) -> None:
            print(subm.id36, '~', subm.title)

        @submission_stream.error.attach
        async def _(exc: Exception) -> None:
            print('ERROR:', repr(exc))

        await flow(submission_stream)

asyncio.run(main())
```

</details>

## Support

Post any questions you have to [r/redditdev].

[r/redditdev]: https://www.reddit.com/r/redditdev/

Join the conversation in the RedditWarp [Discord guild].

[Discord guild]: http://pyprohly.github.io/redditwarp/discord-invite

## Links

* [Repository](https://github.com/Pyprohly/redditwarp)
* [PyPI](https://pypi.org/p/redditwarp/)
* [Documentation](https://redditwarp.readthedocs.io/)
* [r/redditdev]
* [Discord guild]

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "redditwarp",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "reddit,api,wrapper",
    "author": "Pyprohly",
    "author_email": "pyprohly@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/de/24/5ab5217906c6e658bf292dd46c4cb95d8725644a2312835aaae7c27defef/redditwarp-1.2.0.tar.gz",
    "platform": null,
    "description": "\n# RedditWarp\n\n[![Latest version](https://img.shields.io/pypi/v/redditwarp)](https://pypi.org/p/redditwarp/)\n![Supported Python versions](https://img.shields.io/pypi/pyversions/redditwarp)\n[![Discord guild](https://img.shields.io/discord/1084793643673600062?logo=discord&logoColor=white&logoWidth=20&label=&labelColor=5865F2&color=7289DA)](http://pyprohly.github.io/redditwarp/discord-invite)\n\nA comprehensive, type-complete, easy-to-learn Python Reddit API wrapper.\n\nRedditWarp is a Python library that simplifies working with the Reddit API.\nIt handles the complexities of the Reddit API in a way that is comprehensive,\nhighly discoverable, and static-type conscious. It includes a variety of useful\ntools to facilitate a wide range of use cases, and even provides tools for\naccessing the API in ways previously not possible.\n\nLook how easy it is to use:\n\n```python\nimport redditwarp.SYNC\n\nclient = redditwarp.SYNC.Client()\n\nit = client.p.front.pull.hot(5)\nl = list(it)\nfor subm in l:\n    print(\"r/{0.subreddit.name} | {0.id36}+ ^{0.score} | {0.title!r:.80}\".format(subm))\n```\n\n## Features\n\n* A consistent and easy-to-use programming interface.\n* Modern type-complete codebase.\n* Sync and asynchronous IO support.\n* Automatic rate limit handling.\n* A comprehensive and discoverable index of API procedures.\n* Model classes that are fully typed and sensibly wrap API structures.\n* Formal data structures to facilitate comment tree traversals and pagination.\n* HTTP transport library agnosticism.\n* OAuth2 tooling and CLI utilities to help manage auth tokens.\n* A simple event-based listing endpoint streaming framework.\n\n## Installation\n\n**Requires Python 3.8+.**\nType annotations may use 3.9 features.\nCode examples will assume 3.10.\n\nInstall/update:\n\n    pip install -U redditwarp\n\n## Demonstration\n\n<details open>\n  <summary>Examples</summary>\n\n```python\nimport redditwarp.SYNC\n\nclient = redditwarp.SYNC.Client()\n\n# Display the first 5 submissions on the Reddit frontpage.\nit = client.p.front.pull.hot(5)\nl = list(it)\nfor subm in l:\n    print(\"r/{0.subreddit.name} | {0.id36}+ ^{0.score} | {0.title!r:.80}\".format(subm))\n\n# How many subscribers does r/Python have?\nsubr = client.p.subreddit.fetch_by_name('Python')\nprint(subr.subscriber_count)\n\n# Display the top submission of the week in the r/YouShouldKnow subreddit.\nm = next(client.p.subreddit.pull.top('YouShouldKnow', amount=1, time='week'))\nprint(f'''\\\n{m.permalink}\n{m.id36}+ ^{m.score} | {m.title}\nSubmitted {m.created_at.astimezone().ctime()}{' *' if m.is_edited else ''} \\\nby u/{m.author_display_name} to r/{m.subreddit.name}\n''')\n\n# Get the first comment of a submission.\ntree_node = client.p.comment_tree.fetch('uc8i1g', sort='top', limit=1)\nc = tree_node.children[0].value\nprint(f'''\\\n{c.submission.id36}+{c.id36} ^{c.score}\nu/{c.author_display_name} says:\n{c.body}\n''')\n\n# List the moderators of r/redditdev.\nit1 = client.p.moderation.pull_users.moderators('redditdev')\nfor mod in it1:\n    print(mod.name)\n```\n\n</details>\n\n<details>\n  <summary>More examples</summary>\n\n```python\n# ...\n\n# Need credentials for these next few API calls.\nCLIENT_ID = '...'\nCLIENT_SECRET = '...'\nREFRESH_TOKEN = '...'\nclient1 = redditwarp.SYNC.Client(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)\n\n# Who am I?\nme = client1.p.account.fetch()\nprint(f\"Hello u/{me.name}!\")\n\n# Show my last 5 comments.\nit2 = client.p.user.pull.comments(me.name, 5)\nfor comm in it2:\n    print('###')\n    print(comm.body)\n\n# Show my last 10 saved items.\nfrom redditwarp.models.submission_SYNC import Submission\nfrom redditwarp.models.comment_SYNC import Comment\nit3 = client1.p.user.pull.saved(me.name, 10)\nl = list(it3)\nfor obj in l:\n    print('###')\n    match obj:\n        case Submission() as m:\n            print(f'''\\\n{m.permalink}\n{m.id36}+ ^{m.score} | {m.title}\nSubmitted {m.created_at.astimezone().ctime()}{' *' if m.is_edited else ''} \\\nby u/{m.author_display_name} to r/{m.subreddit.name}\n''')\n        case Comment() as c:\n            print(f'''\\\n{c.permalink}\n{c.submission.id36}+{c.id36} ^{c.score}\nu/{c.author_display_name} says:\n{c.body}\n''')\n\n# Submit a link post to r/test.\nsubm_id = client1.p.submission.create_link_post('test',\n        \"Check out this cool website\", \"https://www.reddit.com\")\n\n# Reply to a submission.\nfrom redditwarp.util.extract_id_from_url import extract_submission_id_from_url\nidn = extract_submission_id_from_url(\"https://www.reddit.com/comments/5e1az9\")\ncomm1 = client1.p.submission.reply(idn, \"Pretty cool stuff!\")\n\n# Delete the post and the comment reply.\nclient1.p.submission.delete(subm_id)\nclient1.p.comment.delete(comm1.id)\n```\n\n</details>\n\n<details>\n  <summary>Streaming example</summary>\n\n```python\n#!/usr/bin/env python\nfrom __future__ import annotations\nfrom typing import TYPE_CHECKING\nif TYPE_CHECKING:\n    from redditwarp.models.submission_ASYNC import Submission\n\nimport asyncio\n\nimport redditwarp.ASYNC\nfrom redditwarp.streaming.makers.subreddit_ASYNC import create_submission_stream\nfrom redditwarp.streaming.ASYNC import flow\n\nasync def main() -> None:\n    client = redditwarp.ASYNC.Client()\n    async with client:\n        submission_stream = create_submission_stream(client, 'AskReddit')\n\n        @submission_stream.output.attach\n        async def _(subm: Submission) -> None:\n            print(subm.id36, '~', subm.title)\n\n        @submission_stream.error.attach\n        async def _(exc: Exception) -> None:\n            print('ERROR:', repr(exc))\n\n        await flow(submission_stream)\n\nasyncio.run(main())\n```\n\n</details>\n\n## Support\n\nPost any questions you have to [r/redditdev].\n\n[r/redditdev]: https://www.reddit.com/r/redditdev/\n\nJoin the conversation in the RedditWarp [Discord guild].\n\n[Discord guild]: http://pyprohly.github.io/redditwarp/discord-invite\n\n## Links\n\n* [Repository](https://github.com/Pyprohly/redditwarp)\n* [PyPI](https://pypi.org/p/redditwarp/)\n* [Documentation](https://redditwarp.readthedocs.io/)\n* [r/redditdev]\n* [Discord guild]\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A library for interacting with the Reddit API.",
    "version": "1.2.0",
    "project_urls": null,
    "split_keywords": [
        "reddit",
        "api",
        "wrapper"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "be089fcd55ddfa4bafef2ff4d7e95a737a46409c759ab03028cf6141e99d1571",
                "md5": "b5f22c96cc38eba0e4cfe1069256d8b4",
                "sha256": "36f7b2eca9d2e00f30ab02bdc872744a6f2f5f6c94e8463f7159fa12519ba094"
            },
            "downloads": -1,
            "filename": "redditwarp-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b5f22c96cc38eba0e4cfe1069256d8b4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 494047,
            "upload_time": "2023-08-29T13:18:53",
            "upload_time_iso_8601": "2023-08-29T13:18:53.419209Z",
            "url": "https://files.pythonhosted.org/packages/be/08/9fcd55ddfa4bafef2ff4d7e95a737a46409c759ab03028cf6141e99d1571/redditwarp-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "de245ab5217906c6e658bf292dd46c4cb95d8725644a2312835aaae7c27defef",
                "md5": "2443d9064f467665dc4e87a2122f45c1",
                "sha256": "e066f983a926423765d1f3126037267cc91e0378a00b3355a1113e3296d94869"
            },
            "downloads": -1,
            "filename": "redditwarp-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2443d9064f467665dc4e87a2122f45c1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 212747,
            "upload_time": "2023-08-29T13:18:55",
            "upload_time_iso_8601": "2023-08-29T13:18:55.340556Z",
            "url": "https://files.pythonhosted.org/packages/de/24/5ab5217906c6e658bf292dd46c4cb95d8725644a2312835aaae7c27defef/redditwarp-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-29 13:18:55",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "redditwarp"
}
        
Elapsed time: 0.11031s