Last active
August 29, 2015 14:02
-
-
Save Roger/ef94eb8c796c78bf297e to your computer and use it in GitHub Desktop.
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
try: | |
from urllib.parse import quote | |
except ImportError: | |
# Python 2 | |
from urllib import quote | |
import requests | |
# Session | |
ssomobile = "https://ssomobile.personal.com.ar/loginMobile/" | |
session_url = ssomobile + "sessionManager/json/createSession" | |
logout_url = ssomobile + "sessionManager/json/logout" | |
# EndPoints | |
apim = "https://apim.personal.com.ar/v1//me" | |
balance_url = apim + "/balance" | |
club_url = apim + "/club" | |
packs_units_balance_url = apim + "/packs/packs_and_unitsbalance" | |
plan_url = apim + "/plan" | |
me_url = apim | |
class LoginException(Exception): | |
pass | |
class GetException(Exception): | |
pass | |
class Personal(object): | |
def __init__(self, phone, pin=None, token=None): | |
assert pin or token | |
self.phone = phone | |
self.pin = pin | |
self.headers = {"user-agent": None} | |
self.token = token | |
@property | |
def token(self): | |
return self._token | |
@token.setter | |
def token(self, value): | |
self._token = value | |
if value: | |
self.headers["Authorization"] = "Bearer %s" % quote(value) | |
else: | |
self.headers.pop("Authorization", None) | |
def login(self): | |
data = {"applicationId": "ar.com.personal", | |
"applicationSecret": "be9d74e49ce90f5cbfc255204998f817", | |
"hasCaptcha": "false", | |
"captchaText": "", | |
"tempToken": "null", | |
"msisdn": self.phone, | |
"pin": self.pin, | |
"remember": "true"} | |
response = requests.post(session_url, data=data, headers=self.headers) | |
if not response.ok: | |
raise LoginException(repr(response.text)) | |
data_response = response.json() | |
if data_response.get("status", {"code": None})["code"] != "200" or not \ | |
data_response.get("token", None): | |
raise LoginException(repr(data_response)) | |
self.token = data_response["token"] | |
def logout(self): | |
if self.token: | |
requests.post(logout_url, {"token": self.token}, | |
headers=self.headers) | |
self.token = None | |
def make_url(url): | |
def get(self): | |
if not self.token: | |
self.login() | |
response = requests.get(url, headers=self.headers) | |
if not response.ok: | |
raise GetException(repr(response.text)) | |
data_response = response.json() | |
if data_response.get("code", None): | |
raise GetException(repr(data_response)) | |
return data_response | |
return get | |
# End Points | |
balance = make_url(balance_url) | |
club = make_url(club_url) | |
plan = make_url(plan_url) | |
me = make_url(me_url) | |
packs_units = make_url(packs_units_balance_url) | |
def test_demo(personal): | |
from pprint import pprint | |
print("Balance") | |
pprint(personal.balance()) | |
print("Club") | |
pprint(personal.club()) | |
print("Plan") | |
pprint(personal.plan()) | |
print("me") | |
pprint(personal.me()) | |
print("Packs & Units") | |
pprint(personal.packs_units()) | |
def debug(): | |
import logging | |
try: | |
import http.client as http_client | |
except ImportError: | |
# Python 2 | |
import httplib as http_client | |
http_client.HTTPConnection.debuglevel = 1 | |
logging.basicConfig() | |
logging.getLogger().setLevel(logging.DEBUG) | |
requests_log = logging.getLogger("requests.packages.urllib3") | |
requests_log.setLevel(logging.DEBUG) | |
requests_log.propagate = True | |
if __name__ == "__main__": | |
import sys | |
import atexit | |
personal = Personal(sys.argv[1], sys.argv[2]) | |
debug() | |
try: | |
test_demo(personal) | |
except (LoginException, GetException) as e: | |
print(e) | |
atexit.register(lambda: personal.logout()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment