[PYTHON] I want Airpods Pro, so I will notify LINE when it arrives

The beginning of things

I missed the wave of Airpods Pro. The Airpods Pro was released at the end of last month, but I thought I would buy it after looking at Sony's example Neucan and comparative reviews, but by the time I read the article, it was sold out. __It seems that the Apple Store, which is directly managed, will receive the goods from time to time __, so monitor it and send a notification __ to your __LINE as soon as the stock arrives.

Preliminary research

Problem of how to get warehousing information Apparently it is updated properly when it arrives every morning スクリーンショット 2019-11-15 13.19.36.png

Click Confirm Date to receive this screen. You can get information about nearby shops by entering the zip code. スクリーンショット 2019-11-15 13.25.15.png

With this feeling, it seems that you can get it by GET or POST, so look at the network 68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3235363139362f30656336353930312d353739382d383336632d343739332d3334663038623530363361362e706e67.png

There is something like this is not really this

You can see Response in the developer tools I want to make it easier to understand, so I will try to get information with an application that will GET or POST スクリーンショット 2019-11-15 13.36.28.png Came so much

parts is the part number and location is the zip code

See the response properly

The lenspons that I just returned will be returned at one store.

response


{
  "head": {
    "status": "200",
    "data": {}
  },
  "body": {
    "stores": [
      {
        "storeEmail": "[email protected]",
        "storeName": "Shinsaibashi",
        "reservationUrl": "http://www.apple.com/jp/retail/shinsaibashi",
        "makeReservationUrl": "http://www.apple.com/jp/retail/shinsaibashi",
        "state": "Osaka",
        "storeImageUrl": "https://rtlimages.apple.com/cmc/dieter/store/4_3/R091.png?resize=828:*&output-format=jpg",
        "country": "JP",
        "city": "Osaka City",
        "storeNumber": "R091",
        "partsAvailability": {
          "MWP22J/A": {
            "storePickEligible": true,
            "storeSearchEnabled": true,
            "storeSelectionEnabled": true,
            "storePickupQuote": "Sat 2019/12/07 Apple Shinsaibashi",
            "pickupSearchQuote": "Date of receipt:<br/>Sat 2019/12/07",
            "storePickupLabel": "Receipt date:",
            "partNumber": "MWP22J/A",
            "purchaseOption": "",
            "ctoOptions": "",
            "storePickupLinkText": "Check the date you can receive",
            "storePickupProductTitle": "AirPods Pro",
            "pickupDisplay": "ships-to-store"
          }
        },
        "phoneNumber": "06-4963-4500",
        "address": {
          "address": "Apple Shinsaibashi",
          "address3": "Urban BLD Shinsaibashi",
          "address2": "1 Nishishinsaibashi, Chuo-ku-5-5",
          "postalCode": "542-0086"
        },
        "hoursUrl": "http://www.apple.com/jp/retail/shinsaibashi",
        "storeHours": {
          "storeHoursText": "business hours",
          "bopisPickupDays": "Days",
          "bopisPickupHours": "time",
          "hours": [
            {
              "storeTimings": "10:00~21:00",
              "storeDays": "Month~Day:"
            }
          ]
        },
        "storelatitude": 34.672,
        "storelongitude": 135.50007,
        "storedistance": 3.44,
        "storeDistanceWithUnit": "3.44 km",
        "storeDistanceVoText": "542-Distance from 0086: 3.44 km",
        "storelistnumber": 1,
        "storeListNumber": 1
      }

----------------Continue to the bottom----------------

There seems to be no problem if you can get only __storePickupQuote __

Write in python

If you just get it, you can implement it only with requests, so write it appropriately

Acquisition of inventory information


def get_store_status(_parts, _location):
    arrival = []
    url =  "https://www.apple.com/jp/shop/retail/pickup-message"
    param = {"parts.0": _parts, "location": _location}

    response = requests.get(url, params=param)
    json_data = json.loads(response.text)

    stores = json_data['body']['stores']
    for store in stores:
        store_pickup = store['partsAvailability'][_parts]['storePickupQuote']

        if 'today' in store_pickup:
            arrival.append({'Store':store['address']['address'],
                            'Postal code':store['address']['postalCode']})

    return arrival

I wrote it for the time being, if I explain it appropriately Put the returned json in the dictionary The store that received the goods has today, not the date, so make such a judgment. I will pass if there is today

I don't know when the inventory information will actually be updated, so Make a request once a minute between 6:00 and 10:00.

python


def start_job():
    parts = {'AirpodsPro':'MWP22J/A', 'Airpods':'MV7N2J/A'}
    location = '530-0000'

    end_stamp = datetime.now().timestamp() + (60 * 60 * 4)

    while True:
        now = datetime.now().timestamp()
        arrival = get_store_status(parts['AirpodsPro'], location)

        if arrival or now > end_stamp:
            line_notif(arrival)
            break

        time.sleep(60)

def trigger():
    schedule.every().day.at('06:00').do(start_job)
    while True:
        schedule.run_pending()
        time.sleep(1)

If there is stock, you will be notified by exiting the loop If not, Oryokuru until 10 o'clock

Notify LINE

LINE registers the Notify guy and sends it to himself Let me refer to this-Send notification to LINE with Python

python


def line_notify(stores):
    line_notify_token = 'Access token'
    line_notify_api = 'https://notify-api.line.me/api/notify'

    if stores:
        strings=''
        for s in stores:
            strings += f'''
Store: {s['Store']}
Postal code: {s['Postal code']}\n'''

        message = f'''
Airpods Pro is in stock.
Target stores:{strings}
Shopping cart: https://www.apple.com/jp/shop/bag
        '''

        payload = {'message': message}
        headers = {'Authorization': 'Bearer ' + line_notify_token}
        requests.post(line_notify_api, data=payload, headers=headers)

If there is any arrival for the time being, this will notify LINE You can purchase it as it is by pasting the URL of the shopping cart. (It is necessary to put it in the shopping cart with the terminal and browser to purchase in advance)

I'm sad if it doesn't come even though it's in stock so I'll try if it works

I have airpods so I'll try it with this one I put the information of Airpods in the dictionary earlier, so I will use it

arrival = get_store_status(parts['Airpods'], location)

Now run.

25521.jpg

I got a notification so it's okay

Finally

LINE Notify is very convenient, and I think it will be very useful in the future.

__ The biggest problem is whether I'm awake in the morning __

Recommended Posts

I want Airpods Pro, so I will notify LINE when it arrives
I researched Docker, so I will summarize it
I made a segment tree with python, so I will introduce it
I got stuck so I reviewed it
I wondered if it became so easy when it became centos8 2 ~ I tried adding PHP ~
I want to do it with Python lambda Django, but I will stop
I didn't understand the behavior of numpy's argsort, so I will summarize it.
I tried to make a calculator with Tkinter so I will write it
I made a program to notify you by LINE when switches arrive