Skip to content

Instantly share code, notes, and snippets.

@xtendo-org
Created May 13, 2017 17:32
Show Gist options
  • Save xtendo-org/b4f2bbc0d07a570e236622397532d026 to your computer and use it in GitHub Desktop.
Save xtendo-org/b4f2bbc0d07a570e236622397532d026 to your computer and use it in GitHub Desktop.
맥딜리버리 주문가능 상태 체크하기
# Usage: Create ~/.ssh/mcd.yaml like
#
# username: smith@example.com
# password: mypassword
#
# then run this file.
# Requires requests and PyYAML. Tested with Python 3.6.2.
import datetime
import os.path
import re
import requests
import sys
import time
import yaml
HOME = os.path.expanduser('~')
RE_CSRF = re.compile(rb'<input type="hidden" name="csrfValue" value="(.*?)"')
RE_WELCOME = re.compile('환영합니다'.encode('utf-8'))
RE_UNAVAILABLE = re.compile('(죄송합니다. 선택하신 주소는.*?)<'.encode('utf-8'))
URL_HOME = 'https://www.mcdelivery.co.kr/kr/home.html'
if __name__ == '__main__':
with open(HOME + '/.ssh/mcd.yaml', 'r') as f:
credentials = yaml.load(f)
session = requests.Session()
print('Obtaining CSRF token...')
home_page = session.get(URL_HOME)
csrf_token = RE_CSRF.search(home_page.content).groups()[0]
print('Logging in...')
response = session.post(
'https://www.mcdelivery.co.kr/kr/login.html',
data={
'userName': credentials['username'],
'password': credentials['password'],
'csrfValue': csrf_token,
}
)
logged_in = RE_WELCOME.search(response.content)
if not logged_in:
raise ValueError('Login failed. Perhaps wrong credentials')
while True:
unavailable = RE_UNAVAILABLE.search(response.content)
if not unavailable:
import subprocess
subprocess.run('dzen2 -x 10 -y 10 -w 1000 -h 1000 -p'.split(' '))
break
sys.stdout.buffer.write(
datetime.datetime.now().strftime('%H:%M:%S').encode('ascii') +
b' ' + unavailable.groups()[0] + b'\n'
)
sys.stdout.buffer.flush()
time.sleep(60)
response = session.get(URL_HOME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment