Skip to content

Instantly share code, notes, and snippets.

@ofalvai
Last active February 28, 2024 10:12
Show Gist options
  • Save ofalvai/82617dfc9aa8ef70fc843c8fc9e82a7c to your computer and use it in GitHub Desktop.
Save ofalvai/82617dfc9aa8ef70fc843c8fc9e82a7c to your computer and use it in GitHub Desktop.
OTP SZÉP Kártya parser
import requests
from bs4 import BeautifulSoup as bs
import re
import json
CARD_NUMBER = 12345678
CARD_CODE = 678
URL_API = 'https://magan.szepkartya.otpportalok.hu/ajax/egyenleglekerdezes/'
URL_HTML = 'https://magan.szepkartya.otpportalok.hu/fooldal/'
def parse_balance(input_string: str) -> int:
if input_string.strip() == '':
return 0
else:
input_clean = input_string.strip().replace('+', '')
return int(input_clean)
response_html = requests.get(URL_HTML)
soup = bs(response_html.text, 'html.parser')
script_tag_text = soup.find('script', text=re.compile('ajax_token')).string
match = re.search(r'ajax_token = \'([a-z0-9]{64})\'', script_tag_text)
if not match:
raise ValueError('Can\'t find ajax_token in script tag')
token = match.group(1)
session_id = response_html.cookies['PHPSESSID']
request_body = f's_azonosito_k={CARD_NUMBER}&s_telekod_k={CARD_CODE}&ajax_token={token}&s_captcha='
cookies = dict(PHPSESSID=session_id)
headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
response_api = requests.post(URL_API, headers=headers, data=request_body, cookies=cookies)
response_json = json.loads(response_api.text)
balance_catering = response_json[1]['szamla_osszeg7']
balance_leasure = response_json[1]['szamla_osszeg8']
balance_accomodation = response_json[1]['szamla_osszeg9']
print(f'Vendéglátás: {parse_balance(balance_catering)} Ft')
print(f'Szállás: {parse_balance(balance_accomodation)} Ft')
print(f'Szabadidő: {parse_balance(balance_leasure)} Ft')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment