Skip to content

Commit

Permalink
Fix headless (#123) (#124)
Browse files Browse the repository at this point in the history
* Fix #118 - headless not working as intended and login details not being filled (#123)

* Fix not using headless resulting in needing manual input

* Fix login function always overriding headless argument

* Added terminal manual login prompt

* Force no headless if no login data given

* Readme update

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* fix pre-commit errors

---------

Co-authored-by: Exentio Kawasaki <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 14, 2024
1 parent 5a806b1 commit 9b78838
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ from gppt import GetPixivToken
from pixivpy3 import AppPixivAPI

def get_refresh_token() -> str:
with open(".token.txt", "r+") as f:
with open("token.txt", "w+") as f:
if refresh_token := f.read().strip():
return refresh_token

Expand Down Expand Up @@ -91,12 +91,13 @@ expires_in: 3600
### From Library

- Note: _In advance, please setup google-chrome-stable + selenium + webdriver_
If either username or password are missing, manual input will be required.

```python
from gppt import GetPixivToken

g = GetPixivToken()
res = g.login(headless=True, username="...", password="...")
g = GetPixivToken(headless=False,username=None,password=None)
res = g.login(headless=None,username=None,password=None)
```

- `res.response` returns:
Expand Down
20 changes: 17 additions & 3 deletions gppt/gppt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import json
import re
import sys
from typing import cast
from urllib.parse import urlencode

Expand All @@ -18,7 +19,15 @@
from selenium.webdriver.support import expected_conditions as EC # noqa: N812
from selenium.webdriver.support.ui import WebDriverWait

from .consts import AUTH_TOKEN_URL, CALLBACK_URI, CLIENT_ID, CLIENT_SECRET, LOGIN_URL, REDIRECT_URI, USER_AGENT
from .consts import (
AUTH_TOKEN_URL,
CALLBACK_URI,
CLIENT_ID,
CLIENT_SECRET,
LOGIN_URL,
REDIRECT_URI,
USER_AGENT,
)
from .types import LoginInfo
from .utils import PROXIES, _get_chrome_option, _oauth_pkce, _slow_type

Expand All @@ -40,7 +49,7 @@ def __init__(
def login(
self,
*,
headless: bool | None = False,
headless: bool | None = None,
username: str | None = None,
password: str | None = None,
) -> LoginInfo:
Expand All @@ -51,6 +60,10 @@ def login(
if password is not None:
self.password = password

# No headless if username or password are missing, manual input needed
if self.headless is True and (self.username is None or self.password is None):
self.headless = False

self.driver = webdriver.Chrome(
options=_get_chrome_option(self.headless),
)
Expand All @@ -68,10 +81,11 @@ def login(
f"Login form is not appeared. Please check connectivity for {LOGIN_URL}",
)

if self.headless:
if self.username is not None and self.password is not None:
self.__fill_login_form()
self.__try_login()
else:
print("Waiting for manual login.", file=sys.stderr) # noqa: T201
self.__wait_for_redirect()

# filter code url from performance logs
Expand Down
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ line-length = 120
target-version = ['py39']

[tool.ruff]
line-length = 120

[tool.ruff.lint]
select = ["ALL"]
ignore = ["D", "ANN101"]
line-length = 120

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
max-complexity = 18

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"gppt/main.py" = [
"T201", # `print` found
"T203", # `pprint` found
Expand Down

0 comments on commit 9b78838

Please sign in to comment.