forked from sergey-galasov/psn-galaxy
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4cd018
commit 00c87de
Showing
10 changed files
with
302 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
galaxy.plugin.api==0.64 | ||
certifi==2019.3.9 | ||
beautifulsoup4==4.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import json | ||
import logging | ||
from typing import List, Dict | ||
|
||
from bs4 import BeautifulSoup | ||
from galaxy.api.errors import UnknownBackendResponse | ||
from galaxy.api.types import SubscriptionGame | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PSNGamesParser: | ||
|
||
_SUBSCRIBED_GAMES_PAGINATOR_CSS_CLASS = 'ems-sdk-strand-paginator' | ||
_SUBSCRIBED_GAMES_CSS_CLASS = 'ems-sdk-product-tile-link' | ||
_GAME_DATA_TAG = 'data-telemetry-meta' | ||
|
||
def parse(self, response) -> List[SubscriptionGame]: | ||
try: | ||
games = self._subscription_games(response) | ||
except NotFoundSubscriptionPaginator: | ||
raise UnknownBackendResponse(f"HTML TAG: {self._SUBSCRIBED_GAMES_PAGINATOR_CSS_CLASS} was not found in response.") | ||
else: | ||
return [ | ||
SubscriptionGame(game_id=game['titleId'], game_title=game['name']) | ||
for game in games if game.get('name') and game.get('titleId') and not game['name'].startswith('PlayStation') | ||
] | ||
|
||
def _subscription_games(self, response: str) -> List[Dict]: | ||
"""Scrapes all PS Plus Monthly games from https://store.playstation.com/subscriptions""" | ||
|
||
parsed_html = BeautifulSoup(response, 'html.parser') | ||
paginator = parsed_html.find("div", class_=self._SUBSCRIBED_GAMES_PAGINATOR_CSS_CLASS) | ||
if not paginator: | ||
raise NotFoundSubscriptionPaginator | ||
logger.debug("HTML response slice of %s tag: \n%s" % (self._SUBSCRIBED_GAMES_PAGINATOR_CSS_CLASS, paginator.decode_contents())) | ||
games = paginator.find_all("a", class_=self._SUBSCRIBED_GAMES_CSS_CLASS) | ||
result = [] | ||
for game in games: | ||
try: | ||
game_data = getattr(game, 'attrs', {}).get(self._GAME_DATA_TAG) | ||
result.append(json.loads(game_data)) | ||
except (json.JSONDecodeError, TypeError) as e: | ||
logger.error(e) | ||
return result | ||
|
||
|
||
class NotFoundSubscriptionPaginator(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.