GithubHelp home page GithubHelp logo

froosty / aiorentry Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 84 KB

Asynchronous API client for pastebin service https://rentry.co/ (https://rentry.org/)

License: MIT License

Python 98.97% Makefile 1.03%
markdown pastebin pastebin-client rentry rentryco rentryorg

aiorentry's Introduction

aiorentry

Asynchronous API client for rentry.co (mirror: rentry.org)

GitHub Actions Workflow Status codecov PyPI - Version GitHub License

About

This package allows you to interact with the rentry.co (or rentry.org) service.

Rentry.co is a markdown pastebin and publishing service that offers features such as previews, custom URLs, and editing.

Please note that this library is not developed by the official authors of rentry.co. It replicates the functionality of the official console utility, but provides it as an asynchronous API client. With this package you can manage your pages: create, edit and delete, as well as get data in text, PNG or PDF formats. All directly from your asynchronous Python application.

Installation

pip install aiorentry

Setup client

You can setup client in 2 ways:

As classic object

Caution

If you prefer the classic way, you should call await client.setup() during initialization and await client.close() during completion

import asyncio

from aiorentry.client import Client


async def main():
    client = Client('https://rentry.co')
    await client.setup()

    # Your code here

    await client.close()


asyncio.run(main())

As async context manager

import asyncio

from aiorentry.client import Client


async def main():
    async with Client('https://rentry.co') as client:
        # Your code here


asyncio.run(main())

Examples

Create new page

...
# Create new page
page = await client.new_page(
    '## Hello world from awesome API',
)

print(page)
...
Page(url='m2e2wpe8', edit_code='hUHeRUei', text='## Hello world from awesome API')
...
# Create new page with custom url and edit_code
awesome_page = await client.new_page(
    '## Hello world from awesome API',
    url='awesome-url',
    edit_code='qwerty=)'
)

print(awesome_page)
...
Page(url='awesome-url', edit_code='qwerty=)', text='## Hello world from awesome API')

Edit page

...
# Edit page
await client.edit_page(
    '### Updated Hello world',
    url='awesome-url',
    edit_code='qwerty=)',
)
...

Delete page

...
# Delete page
await client.delete_page(
    url='awesome-url',
    edit_code='qwerty=)',
)
...

Get raw page text

...
# Get raw content
content = await client.raw('awesome-url')
print(content)
...
### Updated Hello world

Get PDF file

...
# Get PDF
with open('page.pdf', mode='wb') as fp:
    fp.write(await client.pdf(page.url))
...

Get PNG

...
with open('page.png', mode='wb') as fp:
    fp.write(await client.png(page.url))
...

Custom ClientSession

Note

By default, a new instance of aiohttp.ClientSession will be created automatically. So normally you don't need to worry about this.

If you don't want to automatically create the session object inside the client, you can pass an existing aiohttp.ClientSession to the client constructor.

Caution

If you pass an existing session object to the client constructor, then you should care about releasing resources yourself.
The session will not be closed automatically! Even if the asynchronous context manager was used.

from aiohttp import ClientSession, TCPConnector
from aiorentry.client import Client

connector = TCPConnector(
    limit=5,  # Just for example
)

session = ClientSession(
    connector=connector,
)


async with Client('https://rentry.co', session=session) as client:
    # Your code here

async with session.get(...) as response:
    # You can still use this session object

aiorentry's People

Contributors

froosty avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.