-
-
Save Dareeo/14d3f4280cd3d103764ccedb1416b28b to your computer and use it in GitHub Desktop.
OTP SZÉP Kártya parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from bs4 import BeautifulSoup as bs | |
| import re | |
| import json | |
| #Full card number | |
| CARD_NUMBER = "1234567812345678" | |
| CARD_CODE = "678" | |
| URL_API = 'https://magan.szepkartya.otpportalok.hu/ajax/gyorsegyenleg/' | |
| URL_HTML = 'https://magan.szepkartya.otpportalok.hu/fooldal/' | |
| def parse_balance(input_string: str) -> int: | |
| if not input_string.strip(): | |
| return 0 | |
| return int(input_string.strip().replace('+', '')) | |
| def scrape_tokens(): | |
| session = requests.Session() | |
| response_html = session.get(URL_HTML) | |
| soup = bs(response_html.text, 'html.parser') | |
| script_tag = soup.find('script', string=re.compile('ajax_token')) | |
| if not script_tag or not script_tag.string: | |
| raise ValueError("Can't find ajax_token in script tag") | |
| match = re.search(r"ajax_token = '([a-z0-9]{64})'", script_tag.string) | |
| if not match: | |
| raise ValueError("Can't extract ajax_token") | |
| token = match.group(1) | |
| session_id = response_html.cookies.get('PHPSESSID', '') | |
| if not session_id: | |
| raise ValueError("Can't find PHPSESSID cookie") | |
| return token, session_id, session | |
| def fetch_balance(card_number, card_code, token, session_id, session): | |
| request_body = f's_azonosito_k={card_number}&s_telekod_k={card_code}&ajax_token={token}&s_captcha=' | |
| cookies = {'PHPSESSID': session_id} | |
| headers = { | |
| 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
| 'Referer': URL_HTML, | |
| 'Origin': 'https://magan.szepkartya.otpportalok.hu', | |
| 'User-Agent': 'Mozilla/5.0' | |
| } | |
| response_api = session.post(URL_API, headers=headers, data=request_body, cookies=cookies) | |
| try: | |
| response_json = response_api.json() | |
| except json.JSONDecodeError: | |
| raise RuntimeError(f"Unexpected response: {response_api.text}") | |
| # Ha lista, vegyük az első elemet | |
| if isinstance(response_json, list): | |
| response_json = response_json[0] | |
| if response_json.get('EREDMENY') != 'OK': | |
| raise RuntimeError(f"Server returned error: {response_json}") | |
| uzenet = response_json.get('UZENET', {}) | |
| balance_catering = parse_balance(uzenet.get('szamla_osszeg7', '0')) | |
| balance_leisure = parse_balance(uzenet.get('szamla_osszeg8', '0')) | |
| balance_accommodation = parse_balance(uzenet.get('szamla_osszeg9', '0')) | |
| return balance_catering, balance_leisure, balance_accommodation | |
| if __name__ == "__main__": | |
| try: | |
| token, session_id, session = scrape_tokens() | |
| catering, leisure, accommodation = fetch_balance(CARD_NUMBER, CARD_CODE, token, session_id, session) | |
| print(f"Vendéglátás: {catering} Ft") | |
| print(f"Szabadidő: {leisure} Ft") | |
| print(f"Szállás: {accommodation} Ft") | |
| except Exception as e: | |
| print(f"Hiba: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment