1
0
personal-data/personal_data/fetchers/playstation.py

64 lines
2.7 KiB
Python
Raw Normal View History

2024-01-28 20:01:50 +00:00
import logging
logger = logging.getLogger(__name__)
URL_RECENTLY_PLAYED_HTML = 'https://library.playstation.com/recently-played'
2024-03-31 22:55:55 +00:00
URL_RECENTLY_PLAYED_API = 'https://web.np.playstation.com/api/graphql/v1/op?operationName=getUserGameList&variables=%7B%22limit%22%3A50%2C%22categories%22%3A%22ps4_game%2Cps5_native_game%22%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22e0136f81d7d1fb6be58238c574e9a46e1c0cc2f7f6977a08a5a46f224523a004%22%7D%7D'
2023-12-10 23:27:56 +00:00
def scrape_played_last(session):
2024-01-28 20:01:50 +00:00
# Initial request to trigger cookie.
logger.warning('Trying to trigger initial cookie usage')
2024-03-31 22:55:55 +00:00
response = session.get(URL_RECENTLY_PLAYED_HTML, cookies=session.cookies)
2024-01-28 20:01:50 +00:00
response.raise_for_status()
print('From herp')
for cookie in session.cookies:
print(' ', cookie.domain, cookie)
exit(1)
# Now trigger API call.
logger.warning('Trying to fetch data from API')
2023-12-10 23:27:56 +00:00
headers = {
2024-03-31 22:55:55 +00:00
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0',
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.5',
'content-type': 'application/json',
'X-PSN-App-Ver': 'my-playstation/0.1.0-20230720235210-hotfix-1-g1e9f07ff-1e9f07ff247eafea4d9d9236f73863cb9cd5d3e8',
'X-PSN-Correlation-Id': 'bc7a39b1-a99c-478b-9494-d2fddf189875',
'apollographql-client-name': 'my-playstation',
'apollographql-client-version': '0.1.0-20230720235210-hotfix-1-g1e9f07ff',
'X-PSN-Request-Id': '8ad64653-d8b5-4941-b565-b5536c9853df',
2023-12-10 23:27:56 +00:00
'Referer': 'https://library.playstation.com/',
'Origin': 'https://library.playstation.com',
}
2024-03-31 22:55:55 +00:00
result = session.get(URL_RECENTLY_PLAYED_API, headers=headers)
2023-12-10 23:27:56 +00:00
result.raise_for_status()
print(result.json())
games_data = result.json()['data']['gameLibraryTitlesRetrieve']['games']
print(games_data)
for game_data in games_data:
yield {
2024-03-31 22:55:55 +00:00
# Important fields
'game.name': game_data['name'],
'me.last_played_time': game_data['lastPlayedDateTime'],
'playstation.product_id': game_data['productId'],
# Secondary fields
'playstation.concept_id': game_data['conceptId'],
'playstation.title_id': game_data['titleId'],
'playstation.entitlement_id': game_data['entitlementId'],
'me.acquired_by_playstation_membership': game_data['membership'],
'game.platform': game_data['platform'],
'game.icon': game_data['image']['url'],
2023-12-10 23:27:56 +00:00
}
2024-03-31 22:55:55 +00:00
"""
2023-12-10 23:27:56 +00:00
SCRAPERS = [
2024-02-25 19:20:37 +00:00
Scraper(scrape_played_last, 'games_played_playstation',
deduplicate_mode = DeduplicateMode.BY_ALL_COLUMNS)
2023-12-10 23:27:56 +00:00
]
2024-03-31 22:55:55 +00:00
"""