Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wx, threading, sys, time, os, sqlite3, tweepy, win32con, ctypes
- from systray import *
- from announce import *
- from subprocess import Popen
- from ctypes import wintypes
- class myapp(wx.App):
- def __init__(self):
- self.conn = sqlite3.connect("db.db")
- self.cursor = self.conn.cursor()
- self.twit = type('', (), {})()
- self.cursor.execute("SELECT * FROM `twit_acs`;")
- self.twit.isLoggedIn = self.cursor.fetchall()
- self.t = threading.Thread(target=self.detectHotKey)
- self.t.start()
- if len(self.twit.isLoggedIn)==0:
- self.twit.isLoggedIn = [()]
- self.twit.count = len(self.twit.isLoggedIn[0])
- if self.twit.count != -1 and self.twit.count !=0:
- self.cursor.execute("SELECT * FROM `twit_acs` ORDER BY `id` DESC LIMIT 1;");
- self.twit.row = self.cursor.fetchall()
- self.twit.access_key = self.twit.row[0][1]
- self.twit.access_secret = self.twit.row[0][2]
- self.twit.CONSUMER_KEY = "pbAI2h2cZMTFSGKrvNEOw"
- self.twit.CONSUMER_SECRET = "glK5eLmbsaKGI6eVvpnTYQIDQrsLn70Z2ntQmApAM"
- self.twit.auth = tweepy.OAuthHandler(self.twit.CONSUMER_KEY, self.twit.CONSUMER_SECRET)
- wx.App.__init__(self)
- self.frame = sysTray(icon='systray.ico', title="NowPlaying Announcer")
- self.addMenu(self.frame)
- panel = self.layout(self.frame)
- self.frame.Centre()
- self.frame.Show()
- self.MainLoop()
- return
- def addMenu(self, master):
- menu = wx.MenuBar()
- fileMenu = wx.Menu()
- fileMenu.Append(1, "Exit")
- editMenu = wx.Menu()
- editMenu.Append(2, "Preferences")
- menu.Append(fileMenu, "File")
- menu.Append(editMenu, "Edit")
- master.SetMenuBar(menu)
- master.Bind(wx.EVT_MENU, master.OnCloseWindow, id=1)
- def layout(self, master):
- panel = wx.Panel(master)
- self.mainSizer = wx.GridSizer(rows=2, cols=1, hgap=10, vgap=10)
- self.sSizer = wx.GridSizer(rows=1, cols=3, hgap=10, vgap=10)
- name = wx.StaticText(panel, wx.ID_ANY, "NowPlaying Announcer")
- self.titlefont = wx.Font(18, wx.NORMAL, wx.NORMAL, wx.NORMAL)
- name.SetFont(self.titlefont)
- self.mainSizer.Add(name, 0, wx.ALL | wx.ALIGN_CENTER, 10)
- self.mtitle = wx.StaticText(panel, wx.ID_ANY, "Donate Button PlaceHolder")
- self.sSizer.Add(self.mtitle, 0, wx.ALL | wx.ALIGN_CENTRE)
- if self.twit.count==-1 or self.twit.count==0:
- self.twitbutton = wx.Button(panel, 1002, "Sign in to Twitter")
- self.twitbutton.Bind(wx.EVT_BUTTON, self.addTwitter)
- else:
- self.twitbutton = wx.Button(panel, wx.ID_ANY, "Sign out of Twitter")
- self.twitbutton.Bind(wx.EVT_BUTTON, self.handleDelTwit)
- self.sSizer.Add(self.twitbutton, 0, wx.ALL | wx.ALIGN_CENTRE)
- self.malbum = wx.StaticText(panel, wx.ID_ANY, "Facebook Button PlaceHolder")
- self.sSizer.Add(self.malbum, 0, wx.ALL | wx.ALIGN_CENTRE)
- self.sSizer.Fit(master)
- self.mainSizer.Add(self.sSizer, 0, wx.ALL|wx.EXPAND, 10)
- panel.SetSizer(self.mainSizer)
- self.mainSizer.Fit(master)
- return panel
- def addTwitter(self, evt=None):
- self.twit.frame = wx.Frame(self.frame, title="Add Twitter Account")
- panel = wx.Panel(self.twit.frame)
- mainSizer = wx.GridSizer(rows=4, cols=1)
- sSizer = wx.GridSizer(rows=1, cols=2)
- title = wx.StaticText(panel, wx.ID_ANY, "Add Twitter Account")
- gototext = wx.TextCtrl(panel, wx.ID_ANY, "Go to "+self.twit.auth.get_authorization_url()+" in your web browser and put the PIN in the box below.", style=wx.TE_READONLY|wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_WORDWRAP|wx.TE_AUTO_URL|wx.CENTRE)
- gototext.SetBackgroundColour("#e2e2e2")
- pintext = wx.StaticText(panel, wx.ID_ANY, "PIN:")
- self.pinTextCtrl = wx.TextCtrl(panel, wx.ID_ANY)
- title.SetFont(self.titlefont)
- subButton = wx.Button(panel, wx.ID_ANY, "Submit")
- subButton.Bind(wx.EVT_BUTTON, self.handleNewTwitter)
- mainSizer.Add(title, 0, wx.ALL|wx.CENTRE, 10)
- mainSizer.Add(gototext, 0, wx.ALL|wx.EXPAND, 5)
- sSizer.Add(pintext, 0, wx.ALL|wx.CENTRE, 5)
- sSizer.Add(self.pinTextCtrl, 0, wx.ALL|wx.CENTRE, 5)
- mainSizer.Add(sSizer, 0, wx.ALL|wx.EXPAND, 5)
- mainSizer.Add(subButton, 0, wx.ALL|wx.EXPAND, 5)
- panel.SetSizer(mainSizer)
- self.twit.frame.Show()
- sSizer.Fit(self.twit.frame)
- mainSizer.Fit(self.twit.frame)
- def handleNewTwitter(self, evt=None):
- pin = self.pinTextCtrl.GetValue()
- try:
- auth = self.twit.auth.get_access_token(pin)
- self.cursor.execute("""INSERT INTO twit_acs
- (id, access_key, access_secret)
- VALUES
- ("""+str(int(self.twit.count)+int(1))+""", '"""+self.twit.auth.access_token.key+"""', '"""+self.twit.auth.access_token.secret+"""');""");
- self.conn.commit()
- self.twitbutton.SetLabel("Sign out of Twitter")
- self.twitbutton.Bind(wx.EVT_BUTTON, self.handleDelTwit)
- self.twit.frame.Destroy()
- dial = wx.MessageDialog(parent=None, message="Logged in.", style=wx.OK)
- dial.ShowModal()
- except tweepy.error.TweepError:
- dial = wx.MessageDialog(parent=None, message="Invalid Pin", style=wx.OK)
- dial.ShowModal()
- def handleDelTwit(self, evt=None):
- self.cursor.execute("DELETE FROM twit_acs")
- self.conn.commit()
- self.twitbutton.SetLabel("Sign in to Twitter")
- self.twitbutton.Bind(wx.EVT_BUTTON, self.addTwitter)
- def detectHotKey(self, evt=None):
- byref = ctypes.byref
- user32 = ctypes.windll.user32
- HOTKEYS = {
- 1 : (ord("N"), win32con.MOD_WIN)
- }
- HOTKEY_ACTIONS = {
- 1 : self.handleHotKey
- }
- for id, (vk, modifiers) in HOTKEYS.items ():
- if not user32.RegisterHotKey (None, id, modifiers, vk):
- print "Unable to register id", id
- try:
- msg = wintypes.MSG ()
- while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
- if msg.message == win32con.WM_HOTKEY:
- action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
- if action_to_take:
- action_to_take ()
- user32.TranslateMessage (byref (msg))
- user32.DispatchMessageA (byref (msg))
- finally:
- for id in HOTKEYS.keys ():
- user32.UnregisterHotKey (None, id)
- def handleHotKey(self, evt=None):
- ann = announce()
- print ann.getTrack()
- if __name__ == '__main__':
- myapp()
Add Comment
Please, Sign In to add comment