Skip to content

Commit

Permalink
Add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
woctezuma committed Sep 13, 2021
1 parent 1d1d249 commit 34de93b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/disk_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json


def save_to_disk(data, fname, verbose=False):
with open(fname, "w", encoding="utf8") as f:
json.dump(data, f)

if verbose:
print(f"#entries = {len(data)}")

return
42 changes: 42 additions & 0 deletions src/fetch_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import requests

from src.query_utils import get_query


def get_geforce_url():
return "https://games.geforce.com/graphql"


def fetch_page(cursor="", is_slim_query=True):
print(f"Cursor: {cursor}")

r = requests.post(
url=get_geforce_url(),
json={"query": get_query(cursor, is_slim_query=is_slim_query)},
)
if r.ok:
data = r.json()
item_info = data["data"]["apps"]["items"]
page_info = data["data"]["apps"]["pageInfo"]
else:
item_info = None
page_info = None

return item_info, page_info


def fetch_all_pages(is_slim_query=True):
data = []

has_next_page = True
cursor = ""

while has_next_page:
item_info, page_info = fetch_page(cursor, is_slim_query=is_slim_query)

data += item_info

has_next_page = page_info["hasNextPage"]
cursor = page_info["endCursor"]

return data
123 changes: 123 additions & 0 deletions src/query_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
def get_query(cursor, is_slim_query=True):
query_prefixe = "{"

query_header = f'apps(first: 1200, after: "{cursor}")'

qery_metadata = """{
numberReturned
pageInfo {
hasNextPage
endCursor
totalCount
}"""

query_content = get_query_content(is_slim_query=is_slim_query)

query_suffixe = "} }"

return query_prefixe + query_header + qery_metadata + query_content + query_suffixe


def get_query_content(is_slim_query=True):
if is_slim_query:
query_content = get_slim_query_content()
else:
query_content = get_full_query_content()

return query_content


def get_slim_query_content():
query_content = """
items {
id
title
developerName
publisherName
type
osType
storeIds {
store
id
}
computedValues {
earliestReleaseDate
earliestStreetDate
}
}
"""

return query_content


def get_full_query_content():
query_content = """
items {
osType
id
cmsId
sortName
title
longDescription
contentRatings {
type
categoryKey
}
developerName
geForceUrl
images {
FEATURE_IMAGE
GAME_BOX_ART
HERO_IMAGE
KEY_ART
KEY_ICON
KEY_IMAGE
TV_BANNER
SCREENSHOTS
}
keywords
maxLocalPlayers
maxOnlinePlayers
apks {
type
version
url
}
publisherName
storeIds {
id
store
}
streamingModes {
framesPerSecond
heightInPixels
widthInPixels
}
supportedControls
supportedGamePlayModes
type
computedValues {
earliestReleaseDate
earliestStreetDate
allKeywords
}
genres
appStore
variants {
id
title
appStore
developerName
gfn {
status
visibility
releaseDate
isInLibrary
}
osType
storeId
}
}
"""

return query_content

0 comments on commit 34de93b

Please sign in to comment.