librefi/librefi/fxckers/macrosystem.py

37 lines
1.4 KiB
Python

from __future__ import unicode_literals
from ._common import BaseFxcker
from ..utils import (
dump_qs,
hidden_inputs,
js_to_json,
regex_search_string,
TEST_URL,
)
import datetime
import json
import urllib.parse
# not tested, intended for Koleje Mazowieckie (https://www.mazowieckie.com.pl/) and possibly others
class MacrosystemFxcker(BaseFxcker):
def unfxck(self, location=None):
first_page = self.request("GET", location or TEST_URL)
qs = regex_search_string(r'(?s)function toDeviceLogin\(\)\s*{.+?queryParams\(({.+?})\);', first_page.text)
qs = qs.replace('new Date().getTime()', str(datetime.datetime.now().microsecond()))
first_url = urllib.parse.urlparse(first_page.url)
browser = urllib.parse.parse_qs(first_url.query)['browser'][0]
qs = qs.replace('getUrlParam("browser")', browser)
qs = js_to_json(qs)
qs = json.loads(qs)
qs = dump_qs(qs)
login_url = urllib.parse.urljoin(first_page.url, './login.html') + '?' + qs
login_page_req = self.request('GET', login_url)
login_page = login_page_req.text()
login_form = hidden_inputs(login_page)
login_req_url = regex_search_string(r"<form\b[^>]*?action='([^']+)'", login_page)
# redirects to KM's own webpage
login_req = self.request('POST', login_req_url, body=dump_qs(login_form), follow_redirects=False)
return login_req.status_code == 302