Last active
June 7, 2017 04:59
-
-
Save hightemp/6cad81d1956ac5b6a97e70a234f20551 to your computer and use it in GitHub Desktop.
[Python] Tray informer (Internet access, Geo, IP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF8 -*- | |
import geoip as mGeoIP | |
import wx as mWX | |
import urllib2 as mURLLib2 | |
import copy as mCopy | |
import re as mRegularExpression | |
import threading as mThreading | |
class TTaskBarIcon(mWX.TaskBarIcon): | |
objFrame = None | |
objSettingsWindow = None | |
objApplication = None | |
aMenu = None | |
def __init__(self, in_objApplication): | |
self.objApplication = in_objApplication | |
super(TTaskBarIcon, self).__init__() | |
self.fnSetIcon() | |
# self.fnCreatePopupMenu() | |
self.fnUpdate() | |
def fnUpdate(self): | |
self.aMenu = list() | |
try: | |
sHTML = mURLLib2.urlopen('http://myip.ru').read() | |
if sHTML.index('ipcontent')>-1: | |
self.fnSetIcon(True) | |
else: | |
self.fnSetIcon(False) | |
except Exception as objException: | |
print objException | |
finally: | |
aPublicIPCheckerHosts = [ | |
'http://icanhazip.com', | |
'http://bot.whatismyipaddress.com', | |
'http://ipinfo.io/ip' | |
] | |
for sItem in aPublicIPCheckerHosts: | |
try: | |
sIPResponse = mURLLib2.urlopen(sItem).read() | |
sIPResponse = unicode(sIPResponse) | |
sIPResponse = mRegularExpression.sub(r'[^\d.]', '', sIPResponse) | |
self.aMenu += [sIPResponse] | |
sGEOIPResponse = mGeoIP.geolite2.lookup(sIPResponse) | |
sGEOIPResponse = unicode(sGEOIPResponse).encode("utf8") | |
objMatch = mRegularExpression.search( | |
ur"timezone='([^']+)'", | |
sGEOIPResponse, | |
mRegularExpression.UNICODE | | |
mRegularExpression.VERBOSE | |
) | |
sGEOIPResponse = objMatch.group(1) | |
self.aMenu += [sGEOIPResponse] | |
except Exception, objException: | |
print objException.message | |
finally: | |
break | |
self.aMenu += ['-'] | |
self.objTimer = mThreading.Timer(300, self.fnUpdate) | |
self.objTimer.start() | |
def CreatePopupMenu(self): | |
objMenu = mWX.Menu() | |
for sItem in self.aMenu: | |
if sItem == "-": | |
objMenu.AppendSeparator() | |
else: | |
self.fnCreateMenuItem(objMenu, sItem, None) | |
self.fnCreateMenuItem(objMenu, 'Exit', self.fnExit) | |
return objMenu | |
def fnCreateMenuItem(self, in_objMenu, in_sLabel, in_objFunction): | |
objItem = mWX.MenuItem(in_objMenu, -1, in_sLabel) | |
in_objMenu.Bind(mWX.EVT_MENU, in_objFunction, id=objItem.GetId()) | |
in_objMenu.AppendItem(objItem) | |
return objItem | |
def fnSetIcon(self, bAccess = False): | |
sIconName = 'red-icon.png' | |
if bAccess is True: | |
sIconName = 'green-icon.png' | |
objIcon = mWX.IconFromBitmap(mWX.Bitmap(sIconName)) | |
self.SetIcon(objIcon, 'Tray informer') | |
def fnExit(self, in_objEvent): | |
objDialog = mWX.MessageDialog(None, 'Are you sure you want to quit?', 'Quit', mWX.YES_NO) | |
if objDialog.ShowModal() == mWX.ID_YES: | |
mWX.CallAfter(self.Destroy) | |
self.objApplication.objFrame.Destroy() | |
class TApplication(mWX.App): | |
objFrame = None | |
def OnInit(self): | |
self.objFrame = mWX.Frame(None) | |
TTaskBarIcon(self) | |
return True | |
if __name__ == "__main__": | |
objApplication = TApplication(False) | |
objApplication.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment