Skip to content

Commit

Permalink
WIP: factory for active month
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGoogle committed Dec 5, 2021
1 parent 09d36f5 commit 53a315a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
66 changes: 66 additions & 0 deletions src/active_month_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import typing as t

from galaxy.api.errors import UnknownBackendResponse

from webservice import AuthorizedHumbleAPI, WebpackParseError
from model.subscription import UserSubscriptionInfo
from model.types import Tier


class ActiveMonthInfoByUser(t.NamedTuple):
machine_name: str
'''
Treats two bussines cases as True: owning active month content AND not owning yet, but having the payment scheduled
https://support.humblebundle.com/hc/en-us/articles/217300487-Humble-Choice-Early-Unlock-Games
'''
is_or_will_be_owned: bool


ActiveMonthInfoFetchStrategy = t.Callable[[AuthorizedHumbleAPI], t.Awaitable[ActiveMonthInfoByUser]]


class _CantFetchActiveMonthInfo(Exception):
pass


class ActiveMonthResolver():

def __init__(self, has_active_subscription: bool) -> None:
if has_active_subscription:
fetch_strategy = _get_ami_from_subscriber_fallbacking_to_marketing
else:
fetch_strategy = _get_ami_from_subscriber
self._fetch_strategy: ActiveMonthInfoFetchStrategy = fetch_strategy

async def resolve(self, api: AuthorizedHumbleAPI) -> ActiveMonthInfoByUser:
return await self._fetch_strategy(api)


async def _get_ami_from_subscriber_fallbacking_to_marketing(api: AuthorizedHumbleAPI) -> ActiveMonthInfoByUser:
try:
return await _get_ami_from_subscriber(api)
except _CantFetchActiveMonthInfo:
return await _get_ami_from_marketing(api)


async def _get_ami_from_subscriber(api: AuthorizedHumbleAPI) -> ActiveMonthInfoByUser:
try:
raw = await api.get_subscriber_hub_data()
subscriber_hub = UserSubscriptionInfo(raw)
machine_name = subscriber_hub.pay_early_options.active_content_product_machine_name
marked_as_owned = subscriber_hub.user_plan.tier != Tier.LITE
except (WebpackParseError, KeyError, AttributeError, ValueError) as e:
msg = f"Can't get info about not-yet-unlocked subscription month: {e!r}"
raise _CantFetchActiveMonthInfo(msg)
else:
return ActiveMonthInfoByUser(machine_name, marked_as_owned)


async def _get_ami_from_marketing(api: AuthorizedHumbleAPI) -> ActiveMonthInfoByUser:
try:
marketing_data = await api.get_choice_marketing_data()
machine_name = marketing_data['activeContentMachineName']
except (KeyError, UnknownBackendResponse) as e:
raise UnknownBackendResponse(e)
else:
return ActiveMonthInfoByUser(machine_name, False)
43 changes: 11 additions & 32 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
from settings import Settings
from webservice import AuthorizedHumbleAPI, WebpackParseError
from model.game import TroveGame, Key, Subproduct, HumbleGame, ChoiceGame
from model.types import HP, Tier
from model.subscription import UserSubscriptionInfo
from model.types import HP
from humbledownloader import HumbleDownloadResolver
from library import LibraryResolver
from local import AppFinder
from privacy import SensitiveFilter
from active_month_resolver import (
ActiveMonthInfoByUser,
ActiveMonthResolver,
)
from utils.decorators import double_click_effect
from gui.options import OPTIONS_MODE
import guirunner as gui
Expand Down Expand Up @@ -196,14 +199,6 @@ def _choice_name_to_slug(subscription_name: str):
year, month = year_month.split('-')
return f'{calendar.month_name[int(month)]}-{year}'.lower()

async def _find_active_month_machine_name(self) -> t.Optional[str]:
try:
marketing_data = await self._api.get_choice_marketing_data()
return marketing_data['activeContentMachineName']
except (KeyError, UnknownBackendResponse) as e:
logger.error(repr(e))
return None

async def get_subscriptions(self):
subscriptions: t.List[Subscription] = []
subscription_state = await self._api.get_user_subscription_state()
Expand All @@ -226,29 +221,13 @@ async def get_subscriptions(self):
))

if not owns_active_content:
early_unlock_info_fetch_success = False
if has_active_subscription:
# for Choice subscribers who not used "Early Unlock" yet:
# https://support.humblebundle.com/hc/en-us/articles/217300487-Humble-Choice-Early-Unlock-Games
try:
raw = await self._api.get_subscriber_hub_data()
subscriber_hub = UserSubscriptionInfo(raw)
active_month_machine_name = subscriber_hub.pay_early_options.active_content_product_machine_name
active_month_marked_as_owned = subscriber_hub.user_plan.tier != Tier.LITE
except (WebpackParseError, KeyError, AttributeError, ValueError) as e:
logger.error(f"Can't get info about not-yet-unlocked subscription month: {e!r}")
else:
early_unlock_info_fetch_success = True

if not early_unlock_info_fetch_success:
# for those who have no "choices" as a potential discovery of current choice games
active_month_machine_name = await self._find_active_month_machine_name()
active_month_marked_as_owned = False

if active_month_machine_name:
active_month_resolver = ActiveMonthResolver(has_active_subscription)
active_month_info: ActiveMonthInfoByUser = await active_month_resolver.resolve(self._api)

if active_month_info.machine_name:
subscriptions.append(Subscription(
self._normalize_subscription_name(active_month_machine_name),
owned = active_month_marked_as_owned,
self._normalize_subscription_name(active_month_info.machine_name),
owned = active_month_info.is_or_will_be_owned,
end_time = None # #117: get_last_friday.timestamp() if user_plan not in [None, Lite] else None
))

Expand Down

0 comments on commit 53a315a

Please sign in to comment.