Skip to content

Commit

Permalink
automatically update user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Henning committed Jun 24, 2022
1 parent 4875cda commit 5bd0674
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 131 deletions.
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r requirements.txt
altgraph==0.17.2
cryptocode==0.1
pycryptodomex==3.14.1
pycryptodomex==3.15.0
pyinstaller==5.1
pyinstaller-hooks-contrib==2022.7
4 changes: 4 additions & 0 deletions src/models/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class TgtgAPIError(Error):
pass


class TgtgCaptchaError(TgtgAPIError):
pass


class TgtgPollingError(TgtgAPIError):
pass

Expand Down
9 changes: 7 additions & 2 deletions src/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tgtg import TgtgClient

VERSION_URL = 'https://api.github.com/repos/Der-Henning/tgtg/releases/latest'
VERSION = "1.10.4"
VERSION = "1.11.0-rc1"

prog_folder = path.dirname(sys.executable) if getattr(
sys, '_MEIPASS', False) else path.dirname(path.abspath(__file__))
Expand Down Expand Up @@ -120,7 +120,7 @@ def _get_favorites(self) -> list[Item]:
page = 1
page_size = 100
error_count = 0
while True and error_count < 5:
while error_count < 5:
try:
new_items = self.tgtg_client.get_items(
favorites_only=True,
Expand Down Expand Up @@ -172,6 +172,11 @@ def run(self) -> NoReturn:
while True:
try:
self._job()
if self.tgtg_client.captcha_error_count > 10:
log.warning("Too many 403 Errors. Sleeping for 1 hour.")
sleep(60 * 60)
log.info("Continuing scanning.")
self.tgtg_client.captcha_error_count = 0
except Exception:
log.error("Job Error! - %s", sys.exc_info())
finally:
Expand Down
60 changes: 42 additions & 18 deletions src/tests/tgtg/test_tgtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@

class TGTGAPITest(unittest.TestCase):
def test_get_items(self):
passkey = environ.get('REPO_ACCESS_TOKEN')
passkey = environ.get("REPO_ACCESS_TOKEN")
username = environ.get("TGTG_USERNAME", None)
env_file = environ.get('GITHUB_ENV', None)
timeout = environ.get('TGTG_TIMEOUT', 60)
env_file = environ.get("GITHUB_ENV", None)
timeout = environ.get("TGTG_TIMEOUT", 60)

if passkey:
encrypted_access_token = environ.get("TGTG_ACCESS_TOKEN", None)
encrypted_refresh_token = environ.get("TGTG_REFRESH_TOKEN", None)
encrypted_user_id = environ.get("TGTG_USER_ID", None)
access_token = cryptocode.decrypt(
encrypted_access_token, passkey) if encrypted_access_token else None
refresh_token = cryptocode.decrypt(
encrypted_refresh_token, passkey) if encrypted_refresh_token else None
user_id = cryptocode.decrypt(
encrypted_user_id, passkey) if encrypted_user_id else None
access_token = (
cryptocode.decrypt(encrypted_access_token, passkey)
if encrypted_access_token
else None
)
refresh_token = (
cryptocode.decrypt(encrypted_refresh_token, passkey)
if encrypted_refresh_token
else None
)
user_id = (
cryptocode.decrypt(encrypted_user_id, passkey)
if encrypted_user_id
else None
)
else:
access_token = environ.get("TGTG_ACCESS_TOKEN", None)
refresh_token = environ.get("TGTG_REFRESH_TOKEN", None)
Expand All @@ -40,17 +49,32 @@ def test_get_items(self):
# the credentials are encrypted with the REPO_ACCESS_TOKEN
credentials = client.get_credentials()
if env_file:
with open(env_file, "a", encoding='utf-8') as file:
file.write(f"TGTG_ACCESS_TOKEN={cryptocode.encrypt(credentials['access_token'], passkey)}\n")
file.write(f"TGTG_REFRESH_TOKEN={cryptocode.encrypt(credentials['refresh_token'], passkey)}\n")
file.write(f"TGTG_USER_ID={cryptocode.encrypt(credentials['user_id'], passkey)}\n")
with open(env_file, "a", encoding="utf-8") as file:
file.write(
f"TGTG_ACCESS_TOKEN={cryptocode.encrypt(credentials['access_token'], passkey)}\n"
)
file.write(
f"TGTG_REFRESH_TOKEN={cryptocode.encrypt(credentials['refresh_token'], passkey)}\n"
)
file.write(
f"TGTG_USER_ID={cryptocode.encrypt(credentials['user_id'], passkey)}\n"
)

# Tests
data = client.get_items(favorites_only=True)
assert len(data) > 0
items = client.get_items(favorites_only=True)
assert len(items) > 0
item = items[0]
item_id = item["item"]["item_id"]
for prop in GLOBAL_PROPERTIES:
assert prop in data[0]
assert prop in item
for prop in ITEM_PROPERTIES:
assert prop in data[0]["item"]
assert prop in item["item"]
for prop in PRICE_PROPERTIES:
assert prop in data[0]["item"]["price_including_taxes"]
assert prop in item["item"]["price_including_taxes"]

client.set_favorite(item_id, False)
client.set_favorite(item_id, True)

item = client.get_item(item_id)

assert item["item"]["item_id"] == item_id
Loading

0 comments on commit 5bd0674

Please sign in to comment.