Skip to content

Instantly share code, notes, and snippets.

@Aeron
Last active June 27, 2017 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aeron/09add5359c4b99e277d6 to your computer and use it in GitHub Desktop.
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*.
#!/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()
@Aeron
Copy link
Author

Aeron commented Jun 27, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment