Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

pieterc

By: a guest on Oct 28th, 2009  |  syntax: Python  |  size: 5.63 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Filename: mobilevikings.py
  5. Version: 0.1
  6. last change: 25/10/2009
  7. Function: mobile vikings is an application that logs in into your
  8. mobile vikings account and checks your credit information
  9.  
  10. The application s code (the GTK part) may look similar to the
  11. kfzcheck made by Patrick Beck. I used his code as an example.
  12. (Thank you Patrick Beck)
  13.  
  14. Copyright (C) 2009 Pieter Colpaert <pieter.colpaert at gmail dot com>  
  15.  
  16. This program is free software: you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation, either version 3 of the License, or
  19. any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more de
  25. tails.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  29. '''
  30.  
  31. import pygtk
  32. pygtk.require('2.0')
  33. import gtk
  34. import sys
  35. import gettext
  36. import urllib2
  37. import json
  38.  
  39. APP = 'mobilevikings'
  40. DIR = '/usr/share/locale'
  41. gettext.bindtextdomain(APP, DIR)
  42. gettext.textdomain(APP)
  43. _ = gettext.gettext
  44.  
  45. class Mobile(object):
  46.     def connect(self, widget,usr, pwd):
  47.         self.window.set_title(_('Logging in'))
  48.         password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  49.         password_mgr.add_password(None,
  50.                         'https://www.mobilevikings.com',
  51.                         usr.get_text(),
  52.                         pwd.get_text())
  53.         auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
  54.         opener = urllib2.build_opener(auth_handler)
  55.         try:
  56.                 response = opener.open('https://www.mobilevikings.com/api/1.0/rest/mobilevikings/sim_balance.json')
  57.                 json_str = response.read()
  58.                 self.window.set_title(_('Mobile Vikings: Sim Balance of ' + usr.get_text()))
  59.                 self.label.set_text(json_str)#TEMP
  60.                 decoder = json.JSONDecoder()
  61.                 decoded = decoder.decode(json_str);
  62.                 self.label.set_text('Valid until: ' + str(decoded.get('valid_until')))
  63.                 self.label2.set_text('Credits: ' + str(decoded.get('credits')))
  64.                 self.label3.set_text('Free sms: ' + str(decoded.get('sms')))
  65.                 self.label4.set_text('MB gprs left :' + str(decoded.get('data')/1024/1024))
  66.         except urllib2.HTTPError:
  67.                 self.label.set_text('Password or username incorrect') # on 401 error
  68.  
  69.     def connectOnStart(self, widget,usr, pwd):
  70.         self.window.set_title(_('Logging in'))
  71.         password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  72.         password_mgr.add_password(None,
  73.                         'https://www.mobilevikings.com',
  74.                         usr,
  75.                         pwd)
  76.         auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
  77.         opener = urllib2.build_opener(auth_handler)
  78.         try:
  79.                 response = opener.open('https://www.mobilevikings.com/api/1.0/rest/mobilevikings/sim_balance.json')
  80.                 json_str = response.read()
  81.                 self.window.set_title(_('Mobile Vikings: Sim Balance of ' + usr))
  82.                 self.label.set_text(json_str)#TEMP
  83.                 decoder = json.JSONDecoder()
  84.                 decoded = decoder.decode(json_str);
  85.                 self.label.set_text('Valid until: ' + str(decoded.get('valid_until')))
  86.                 self.label2.set_text('Credits: ' + str(decoded.get('credits')))
  87.                 self.label3.set_text('Free sms: ' + str(decoded.get('sms')))
  88.                 self.label4.set_text('MB gprs left:' + str(decoded.get('data')/1024/1024))
  89.         except urllib2.HTTPError:
  90.                 self.label.set_text('Password or username incorrect') # on 401 error
  91.  
  92.  
  93.     def delete_event(self, widget, event, data=None):
  94.         return False
  95.  
  96.     def destroy(self, widget, data=None):
  97.         gtk.main_quit()
  98.        
  99.     def __init__(self):
  100.         if len(sys.argv) == 3:
  101.             user_name = sys.argv[1]
  102.             pass_word = sys.argv[2]
  103.         elif len(sys.argv) == 2:
  104.             user_name = sys.argv[1]
  105.             pass_word = ''
  106.         else:
  107.             user_name = 'username'
  108.             pass_word = ''
  109.  
  110.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  111.         self.window.set_size_request(480, 640)
  112.         self.window.set_title(_('Mobile Vikings'))
  113.         self.window.connect('delete_event', self.delete_event)
  114.         self.window.connect('destroy', self.destroy)
  115.  
  116.         self.entry = gtk.Entry()
  117.         self.entry.set_text(user_name)
  118.         self.entry2 = gtk.Entry()
  119.         self.entry2.connect('activate', self.connect, self.entry, self.entry2)
  120.         self.entry2.set_text(pass_word)
  121.         self.entry2.set_visibility(False)
  122.         self.button = gtk.Button(_('Log in'))
  123.         self.button.connect('clicked', self.connect, self.entry, self.entry2)
  124.        
  125.         self.box = gtk.VBox(False,2)
  126.         self.window.add(self.box)
  127.         self.hbox = gtk.HBox(False,2)
  128.         self.box.pack_start(self.hbox, False, False, 0)
  129.  
  130.         self.label  = gtk.Label(_('Please, be sure to log in first'))
  131.         self.box.pack_start(self.label, False, False,0)
  132.         self.label2 = gtk.Label()
  133.         self.box.pack_start(self.label2, False, False,0)
  134.         self.label3 = gtk.Label()
  135.         self.box.pack_start(self.label3, False, False,0)
  136.         self.label4 = gtk.Label()
  137.         self.box.pack_start(self.label4, False, False,0)
  138.  
  139.         self.hbox.pack_start(self.entry, True, True, 2)
  140.         self.hbox.pack_start(self.entry2, False, False, 2)
  141.         self.hbox.pack_end(self.button, False, False, 2)
  142.        
  143.         self.entry.show()
  144.         self.entry2.show()
  145.         self.button.show()
  146.         self.hbox.show()
  147.         self.label.show()
  148.         self.label2.show()
  149.         self.label3.show()
  150.         self.label4.show()
  151.         self.box.show()
  152.         self.window.show()
  153.         if len(sys.argv)==3:
  154.             self.connectOnStart('onStart', sys.argv[1],sys.argv[2])
  155.  
  156.  
  157. def main():
  158.     gtk.main()
  159.     return 0
  160. if __name__ == '__main__':
  161.     Mobile()
  162.     main()