Created
June 28, 2018 18:02
-
-
Save frafra/d766d536f2ab0e1c9f13cd8b2eac0955 to your computer and use it in GitHub Desktop.
iliad.it basic account information parser
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 python3 | |
import bs4 # fades beautifulsoup4 | |
import hug # fades | |
import humanfriendly # fades | |
import requests # fades | |
import datetime | |
import string | |
# https://github.com/xolox/python-humanfriendly/issues/29 | |
import copy | |
parse_timespan = copy.copy(humanfriendly.parse_timespan) | |
humanfriendly.parse_timespan = lambda s: sum(map(parse_timespan, s.split())) | |
BASE = "https://www.iliad.it/account/" | |
def extractDigits(text): | |
result = "" | |
for character in text: | |
if character in string.digits: | |
result += character | |
return result | |
parser = { | |
1: lambda s: {'calls': humanfriendly.parse_timespan(s)}, | |
3: lambda s: {'SMS': int(extractDigits(s))}, | |
5: lambda s: {'data': humanfriendly.parse_size(s.replace(",", "."))}, | |
9: lambda s: {'MMS': int(extractDigits(s))}, | |
} | |
@hug.cli() | |
@hug.get("/iliad", output=hug.output_format.json) | |
def iliad(username: hug.types.text, password: hug.types.text): | |
""" iliad.it basic account information parser """ | |
data = {'login-ident': username, 'login-pwd': password} | |
response = requests.post(BASE, data=data) | |
soup = bs4.BeautifulSoup(response.text, 'html.parser') | |
highlighted = soup.find_all(class_='red') | |
response = {} | |
for index, item in enumerate(highlighted): | |
if index in parser: | |
information = parser[index](item.text) | |
response.update(information) | |
phone_field = soup.find_all(class_='smaller')[3] | |
phone_number = extractDigits(phone_field.text) | |
response['phone_number'] = phone_number | |
return response | |
if __name__ == '__main__': | |
iliad.interface.cli() |
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
bs4 | |
hug | |
humanfriendly | |
requests |
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
from iliad import __hug_wsgi__ as application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment