Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created August 6, 2018 21:58
Show Gist options
  • Save grimpy/4adf1afaae29b87acc5c5172d6b7d2e6 to your computer and use it in GitHub Desktop.
Save grimpy/4adf1afaae29b87acc5c5172d6b7d2e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from pyquery import PyQuery
from datetime import datetime
LIMITSELECTOR = ".QutaDetailsfixed > h3"
DAYSELECTOR = ".QutaDetailsfixed > h5"
TIMESELECTOR = ".QutaDetailsfixed > p"
def get_number(text):
if not isinstance(text, str):
text = text.text_content()
splits = text.split()
if splits[-1] == 'MB':
return int(splits[-2]) / 1024.
return float(splits[0])
class Vodafone:
def __init__(self):
self.url = "http://www.vodafone.com.eg/data/usbEntry.do?lang=en"
self.limit = 40
self.totaldays = 30
self.daysleft = 0
self.used = 0
def parse(self):
for _ in range(3):
try:
pq = PyQuery(url=self.url)
break
except:
continue
else:
return
day = pq(LIMITSELECTOR)
self.limit = get_number(day[0])
day = pq(DAYSELECTOR)
self.used = self.limit - get_number(day[0])
dayspan = pq(TIMESELECTOR)[0]
self.endday = int(get_number(dayspan.text_content().split(' ')[-2]))
now = datetime.now()
if self.endday > now.day:
daysleft = self.endday - now.day
else:
daysleft = (now.replace(day=self.endday, month=now.month+1) - now).days
self.daysleft = daysleft
def __str__(self):
return "{:.2f}/{} {} left".format(self.used, self.limit, self.daysleft)
if __name__ == '__main__':
vf = Vodafone()
vf.parse()
print(vf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment