Guest User

Untitled

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. import wx, threading, sys, time, os, sqlite3, tweepy, win32con, ctypes
  2. from systray import *
  3. from announce import *
  4. from subprocess import Popen
  5. from ctypes import wintypes
  6.  
  7. class myapp(wx.App):
  8. def __init__(self):
  9.  
  10. self.conn = sqlite3.connect("db.db")
  11. self.cursor = self.conn.cursor()
  12. self.twit = type('', (), {})()
  13. self.cursor.execute("SELECT * FROM `twit_acs`;")
  14. self.twit.isLoggedIn = self.cursor.fetchall()
  15. self.t = threading.Thread(target=self.detectHotKey)
  16. self.t.start()
  17. if len(self.twit.isLoggedIn)==0:
  18. self.twit.isLoggedIn = [()]
  19.  
  20. self.twit.count = len(self.twit.isLoggedIn[0])
  21. if self.twit.count != -1 and self.twit.count !=0:
  22. self.cursor.execute("SELECT * FROM `twit_acs` ORDER BY `id` DESC LIMIT 1;");
  23. self.twit.row = self.cursor.fetchall()
  24. self.twit.access_key = self.twit.row[0][1]
  25. self.twit.access_secret = self.twit.row[0][2]
  26.  
  27. self.twit.CONSUMER_KEY = "pbAI2h2cZMTFSGKrvNEOw"
  28. self.twit.CONSUMER_SECRET = "glK5eLmbsaKGI6eVvpnTYQIDQrsLn70Z2ntQmApAM"
  29. self.twit.auth = tweepy.OAuthHandler(self.twit.CONSUMER_KEY, self.twit.CONSUMER_SECRET)
  30. wx.App.__init__(self)
  31. self.frame = sysTray(icon='systray.ico', title="NowPlaying Announcer")
  32. self.addMenu(self.frame)
  33.  
  34. panel = self.layout(self.frame)
  35.  
  36. self.frame.Centre()
  37. self.frame.Show()
  38. self.MainLoop()
  39. return
  40.  
  41. def addMenu(self, master):
  42. menu = wx.MenuBar()
  43. fileMenu = wx.Menu()
  44. fileMenu.Append(1, "Exit")
  45. editMenu = wx.Menu()
  46. editMenu.Append(2, "Preferences")
  47. menu.Append(fileMenu, "File")
  48. menu.Append(editMenu, "Edit")
  49. master.SetMenuBar(menu)
  50. master.Bind(wx.EVT_MENU, master.OnCloseWindow, id=1)
  51.  
  52. def layout(self, master):
  53. panel = wx.Panel(master)
  54. self.mainSizer = wx.GridSizer(rows=2, cols=1, hgap=10, vgap=10)
  55. self.sSizer = wx.GridSizer(rows=1, cols=3, hgap=10, vgap=10)
  56. name = wx.StaticText(panel, wx.ID_ANY, "NowPlaying Announcer")
  57. self.titlefont = wx.Font(18, wx.NORMAL, wx.NORMAL, wx.NORMAL)
  58. name.SetFont(self.titlefont)
  59. self.mainSizer.Add(name, 0, wx.ALL | wx.ALIGN_CENTER, 10)
  60. self.mtitle = wx.StaticText(panel, wx.ID_ANY, "Donate Button PlaceHolder")
  61. self.sSizer.Add(self.mtitle, 0, wx.ALL | wx.ALIGN_CENTRE)
  62.  
  63. if self.twit.count==-1 or self.twit.count==0:
  64. self.twitbutton = wx.Button(panel, 1002, "Sign in to Twitter")
  65. self.twitbutton.Bind(wx.EVT_BUTTON, self.addTwitter)
  66. else:
  67. self.twitbutton = wx.Button(panel, wx.ID_ANY, "Sign out of Twitter")
  68. self.twitbutton.Bind(wx.EVT_BUTTON, self.handleDelTwit)
  69.  
  70. self.sSizer.Add(self.twitbutton, 0, wx.ALL | wx.ALIGN_CENTRE)
  71. self.malbum = wx.StaticText(panel, wx.ID_ANY, "Facebook Button PlaceHolder")
  72. self.sSizer.Add(self.malbum, 0, wx.ALL | wx.ALIGN_CENTRE)
  73.  
  74. self.sSizer.Fit(master)
  75.  
  76. self.mainSizer.Add(self.sSizer, 0, wx.ALL|wx.EXPAND, 10)
  77.  
  78. panel.SetSizer(self.mainSizer)
  79.  
  80. self.mainSizer.Fit(master)
  81.  
  82. return panel
  83.  
  84. def addTwitter(self, evt=None):
  85. self.twit.frame = wx.Frame(self.frame, title="Add Twitter Account")
  86. panel = wx.Panel(self.twit.frame)
  87. mainSizer = wx.GridSizer(rows=4, cols=1)
  88. sSizer = wx.GridSizer(rows=1, cols=2)
  89.  
  90. title = wx.StaticText(panel, wx.ID_ANY, "Add Twitter Account")
  91. 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)
  92. gototext.SetBackgroundColour("#e2e2e2")
  93. pintext = wx.StaticText(panel, wx.ID_ANY, "PIN:")
  94. self.pinTextCtrl = wx.TextCtrl(panel, wx.ID_ANY)
  95. title.SetFont(self.titlefont)
  96. subButton = wx.Button(panel, wx.ID_ANY, "Submit")
  97. subButton.Bind(wx.EVT_BUTTON, self.handleNewTwitter)
  98.  
  99. mainSizer.Add(title, 0, wx.ALL|wx.CENTRE, 10)
  100. mainSizer.Add(gototext, 0, wx.ALL|wx.EXPAND, 5)
  101. sSizer.Add(pintext, 0, wx.ALL|wx.CENTRE, 5)
  102. sSizer.Add(self.pinTextCtrl, 0, wx.ALL|wx.CENTRE, 5)
  103. mainSizer.Add(sSizer, 0, wx.ALL|wx.EXPAND, 5)
  104. mainSizer.Add(subButton, 0, wx.ALL|wx.EXPAND, 5)
  105. panel.SetSizer(mainSizer)
  106. self.twit.frame.Show()
  107. sSizer.Fit(self.twit.frame)
  108. mainSizer.Fit(self.twit.frame)
  109.  
  110. def handleNewTwitter(self, evt=None):
  111. pin = self.pinTextCtrl.GetValue()
  112. try:
  113. auth = self.twit.auth.get_access_token(pin)
  114. self.cursor.execute("""INSERT INTO twit_acs
  115. (id, access_key, access_secret)
  116. VALUES
  117. ("""+str(int(self.twit.count)+int(1))+""", '"""+self.twit.auth.access_token.key+"""', '"""+self.twit.auth.access_token.secret+"""');""");
  118. self.conn.commit()
  119. self.twitbutton.SetLabel("Sign out of Twitter")
  120. self.twitbutton.Bind(wx.EVT_BUTTON, self.handleDelTwit)
  121. self.twit.frame.Destroy()
  122. dial = wx.MessageDialog(parent=None, message="Logged in.", style=wx.OK)
  123. dial.ShowModal()
  124. except tweepy.error.TweepError:
  125. dial = wx.MessageDialog(parent=None, message="Invalid Pin", style=wx.OK)
  126. dial.ShowModal()
  127.  
  128. def handleDelTwit(self, evt=None):
  129. self.cursor.execute("DELETE FROM twit_acs")
  130. self.conn.commit()
  131. self.twitbutton.SetLabel("Sign in to Twitter")
  132. self.twitbutton.Bind(wx.EVT_BUTTON, self.addTwitter)
  133.  
  134. def detectHotKey(self, evt=None):
  135. byref = ctypes.byref
  136. user32 = ctypes.windll.user32
  137. HOTKEYS = {
  138. 1 : (ord("N"), win32con.MOD_WIN)
  139. }
  140. HOTKEY_ACTIONS = {
  141. 1 : self.handleHotKey
  142. }
  143. for id, (vk, modifiers) in HOTKEYS.items ():
  144. if not user32.RegisterHotKey (None, id, modifiers, vk):
  145. print "Unable to register id", id
  146.  
  147. try:
  148. msg = wintypes.MSG ()
  149. while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
  150. if msg.message == win32con.WM_HOTKEY:
  151. action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
  152. if action_to_take:
  153. action_to_take ()
  154.  
  155. user32.TranslateMessage (byref (msg))
  156. user32.DispatchMessageA (byref (msg))
  157.  
  158. finally:
  159. for id in HOTKEYS.keys ():
  160. user32.UnregisterHotKey (None, id)
  161.  
  162. def handleHotKey(self, evt=None):
  163. ann = announce()
  164. print ann.getTrack()
  165.  
  166. if __name__ == '__main__':
  167. myapp()
Add Comment
Please, Sign In to add comment