Skip to content

Commit 3d4f2af

Browse files
authored
Merge pull request #1156 from fronzbot/dev
0.25.1
2 parents b77e2d9 + 053ce10 commit 3d4f2af

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

blinkpy/api.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import logging
44
import string
5-
import json
65
from json import dumps
76
from asyncio import sleep
87
from urllib.parse import urlencode, urlparse, parse_qs
9-
from bs4 import BeautifulSoup
108
from blinkpy.helpers.util import (
119
get_time,
1210
Throttle,
1311
local_storage_clip_url_template,
1412
)
13+
from blinkpy.helpers.oauth_parser import OAuthArgsParser
1514
from blinkpy.helpers.constants import (
1615
TIMEOUT,
1716
DEFAULT_USER_AGENT,
@@ -812,18 +811,10 @@ async def oauth_get_signin_page(auth):
812811

813812
# Extract CSRF token from oauth-args script tag
814813
try:
815-
# Parse HTML
816-
soup = BeautifulSoup(html, "html.parser")
817-
818-
# Find oauth-args script
819-
oauth_script = soup.find(
820-
"script", {"id": "oauth-args", "type": "application/json"}
821-
)
822-
if oauth_script and oauth_script.string:
823-
oauth_data = json.loads(oauth_script.string)
824-
csrf_token = oauth_data.get("csrf-token")
825-
if csrf_token:
826-
return csrf_token
814+
parser = OAuthArgsParser()
815+
parser.feed(html)
816+
if parser.csrf_token:
817+
return parser.csrf_token
827818
except Exception as error:
828819
_LOGGER.error("Failed to extract CSRF token: %s", error)
829820

blinkpy/helpers/oauth_parser.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""HTML parser for OAuth CSRF token extraction."""
2+
3+
import json
4+
from html.parser import HTMLParser
5+
6+
7+
class OAuthArgsParser(HTMLParser):
8+
"""HTML parser to extract CSRF token from oauth-args script tag."""
9+
10+
def __init__(self):
11+
"""Initialize parser."""
12+
super().__init__()
13+
self.csrf_token = None
14+
self._in_oauth_script = False
15+
16+
def handle_starttag(self, tag, attrs):
17+
"""Handle opening tags."""
18+
if tag == "script":
19+
attrs_dict = dict(attrs)
20+
if (
21+
attrs_dict.get("id") == "oauth-args"
22+
and attrs_dict.get("type") == "application/json"
23+
):
24+
self._in_oauth_script = True
25+
26+
def handle_data(self, data):
27+
"""Handle tag content."""
28+
if self._in_oauth_script:
29+
try:
30+
oauth_data = json.loads(data)
31+
self.csrf_token = oauth_data.get("csrf-token")
32+
except (json.JSONDecodeError, AttributeError):
33+
pass
34+
self._in_oauth_script = False
35+
36+
def handle_endtag(self, tag):
37+
"""Handle closing tags."""
38+
if tag == "script":
39+
self._in_oauth_script = False

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "blinkpy"
7-
version = "0.25.0"
7+
version = "0.25.1"
88
license = {text = "MIT"}
99
description = "A Blink camera Python Library."
1010
readme = "README.rst"

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ requests>=2.24.0
33
python-slugify>=4.0.1
44
sortedcontainers~=2.4.0
55
aiohttp>=3.8.4
6-
aiofiles>=23.1.0
7-
beautifulsoup4>=4.12.0
6+
aiofiles>=23.1.0

0 commit comments

Comments
 (0)