Skip to content

Commit

Permalink
Add a way for the application to dump inventory data into a file
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilXD committed Jun 30, 2024
1 parent 6d258ef commit f5d5fc3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ __pycache__
/cache
/*.jar
log.txt
/dump.dat
/lock.file
settings.json
/lang/English.json
Expand Down
17 changes: 4 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,20 @@
"console": "integratedTerminal"
},
{
"name": "Run App (INFO)",
"type": "python",
"request": "launch",
"program": "main.py",
"args": ["-vv"],
"console": "integratedTerminal",
"justMyCode": false
},
{
"name": "Run App (INFO+Tray)",
"name": "Run App (CALL)",
"type": "python",
"request": "launch",
"program": "main.py",
"args": ["-vv", "--tray"],
"args": ["-vvv"],
"console": "integratedTerminal",
"justMyCode": false
},
{
"name": "Run App (CALL)",
"name": "Run App (DUMP)",
"type": "python",
"request": "launch",
"program": "main.py",
"args": ["-vvv"],
"args": ["--dump"],
"console": "integratedTerminal",
"justMyCode": false
},
Expand Down
5 changes: 3 additions & 2 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
IS_APPIMAGE = "APPIMAGE" in os.environ and os.path.exists(os.environ["APPIMAGE"])
IS_PACKAGED = hasattr(sys, "_MEIPASS") or IS_APPIMAGE
# logging special levels
CALL = logging.INFO - 1
CALL: int = logging.INFO - 1
logging.addLevelName(CALL, "CALL")
# site-packages venv path changes depending on the system platform
if sys.platform == "win32":
Expand Down Expand Up @@ -94,8 +94,9 @@ def _merge_vars(base_vars: JsonType, vars: JsonType) -> None:
LANG_PATH = _resource_path("lang")
# Other Paths
LOG_PATH = Path(WORKING_DIR, "log.txt")
CACHE_PATH = Path(WORKING_DIR, "cache")
DUMP_PATH = Path(WORKING_DIR, "dump.dat")
LOCK_PATH = Path(WORKING_DIR, "lock.file")
CACHE_PATH = Path(WORKING_DIR, "cache")
CACHE_DB = Path(CACHE_PATH, "mapping.json")
COOKIES_PATH = Path(WORKING_DIR, "cookies.jar")
SETTINGS_PATH = Path(WORKING_DIR, "settings.json")
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ParsedArgs(argparse.Namespace):
_debug_gql: bool
log: bool
tray: bool
no_run_check: bool
dump: bool

# TODO: replace int with union of literal values once typeshed updates
@property
Expand Down Expand Up @@ -103,6 +103,7 @@ def debug_gql(self) -> int:
parser.add_argument("-v", dest="_verbose", action="count", default=0)
parser.add_argument("--tray", action="store_true")
parser.add_argument("--log", action="store_true")
parser.add_argument("--dump", action="store_true")
# undocumented debug args
parser.add_argument(
"--debug-ws", dest="_debug_ws", action="store_true", help=argparse.SUPPRESS
Expand Down
9 changes: 7 additions & 2 deletions manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
Available command line arguments:

• --tray
Start application as minimised into tray.
Start the application as minimised into tray.
• -v
Increase verbosity level. Can be stacked up several times (-vv, -vvv, etc.) to show
increasingly more information during application runtime.
• --log
Enables logging of runtime information into a 'log.txt' file. Verbosity level of this logging
matches the level set by `-v`.
• --dump
Start the application in a data-dump mode, where a 'dump.dat' file is created.
The file contains anonymous raw Twitch API data, studying of which can help troubleshoot
issues with the application. The application automatically closes shortly after launching,
once dumping is finished.
• --version
Show application version information
Show application version information.

Note: Additional settings are available within the application GUI.

Expand Down
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Settings:
# from args
log: bool
tray: bool
no_run_check: bool
dump: bool
# args properties
debug_ws: int
debug_gql: int
Expand Down
32 changes: 32 additions & 0 deletions twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import logging
from time import time
from copy import deepcopy
from itertools import chain
from functools import partial
from collections import abc, deque, OrderedDict
Expand Down Expand Up @@ -39,6 +40,7 @@
)
from constants import (
CALL,
DUMP_PATH,
COOKIES_PATH,
GQL_OPERATIONS,
MAX_CHANNELS,
Expand Down Expand Up @@ -559,6 +561,10 @@ def _viewers_key(channel: Channel) -> int:
return -1

async def run(self):
if self.settings.dump:
with open(DUMP_PATH, 'w', encoding="utf8"):
# replace the existing file with an empty one
pass
while True:
try:
await self._run()
Expand Down Expand Up @@ -599,6 +605,9 @@ async def _run(self):
self.change_state(State.INVENTORY_FETCH)
while True:
if self._state is State.IDLE:
if self.settings.dump:
self.gui.close()
continue
self.gui.status.update(_("gui", "status", "idle"))
self.stop_watching()
# clear the flag and wait until it's set again
Expand Down Expand Up @@ -787,6 +796,9 @@ async def _run(self):
watching_channel,
)
elif self._state is State.CHANNEL_SWITCH:
if self.settings.dump:
self.gui.close()
continue
self.gui.status.update(_("gui", "status", "switching"))
# Change into the selected channel, stay in the watching channel,
# or select a new channel that meets the required conditions
Expand Down Expand Up @@ -1412,6 +1424,26 @@ async def fetch_inventory(self) -> None:
chunk_campaigns_data = await chunk_coro
# merge the inventory and campaigns datas together
inventory_data = self._merge_data(inventory_data, chunk_campaigns_data)

if self.settings.dump:
# dump the campaigns data to the dump file
with open(DUMP_PATH, 'a', encoding="utf8") as file:
# pre-process a little, so the dump file isn't overly bloated
dump_data: JsonType = deepcopy(inventory_data)
for campaign_data in dump_data.values():
if (
campaign_data["allow"]
and campaign_data["allow"].get("isEnabled", True)
and campaign_data["allow"]["channels"]
):
# simply count the channels included in the ACL
campaign_data["allow"]["channels"] = (
f"{len(campaign_data['allow']['channels'])} channels"
)
json.dump(dump_data, file, indent=4, sort_keys=True)
file.write("\n\n") # add a new line spacer
json.dump(claimed_benefits, file, indent=4, sort_keys=True, default=str)

# use the merged data to create campaign objects
campaigns: list[DropsCampaign] = [
DropsCampaign(self, campaign_data, claimed_benefits)
Expand Down

0 comments on commit f5d5fc3

Please sign in to comment.