Metadata-Version: 2.1
Name: browserfetch
Version: 0.1.0
Summary: fetch in Python using your browser! 
License: GNU General Public License v3 (GPLv3)
Project-URL: Homepage, https://github.com/5j9/browserfetch
Keywords: browser,fetch,python,cookies
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Requires-Python: >=3.11
Description-Content-Type: text/x-rst
Requires-Dist: aiohttp
Requires-Dist: pyperclip

Fetch using your browser.

Let the browser manage cookies for you.

⚠️ This project is a very simple implementation. Not tested thoroughly. Consider it a proof of concept.

Usage
-----
1. You'll run a Python script containing some code like this:

.. code-block:: python

    from asyncio import gather, get_event_loop

    from browserfetch import fetch, run_server


    async def main():
        response1, response2, reponse3 = await gather(
            fetch('https://example.com/path1'),
            fetch('https://example.com/image.png'),
            fetch('https://example.com/path2'),
        )
        # do stuff with retrieved responses


    loop = get_event_loop()
    loop.create_task(main())
    run_server(loop=loop)


2. Open your browser, goto http://example.com (perhaps solve a captcha and log in).
3. Copy the contents of `browserfetch.js`_ file and paste it in browser's console. (You can use a browser extensions like violentmonkey_/tampermonkey_ to this step for you.)

That's it! Your Python script starts handling requests.
The browser tab should remain open of-coarse.

The server can handle multiple websocket connections from different websites simultaneously.

How it works
------------
``browserfetch`` communicates with your browser using a websocket. The ``fetch`` function just passes the request to browser and it is the browser that handles the actual request. Response data is sent back to Python using the same WebSocket connection.

Motivations
-----------
* `browser_cookie3 stopped working on Chrome-based browsers`_. There is a workaround: ShadowCopy, but it requires admin privilege.
* Another issue with browser_cookie's approach is that it retrieves cookies from cookie files, but these files are not updated instantly. Thus, you might have to wait or retry a few times before you can successfully access newly set cookies.
* ShadowCopying and File access are slow and inefficient operations.

Downsides
---------
* Setting up ``browserfetch`` is a more cumbersome since it requires running a Python server and also injecting a small script into the webpage. Using ``browser_cookie3`` might be a better choice if there are many websites that you need to communicate with.

.. _`browser_cookie3 stopped working on Chrome-based browsers`: https://github.com/borisbabic/browser_cookie3/issues/180
.. _tampermonkey: https://github.com/Tampermonkey/tampermonkey
.. _violentmonkey: https://github.com/violentmonkey/violentmonkey
.. _browserfetch.js: https://github.com/5j9/browserfetch/blob/master/browserfetch/browserfetch.js
