h5bp / lazyweb-requests

Get projects and ideas built by the community
https://github.com/h5bp/lazyweb-requests/issues
1.69k stars 84 forks source link

Image Access from API #209

Closed modelmat closed 5 years ago

modelmat commented 5 years ago

When I am using README.MD, sometimes I want to have a logo in the README. However, sometimes this logo updates (as it is the icon for a user) and I have to create a commit on my repository to update it.

Good for me, there is a public-facing API in which I can access this image. Unfortunately, this isn't possible via markdown though.

What I suggest is similar to shields.io, where you can specify text in your image from an api (through https://www.npmjs.com/package/jsonpath), except the server will return the image specified.

For instance, https://botsfordiscord.com/api/v1/bots/380598116488970261 provides an avatar component, accessible in jsonpath through $.avatar. In this service, it would support: https://api-ima.ge/?url=https://botsfordiscord.com/api/v1/bots/380598116488970261&query=$.avatar, which returns the image.

I could create an equivalent like this in a bit, but would not be able to easily host it.

modelmat commented 5 years ago

Here's a quick demo (in python) - it works with gunicorn

import aiohttp
from aiohttp import web
import jsonpath_rw # pip install jsonpath-rw
import traceback

async def root_handled(request):
    try:
        return await root(request)
    except:
        traceback.print_exc()
        return web.Response(body=traceback.format_exc())

async def root(request):
    url = request.query["url"]
    expression = jsonpath_rw.parse(request.query["query"])

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            json = await resp.json()

        image_url = expression.find(json)[0].value

        suffix = request.query.get("suffix")
        prefix = request.query.get("prefix")
        replace = request.query.get("replace")
        if replace:
            be_replaced, to_replace = replace.split(",")[:2]
            image_url = image_url.replace(be_replaced, to_replace)

        if prefix:
            image_url = prefix + image_url

        if suffix:
            image_url += suffix

        async with session.get(image_url) as resp:
            body = await resp.read()

    return web.Response(body=body, content_type="image/png")

async def app():
    app = web.Application()
    app.router.add_get("/", root_handled)
    return app

if __name__ == "__main__":
    web.run_app(app())
Garbee commented 5 years ago

All this is asking for seems to be a proxy system or some URL system where you can change the URL a custom one is pointing at over time. Lots of web servers do proxy stuff and there is plenty of documentation on setting that up. URL forwarders are aplenty and a few DNS providers (such as Hover) provide it out of the box.

This is also a good request to go to your badge providers and ask them specifically to resolve. I don't see where this is a good candidate for H5BP to include.

modelmat commented 5 years ago

I was thinking about this and realise that it is a really niche area (I haven't found any tools for this though, I have searched).

Anyone, I think I found a way I can host this myself, so I might have just created a pointless issue, sorry about that.

I'm not sure how this is related to badge providers?

Garbee commented 5 years ago

I'm not sure how this is related to badge providers?

Your specific use-case provide was the badge providers changing their locations. Therefore, it sounds like a prime area to go to them and have them address it on their end.

The issue as a whole isn't niche. But there are some other more subtle issues that could come up. Like some sites serve their content with means to prevent other sites from using their bandwidth. So any kind of a proxy system would need to take this into account routinely (if not with every request) in case the state changes from the time a user defined it. So, it isn't a task that is a straightforward for an org to host openly for others since there is some needed computation power and/or possibly large bandwidth needs to solve it.

modelmat commented 5 years ago

No, this isn't about badges. This is about a user's logo (specifically a Discord one) which I wanted to keep up to date without having numerous commits. (Discord manages logos with an avatar hash)

I agree with you on the other points, are you saying that this issue should stay open?

Garbee commented 5 years ago

I agree with you on the other points, are you saying that this issue should stay open?

No, it should stay closed for those reasons. This kind of a project is too, resource intensive and possibly open to malicious use for an open community project to cover.

On the Discord logo note... Yah, they also probably do that for privacy as to make tracking a specific users logo harder. Discord is engineered for user privacy after all.