LoginSignup
16
16

More than 5 years have passed since last update.

Pythonista for iOSで松屋のクーポン(QRコード)を表示する

Last updated at Posted at 2016-12-14

はじめに

松屋好きですか?私は大好きです。
そんなエンジニアなら誰しもが好きな松屋ですが、スマホの公式アプリLINE上でクーポンが配信されていることはご存知でしょうか。

タッチパネル式券売機にQRコードをかざすと使える1のですが、いちいちアプリやLINEを起動してQRコードを表示するのが面倒な上に、画面の輝度が低かったりするとうまく読み取ってくれません。
松屋で発券に17秒以上かける輩は出禁もやむを得ないというのは周知の事実2なので、最新のQRコードの表示画面の輝度調整ワンタップでやってくれるPythonista for iOS用のスクリプトを書きました。

Pythonista for iOSというのはiOS上で動くPython環境で、iOS上でpythonが動くだけでもヤバいのにpythonからObjective-Cのオブジェクトも触れたりなんかもう色々ヤバいアプリです。3

使い方

スクリプト

まずはPythonistaで次のスクリプトを保存します。
PythonistaにはPython2とPython3が搭載されていますがPython3で実行してください。
実行すると、画面輝度を最大にした上で、最新のQRコードが表示されます。[とじる]をタップすると、画面輝度が元に戻ります。

松屋.py
import urllib.request
import re
import ui
from objc_util import *

def get_matsuya_qrcode_url_linecoupon():
    # LINEクーポン Example
    # http://www.matsuyafoods.co.jp/line/
    # http://www.matsuyafoods.co.jp/sp/line_cp/161115/
    #   http://www.matsuyafoods.co.jp/sp/line_cp/161115/qr_line.png

    line_url = 'http://www.matsuyafoods.co.jp/line/'
    with urllib.request.urlopen(line_url) as res:
        html = res.read()

    qr_date = re.findall(r"/line/images/cp_(\d{6}).jpg", html.decode('utf-8'))[0]
    qr_url = 'http://www.matsuyafoods.co.jp/sp/line_cp/%s/qr_line.png' % qr_date

    return(qr_url)

def get_matsuya_qrcode_url_mobilecoupon():
    # 松屋モバイルクーポン Example
    # http://www.matsuyafoods.co.jp/sp/coupon.html
    #   http://www.matsuyafoods.co.jp/coupon/161115/qr_sp.png

    # 松屋モバイルクーポン ケータイサイト Example
    # http://www.matsuyafoods.co.jp/mobile/coupon/
    #   http://www.matsuyafoods.co.jp/mobile/coupon/161115/qr_fp.png

    mobile_url = 'http://www.matsuyafoods.co.jp/sp/coupon.html'
    with urllib.request.urlopen(mobile_url) as res:
        html = res.read()

    qr_date = re.findall(r"/coupon/(\d{6})/qr_sp.png", html.decode('utf-8'))[0]
    qr_url = 'http://www.matsuyafoods.co.jp/coupon/%s/qr_sp.png' % qr_date

    return(qr_url)


def get_matsuya_qrcode_image(url):
    qr_image, headers = urllib.request.urlretrieve(url)
    return(qr_image)

def show_view(qr_image):
    UIScreen = ObjCClass('UIScreen')
    screen = UIScreen.mainScreen()
    prev_brightness = screen.brightness()
    screen.setBrightness(1.0)

    view = ui.View()
    view.name = 'みんなの食卓でありたい。'
    view.background_color = 'white'

    image = ui.Image.named(qr_image)

    imageView = ui.ImageView()
    imageView.image = image
    imageView.flex = 'WH'
    imageView.content_mode = ui.CONTENT_SCALE_ASPECT_FIT

    def button_tapped(sender):
        screen.setBrightness(prev_brightness)
        view.close()

    button = ui.Button(title='[とじる]')
    button.flex = 'TRL'
    button.center = (view.width * 0.5, view.height * 0.5)
    button.action = button_tapped

    view.add_subview(imageView)
    view.add_subview(button)
    view.present(style='full_screen', hide_title_bar=True, orientations=('portrait',))


def main():
    # 松屋モバイルクーポンを表示します。
    url = get_matsuya_qrcode_url_mobilecoupon()
    # LINEクーポンを表示したい場合は次の行をコメントアウト
#   url = get_matsuya_qrcode_url_linecoupon()

    qr_image = get_matsuya_qrcode_image(url)
    show_view(qr_image)



main()

実行画面

写真 2016-12-14 20 45 52.png

ホームスクリーンにショートカットを設置

前述のスクリプトをワンタップで起動するために、iPhoneのホームスクリーンにリンクを設置します。

  1. Pythonistaでスクリプトを開き、右上のレンチアイコンから"Home Screen"を選択します。
    写真 2016-12-14 20 19 06.png

  2. 設定画面が表示されるので、"Run Script"にチェックを入れて各種設定4を行います。
    写真 2016-12-14 20 20 19.png

  3. Continueをタップし、画面の指示に従い設定すれば、ホームスクリーンに松屋アイコンが設置されます。
    写真 2016-12-14 20 24 56.png

おわりに

いかがでしょうか。雑なスクリプトですが、これくらいの用途のものを自分で簡単に作れるPythonistaはヤバいと思います。
それでは皆様も良い松屋ライフを!


  1. 古い券売機の場合は店員さんにクーポンを提示すると使えます。 

  2. 要出典 

  3. ヤバさについてはこちらのブログが詳しいです。iOS上で動作する革命的ものづくり環境「Pythonista 3」の魅力をとくと語る 

  4. 松屋カラーはIcon Color: 014099, Background Color: FCC929です。 

16
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16