Skip to main content

Python API for LemmyHttp

Project description

AIOPlemmy: a Python package for accessing the Lemmy API asynchronously

drawing

GitHub license

AIOPlemmy allows you to interact with any Lemmy instance using Python and the LemmyHttp API.

WARNING: Plemmy is still in development and needs testing!

Installation

For the most up-to-date version of AIOPlemmy, clone and install from the repository:

git clone https://github.com/schicksal-hq/plemmy-aio
cd plemmy
pip3 install .

Basic usage

  • Interact with a Lemmy instance using the LemmyHttp object:
import aiohttp
import asyncio
import orjson
from aioplemmy import LemmyHttp, responses

async def main():
    # Unlike in original Plemmy, Plemmy-AIO accepts aiohttp session from outside and relies on developer to set
    # base_url parameter
    sess = aiohttp.ClientSession(base_url="https://lemmy.ml", json_serialize=lambda x: str(orjson.dumps(x)))
    lemmy = LemmyHttp(client=sess, key=None) # login anonymously (as guest, no key)

    # The API requests are async :3
    resp = await lemmy.get_community(name="anime")
    resp = responses.GetCommunityResponse(resp)
    print(resp)

asyncio.run(main())
  • Logging in:
import aiohttp
import asyncio
import orjson
from aioplemmy import LemmyHttp, responses

async def main():
    sess = aiohttp.ClientSession(base_url="https://lemmy.ml", json_serialize=lambda x: str(orjson.dumps(x)))
    
    # Use anonymous access to log in
    key = await LemmyHttp(client=sess, key=None).login("test@example.org", "j12345678")
    
    # And then create the LemmyHttp instance you'll actually use.
    # Of course, you can (and should) reuse the same aiohttp session.
    lemmy = LemmyHttp(client=sess, key=key)

    resp = await lemmy.get_community(name="anime")
    resp = responses.GetCommunityResponse(resp)
    print(resp)

asyncio.run(main())
  • Catching errors:
import aiohttp
import asyncio
import orjson
from aioplemmy import LemmyHttp, LemmyError, responses

async def main():
    sess = aiohttp.ClientSession(base_url="https://lemmy.ml", json_serialize=lambda x: str(orjson.dumps(x)))
    lemmy = LemmyHttp(client=sess, key=None)

    try:
        resp = await lemmy.get_community(name="nonexistingcommunity")
        resp = responses.GetCommunityResponse(resp)
        print(resp)
    except LemmyError as e:
        # The error code will be in the `error` property
        # Keep in mind that this property will be set if and only if
        # the Lemmy API could generate a response.
        #
        # Unexpected I/O and HTTP errors will trigger LemmyError, but will 
        # not set the property.
        
        print(e.error) # should print COULDNT_FIND COMMUNITY

asyncio.run(main())

Full documentation and further API refinements are on their way, but in meantime you should check out source code and examples from upstream. The method names are essentially the same after all, and you're unlikely to spot any difference if you just pipe LemmyHttp results to Response objects. The important differences are:

  • aioplemmy has its LemmyHttp methods all async
  • ...and the methods return objects parsed from JSON response, not response itself
  • ...and methods occasionally throw LemmyError

Reporting issues, making contributions, etc.

Pull requests and issues are welcome. You're also welcome to submit them to the upstream repository, I will pull the fixes from there :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioplemmy-0.3.3.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

aioplemmy-0.3.3-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file aioplemmy-0.3.3.tar.gz.

File metadata

  • Download URL: aioplemmy-0.3.3.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for aioplemmy-0.3.3.tar.gz
Algorithm Hash digest
SHA256 39c05ab8f61ba619ad72e960942596ac12bdbdc23f3584e338cd6484d91b4562
MD5 fb305019722949984a7bafa8a167d58d
BLAKE2b-256 6bf4da7f04bf70ab738cea3b24f496930976a1c47d73a36d403d51d5bac71fcf

See more details on using hashes here.

File details

Details for the file aioplemmy-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: aioplemmy-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for aioplemmy-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 00fd745e41724f7a0e0201d71644f07a5f07ead02ae44a4c9cedce59c3729e1b
MD5 da804d00b5e6879fba68c78fdffd3c35
BLAKE2b-256 ca949652ff3d7b11839d9487496d5771cc9d39602d9979eefc45695c6e4d97ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page