# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 00. Capture and Mimic First Run
#
# Procedure:
#
# Settings > Apps > Meijer.
#
# 1. Force Stop.
# 2. Clear Data.
#
# Start from complete scratch for mitmproxy.
#
# 1. Launch mitmproxy, if not already running.
# 2. Launch Meijer app.

# There is a lot of 'chatter', requests blocked by pihole don't need replicated.
#
# Right now just working on replicating requests, not comprehending them.

import requests

# #### Request 1

request=dict()
request["url"] = "https://meijer.122.2o7.net/id"
request["headers"] = {
    'user-agent': 'Dalvik/2.1.0 (Linux; U; Android 5.0.2; HTCONE Build/LRX22G)'
}
r = requests.get(**request)
assert r.status_code==200
r.json() # An ID used for?

# #### Request 2

request=dict()
request["url"] = "https://static.meijer.com/mobileassets/info/mma_config.json"
request["headers"] = {
    'Platform': 'Android',
    'Version': '5.10.0',
    'Build': '51000000',
    'User-Agent': 'okhttp/3.8.0'
}
r = requests.get(**request)
assert r.status_code==200
r.json() # Determine if the user needs to update.

# #### Request 3
#
# The ```Authorization``` header is a base64 encoded string generated from:
#
# ```./res/values/strings.xml:    <string name="account_services_secret">drAqas76Re7RekeBanaMaNEMah7paDE5</string>```

import base64
# Working Backwards.
AUTH="<KEY>"
base64.decodebytes(AUTH.encode("UTF-8"))

# Working Forward.
account_services_secret="drAqas76Re7RekeBanaMaNEMah7paDE5"
AUTH_=base64.encodebytes("mma:{}".format(account_services_secret).encode("UTF-8")).decode("UTF-8").strip()

assert AUTH==AUTH_

request=dict()
request["url"] = "https://login.meijer.com/as/token.oauth2"
request["headers"] = {
    'Authorization': 'Basic {}'.format(AUTH),
    'Platform': 'Android',
    'Version': '5.10.0',
    'Build': '51000000',
    'User-Agent': 'okhttp/3.8.0'
}
request["params"] =  {
    'grant_type': 'client_credentials',
    'scope': 'openid',
}
r = requests.post(**request)
assert r.status_code==200
r.json().keys() # Determine if the user needs to update.

access_token = r.json()["access_token"] # We'll need this later.

# #### Request 4
#
# Get ads and dynamic content for the Meijer App.
#
# Relevant source file: ```./smali/com/meijer/mobile/serverapi/rxjava/DigitalMperksMMAAPIObservable.smali`` 

request=dict()
request["url"] = "https://mperksservices.meijer.com/dgtlmPerksMMA/api/cms/home/content"
request["headers"] = {
    'Accept': 'application/vnd.meijer.digitalmperks.homecontent-v1.0+json',
    'Authorization': 'Bearer {}'.format(access_token),
    'Content-Type': 'application/vnd.meijer.digitalmperks.homecontent-v1.0+json',
    'Platform': 'Android',
    'Version': '5.10.0',
    'Build': '51000000',
    'User-Agent': 'okhttp/3.8.0'
}
r = requests.get(**request)
r

r.json()

import IPython.display

IPython.display.Image(url=r.json()["specialOffersBannerURL"])

IPython.display.Image(url=r.json()["topBanner"]["bannerURL"])

StarcoderdataJupyterScriptsDedupFiltered