Advertisement
Guest User

Pyhon Megafon Module

a guest
Jul 28th, 2015
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. # -*- coding: utf8 -*-
  2. __author__ = 'n00bcoder'
  3. '''module to create on-screen widget to show MegaFon balance from corporate account and switch cellular lines to IP mode
  4.  Module will be very useful to those, who have IP-PBX with many connected lines and need to see balance in real-time, to control expences
  5.  and to stop SIP-attacks in time, for example, when you see, that your balance spends nearly at the light speed you could easily go and power off
  6.  your PBX-server. With a gun, if you would like to.=)'''
  7. import urllib
  8. import urllib2
  9. import lxml
  10. import lxml.html
  11. import cookielib
  12. import Tkinter
  13.  
  14.  
  15. class App: # It is a class-based application
  16.     def __init__(self):
  17.         self.root = Tkinter.Tk() # It is a tk application
  18.         self.root.attributes("-toolwindow", 1) # With a single border and without buttons except "close"
  19.         self.root.wm_attributes("-topmost", 1)  # And always topmost (except most topmost cases, of course =))
  20.         self.var = Tkinter.StringVar() #And with a var to get text from outside
  21.         self.label = Tkinter.Label(self.root, textvariable=self.var) # With a label, of course, to show your precious balance (mah precious, kekeke)
  22.         self.response=urllib2.urlopen('https://sm.megafon.ru/sm/client/routing/[email protected]&password=somepass&routing=1') # With an ability to change line state into LINE_STATE_SIP
  23.         self.response=urllib2.urlopen('https://sm.megafon.ru/sm/client/routing/[email protected]&password=somepass&routing=1') # Not even one line state =)
  24.         self.log = 'CP_9241234567' # With a built-in login (will be in separate config file on the next version)
  25.         self.pas = '50me10nnandstr0NNPas5w0rd' # And built-in password (will be in separate config file too)
  26.         self.cookie = cookielib.CookieJar() # With an ability to store cookies
  27.         self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie)) # And process them
  28.         urllib2.install_opener(self.opener)
  29.         self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0',
  30.                         'Accept': 'application/json, text/javascript, */*; q=0.01',
  31.                         'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
  32.                         'Accept-Encoding': 'gzip, deflate',
  33.                         'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  34.                         'X-Requested-With': 'XMLHttpRequest'} # It can even transfer headers, like Mozilla do!
  35.         self.values = {'j_username': self.log, 'j_password': self.pas} # And $POST parameters too, Isn't it cool?
  36.         self.data = urllib.urlencode(self.values) # Of course it can encode $POST too
  37.         self.req = urllib2.Request('https://dv.b2blk.megafon.ru/sc_cp_apps/loginProcess', self.data, self.headers)# And compile all theese cool small things in one request, yay!
  38.         self.response = urllib2.urlopen(self.req) # 0pen this request
  39.         self.updater() # And process it by his own methods
  40.         self.root.mainloop()
  41.  
  42.     def updater(self):
  43.         self.response = urllib2.urlopen(self.req)
  44.         self.response = urllib2.urlopen('https://dv.b2blk.megafon.ru/sc_cp_apps/account/accountInfo/19435519')
  45.         site1 = self.response.read()
  46.         site_u = lxml.html.document_fromstring(site1)
  47.         balance = site_u.xpath('//span[@class="money bold"]/text()')
  48.         self.var.set(u'Баланс аккаунта: '+balance[0])
  49.         self.label.pack()
  50.         self.root.update_idletasks()
  51.         self.root.after(1000, self.updater)
  52. app = App()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement