#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Filename: mobilevikings.py
Version: 0.1
last change: 25/10/2009
Function: mobile vikings is an application that logs in into your
mobile vikings account and checks your credit information
The application s code (the GTK part) may look similar to the
kfzcheck made by Patrick Beck. I used his code as an example.
(Thank you Patrick Beck)
Copyright (C) 2009 Pieter Colpaert <pieter.colpaert at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more de
tails.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import pygtk
pygtk.require('2.0')
import gtk
import sys
import gettext
import urllib2
import json
APP = 'mobilevikings'
DIR = '/usr/share/locale'
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
_ = gettext.gettext
class Mobile(object):
def connect(self, widget,usr, pwd):
self.window.set_title(_('Logging in'))
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,
'https://www.mobilevikings.com',
usr.get_text(),
pwd.get_text())
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
try:
response = opener.open('https://www.mobilevikings.com/api/1.0/rest/mobilevikings/sim_balance.json')
json_str = response.read()
self.window.set_title(_('Mobile Vikings: Sim Balance of ' + usr.get_text()))
self.label.set_text(json_str)#TEMP
decoder = json.JSONDecoder()
decoded = decoder.decode(json_str);
self.label.set_text('Valid until: ' + str(decoded.get('valid_until')))
self.label2.set_text('Credits: ' + str(decoded.get('credits')))
self.label3.set_text('Free sms: ' + str(decoded.get('sms')))
self.label4.set_text('MB gprs left :' + str(decoded.get('data')/1024/1024))
except urllib2.HTTPError:
self.label.set_text('Password or username incorrect') # on 401 error
def connectOnStart(self, widget,usr, pwd):
self.window.set_title(_('Logging in'))
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,
'https://www.mobilevikings.com',
usr,
pwd)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
try:
response = opener.open('https://www.mobilevikings.com/api/1.0/rest/mobilevikings/sim_balance.json')
json_str = response.read()
self.window.set_title(_('Mobile Vikings: Sim Balance of ' + usr))
self.label.set_text(json_str)#TEMP
decoder = json.JSONDecoder()
decoded = decoder.decode(json_str);
self.label.set_text('Valid until: ' + str(decoded.get('valid_until')))
self.label2.set_text('Credits: ' + str(decoded.get('credits')))
self.label3.set_text('Free sms: ' + str(decoded.get('sms')))
self.label4.set_text('MB gprs left:' + str(decoded.get('data')/1024/1024))
except urllib2.HTTPError:
self.label.set_text('Password or username incorrect') # on 401 error
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
if len(sys.argv) == 3:
user_name = sys.argv[1]
pass_word = sys.argv[2]
elif len(sys.argv) == 2:
user_name = sys.argv[1]
pass_word = ''
else:
user_name = 'username'
pass_word = ''
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_size_request(480, 640)
self.window.set_title(_('Mobile Vikings'))
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
self.entry = gtk.Entry()
self.entry.set_text(user_name)
self.entry2 = gtk.Entry()
self.entry2.connect('activate', self.connect, self.entry, self.entry2)
self.entry2.set_text(pass_word)
self.entry2.set_visibility(False)
self.button = gtk.Button(_('Log in'))
self.button.connect('clicked', self.connect, self.entry, self.entry2)
self.box = gtk.VBox(False,2)
self.window.add(self.box)
self.hbox = gtk.HBox(False,2)
self.box.pack_start(self.hbox, False, False, 0)
self.label = gtk.Label(_('Please, be sure to log in first'))
self.box.pack_start(self.label, False, False,0)
self.label2 = gtk.Label()
self.box.pack_start(self.label2, False, False,0)
self.label3 = gtk.Label()
self.box.pack_start(self.label3, False, False,0)
self.label4 = gtk.Label()
self.box.pack_start(self.label4, False, False,0)
self.hbox.pack_start(self.entry, True, True, 2)
self.hbox.pack_start(self.entry2, False, False, 2)
self.hbox.pack_end(self.button, False, False, 2)
self.entry.show()
self.entry2.show()
self.button.show()
self.hbox.show()
self.label.show()
self.label2.show()
self.label3.show()
self.label4.show()
self.box.show()
self.window.show()
if len(sys.argv)==3:
self.connectOnStart('onStart', sys.argv[1],sys.argv[2])
def main():
gtk.main()
return 0
if __name__ == '__main__':
Mobile()
main()