File tree Expand file tree Collapse file tree 4 files changed +46
-17
lines changed
Expand file tree Collapse file tree 4 files changed +46
-17
lines changed Original file line number Diff line number Diff line change 22
33import logging
44import string
5- import json
65from json import dumps
76from asyncio import sleep
87from urllib .parse import urlencode , urlparse , parse_qs
9- from bs4 import BeautifulSoup
108from blinkpy .helpers .util import (
119 get_time ,
1210 Throttle ,
1311 local_storage_clip_url_template ,
1412)
13+ from blinkpy .helpers .oauth_parser import OAuthArgsParser
1514from 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44
55[project ]
66name = " blinkpy"
7- version = " 0.25.0 "
7+ version = " 0.25.1 "
88license = {text = " MIT" }
99description = " A Blink camera Python Library."
1010readme = " README.rst"
Original file line number Diff line number Diff line change @@ -3,5 +3,4 @@ requests>=2.24.0
33python-slugify >= 4.0.1
44sortedcontainers ~= 2.4.0
55aiohttp >= 3.8.4
6- aiofiles >= 23.1.0
7- beautifulsoup4 >= 4.12.0
6+ aiofiles >= 23.1.0
You can’t perform that action at this time.
0 commit comments