Skip to content

Commit

Permalink
ability to save cookies to disk (#8)
Browse files Browse the repository at this point in the history
* ability to save cookies to disk
* bump version to v0.2.3
  • Loading branch information
danclaudiupop authored Mar 3, 2022
1 parent 06d88ac commit 87def19
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## 0.2.3 (3th March, 2022)
* save and load cookies from disk

## 0.2.2 (22th February, 2022)
* ability to parse tables

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ with Robox() as robox:
...
```

An example on how to reuse authentication state with cookies:
```python
with Robox() as robox:
page = robox.open("https://news.ycombinator.com/login")
form = page.get_forms()[0]
form.fill_in("acct", value=os.getenv("PASSWORD"))
form.fill_in("pw", value=os.getenv("USERNAME"))
page.submit_form(form)
robox.save_cookies("cookies.json")


with Robox() as robox:
robox.load_cookies("cookies.json")
page = robox.open("https://news.ycombinator.com/")
assert page.parsed.find("a", attrs={"id": "logout"})
```

See [examples](https://github.com/danclaudiupop/robox/tree/main/examples) folder for more detailed examples.

## Installation
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "robox"
version = "0.2.2"
version = "0.2.3"
description = "Robox is a simple library for exploring/scraping the web or testing a website you’re developing."
authors = ["Dan Claudiu Pop <[email protected]>"]
license = "BSD 3"
Expand Down
16 changes: 16 additions & 0 deletions src/robox/_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import asyncio
import itertools
import json
import random
import time
import typing as tp
from pathlib import Path

import httpx
from httpx._client import USE_CLIENT_DEFAULT, UseClientDefault
Expand Down Expand Up @@ -59,6 +61,20 @@ def current_url(self) -> URL:
else:
raise RoboxError("Not tracking history")

def save_cookies(self, filename: str) -> None:
cookies = {}
for cookie in self.cookies.jar:
cookies[cookie.name] = cookie.value
with open(filename, "w") as f:
json.dump(cookies, f)

def load_cookies(self, filename: str) -> None:
if not Path(filename).is_file():
return None
with open(filename, "r") as f:
cookies = httpx.Cookies(json.load(f))
self.cookies = cookies

def _increment_request_counter(self) -> None:
self.total_requests = next(self._request_counter)

Expand Down
40 changes: 40 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
from http.cookiejar import Cookie, CookieJar
from unittest.mock import MagicMock, patch

import httpx
Expand Down Expand Up @@ -150,3 +152,41 @@ def test_retry_recovarable(respx_mock):
with Robox(options=Options(retry=True, retry_max_attempts=2)) as robox:
page = robox.open(TEST_URL)
assert page.status_code == 200


def test_save_and_load_cookies(respx_mock, tmp_path):
cookies = CookieJar()
cookie = Cookie(
version=0,
name="example-name",
value="example-value",
port=None,
port_specified=False,
domain="",
domain_specified=False,
domain_initial_dot=False,
path="/",
path_specified=True,
secure=False,
expires=None,
discard=True,
comment=None,
comment_url=None,
rest={"HttpOnly": ""},
rfc2109=False,
)
cookies.set_cookie(cookie)

respx_mock.get(TEST_URL).respond(200)
with Robox(cookies=cookies) as robox:
robox.open(TEST_URL)
robox.save_cookies(tmp_path / "cookies.json")
with open(tmp_path / "cookies.json") as f:
loaded_cookies = json.load(f)
assert loaded_cookies == {"example-name": "example-value"}
assert len(robox.cookies) == 1

with Robox() as robox:
robox.load_cookies(tmp_path / "cookies.json")
robox.open(TEST_URL)
assert len(robox.cookies) == 1

0 comments on commit 87def19

Please sign in to comment.