Skip to content

Instantly share code, notes, and snippets.

@py-ranoid
Created October 26, 2018 03:23
Show Gist options
  • Save py-ranoid/89684b077d5cde237f14c054e80ad123 to your computer and use it in GitHub Desktop.
Save py-ranoid/89684b077d5cde237f14c054e80ad123 to your computer and use it in GitHub Desktop.
from urllib2 import urlopen
from bs4 import BeautifulSoup
import pickle
import time
def getPlan(html):
return sum(float(i.text.split(' ')[0]) for i in html.find_all('div', attrs={'class': 'description'})[0].find_all('span'))
def getRem(html):
return float(html.find_all('div', attrs={'class': 'description'})[1].p.span.text.split(' ')[0])
def getDays(html):
return int(html.find_all('div', attrs={'class': 'description'})[2].p.span.text.split(' ')[0])
def gist(remaining, days):
print '-' * 60 + '\nQuota left :\t' + str(remaining) + ' GB\n' + 'Days left :\t' + str(days) + ' days'
redtime = days + (24 - float(time.strftime('%H')) +
float(time.strftime('%H')) / 60) / 24
economy = round(remaining / redtime, 2)
print 'Economy :\t', economy, 'GB/day\n' + '-' * 60
return economy
def fromPickle():
print 'Left', '\t', 'Used', '\t', 'Days', '\t', 'Economy', '\t', 'Time'
with open('/home/b/data.pkl', 'rb') as inp:
while True:
try:
data = pickle.load(inp)
print data[0], '\t', data[1], '\t', data[2], '\t', data[3], '\t\t', data[4]
except EOFError:
last_days = data[2]
break
return last_days
def toPickle(plan, remaining, days, economy):
last_days = fromPickle()
usage = plan - remaining
mode = 'w' if last_days < days else 'a'
with open('/home/b/data.pkl', mode) as output:
date = time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%y")
store = [remaining, usage, days, economy, date]
print remaining, '\t', usage, '\t', days, '\t', economy, '\t\t', date
pickle.dump(store, output)
if __name__ == '__main__':
prime_url = BeautifulSoup(urlopen('http://www.airtel.in/smartbyte-s/page.html').read(),
"html.parser").find('iframe', attrs={'width': '100%'}).get('src')
html = BeautifulSoup(urlopen(prime_url).read(), "html.parser").find(
'ul', attrs={'class': 'list1 clearfix'})
plan = getPlan(html)
remaining = getRem(html)
days = getDays(html)
eco = gist(remaining, days)
toPickle(plan, remaining, days, eco)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment