Last active
June 27, 2017 02:45
-
-
Save Aeron/09add5359c4b99e277d6 to your computer and use it in GitHub Desktop.
Simple example (CLI) of how to get MegaFon balance data. Required `requests` and `baker`, but can be made without them easily. *Megafon is a Russian mobile carrier*.
This file contains 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
#!/usr/bin/env python | |
# coding: utf-8 | |
import os | |
import requests | |
import baker | |
from xml.etree import ElementTree | |
BALANCE_URL = 'https://moscowsg.megafon.ru/MEGAFON_BALANCE/MGFSTF_GET_BALANCE' | |
@baker.command(default=True, params={ | |
'msisdn': "Mobile phone number in `9261234567` format", | |
'password': "Service Guide password (call the number `*105*00#`)" | |
}) | |
def balance(msisdn=None, password=None): | |
msisdn = os.environ.get('MEGABALANCE_MSISDN', msisdn) | |
password = os.environ.get('MEGABALANCE_PASSWORD', password) | |
if msisdn is None or password is None: | |
return "No `msisdn` and/or `password` provided." | |
headers = { | |
'content-type': "application/x-www-form-urlencoded" | |
} | |
params = { | |
'MSISDN': msisdn, | |
'PASSWORD': password, | |
} | |
response = requests.get(BALANCE_URL, params=params, headers=headers) | |
if not response.ok: | |
response.raise_for_status() | |
xml = ElementTree.fromstring(response.text) | |
return "{balance:.2f} rub".format(balance=float(xml.find("./GET_BALANCE/BALANCE").text)) | |
if __name__ == '__main__': | |
baker.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It’s highly outdated and doesn’t work anymore (for a few years maybe), but if there’re any new balance widgets (e.g. for macOS’ dashboard) by Megafon, then they can be used to find a new endpoint and data format.