Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created April 25, 2018 22:49
Show Gist options
  • Save grimpy/bd259a0f60892d8003c5fbcf1cfe8d28 to your computer and use it in GitHub Desktop.
Save grimpy/bd259a0f60892d8003c5fbcf1cfe8d28 to your computer and use it in GitHub Desktop.
import requests
import copy
import time
TOKENURL = 'https://api-my.te.eg/api/user/generatetoken?channelId=WEB_APP'
STATUSURL = 'https://api-my.te.eg/api/user/status'
LOGINURL = 'https://api-my.te.eg/api/user/login?channelId=WEB_APP'
DATAURL = 'https://api-my.te.eg/api/line/freeunitusage'
STATUSDATA = {
"header": {
"timestamp": 0,
"customerId": "",
"msisdn": "",
"messageCode": "",
"locale": "En"
},
"body": {}
}
LOGINDATA = {
"header": {
"msisdn": "",
"timestamp": "",
"locale": "En"
},
"body": {
"password": ""
}
}
class TelecomEgypt:
def __init__(self, username, password):
self.username = username
self.customerId = None
self.password = password
self.session = requests.Session()
def login(self):
resp = self.session.get(TOKENURL)
responsedata = self._validate_response(resp, 'Failed to get token')
self.session.headers['Jwt'] = responsedata['body']['jwt']
statusdata = copy.deepcopy(STATUSDATA)
statusdata['header']['msisdn'] = self.username
resp = self.session.post(STATUSURL, json=statusdata)
responsedata = self._validate_response(resp, 'Failed to get status')
logindata = copy.deepcopy(LOGINDATA)
logindata['header']['msisdn'] = self.username
logindata['header']['timestamp'] = str(responsedata['header']['timstamp'])
logindata['body']['password'] = self.password
resp = self.session.post(LOGINURL, json=logindata)
responsedata = self._validate_response(resp, 'Failed to login')
self.session.headers['Jwt'] = responsedata['body']['jwt']
self.customerId = responsedata['header']['customerId']
def _validate_response(self, response, message):
response.raise_for_status()
responsedata = response.json()
if str(responsedata['header']['responseCode']) != "0":
raise RuntimeError('{}: {}'.format(message, responsedata['header']['responseMessage']))
return responsedata
def get_data(self):
postdata = {
'header': {
'customerId': self.customerId,
'msisdn': self.username,
'locale': 'En'
},
'body': {}
}
resp = self.session.post(DATAURL, json=postdata)
return self._validate_response(resp, 'Failed to get data')
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-p', '--password')
options = parser.parse_args()
inet = myte(options.user, options.password)
inet.login()
print(inet.get_data())
@kim0
Copy link

kim0 commented Aug 22, 2022

@grimpy holly smokes, can't believe I just ran it you :D Cheers man!
I was looking into a way to talk to TE API. Apparently they now don't send the password as is! The browser seems to be munging it somehow. If you're still kinda interested in this, would be great if you can update it. Salam 😚

@grimpy
Copy link
Author

grimpy commented Aug 22, 2022

Yes you are right it's always been like this, as far as I remember I captured the 'password' which is as bas64 of some binary version of your password, i captured the password once through my browser inspect and have been using it ever since (it doesn't change).

This code is actually a proof of concept and was integrated into https://github.com/grimpy/ispapi/blob/master/ispapi/providers/telecomegypt.py with the same restrictions.

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