-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAIN.py
220 lines (192 loc) · 8.09 KB
/
MAIN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# © Designed and Developed by Mehmet Güdük.
# © Licensed with GPL-3.0 License, Author is Mehmet Güdük.
import time
import sys
import os
import win32gui
from database_functions import *
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMenu, QAction, qApp, QSystemTrayIcon
from PyQt5.QtCore import QThread, pyqtSignal
from interface import Ui_MainWindow
from PyQt5.QtGui import QIcon
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class Blocker(QThread):
block = pyqtSignal()
playing = pyqtSignal(tuple)
count = pyqtSignal(int)
def __init__(self):
super().__init__()
def run(self):
while True:
import ctypes
from pycaw.pycaw import AudioUtilities
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(buff.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
sessions = AudioUtilities.GetAllSessions()
if "Advertisement" in titles:
for session in sessions:
try:
if 'Spotify' in session.Process.name():
volume = session.SimpleAudioVolume
volume.SetMute(1, None)
self.block.emit()
empty_tuple = ("","")
self.playing.emit(empty_tuple)
self.count.emit(1)
except AttributeError:
pass
else:
self.count.emit(0)
for session in sessions:
try:
if 'Spotify' in session.Process.name():
volume = session.SimpleAudioVolume
volume.SetMute(0, None)
try:
self.playing.emit(self.catch_song_name())
except:
pass
except AttributeError:
pass
time.sleep(0.2)
def catch_song_name(self):
list_of_windows = []
finding_window = win32gui.FindWindow("SpotifyMainWindow", None)
found = win32gui.GetWindowText(finding_window)
def find_spotify_uwp(hwnd, list_of_windows):
text_of_windows = win32gui.GetWindowText(hwnd)
catched_classname = win32gui.GetClassName(hwnd)
if catched_classname == "Chrome_WidgetWin_0" and len(text_of_windows) > 0:
list_of_windows.append(text_of_windows)
if found:
list_of_windows.append(found)
else:
win32gui.EnumWindows(find_spotify_uwp, list_of_windows)
if len(list_of_windows) == 0:
pass
try:
artist, songname = list_of_windows[0].split(" - ", 1)
except ValueError:
artist = ''
songname = list_of_windows[0]
except IndexError:
pass
return songname, artist
class myApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.blocker = Blocker()
self.blocker.block.connect(ADDING_LAST_BLOCKED_TIME)
self.blocker.block.connect(self.changingLabels)
self.blocker.playing.connect(self.song)
self.blocker.count.connect(self.blockCounter_tool_one)
self.blocker.start()
DB_CONNECT()
DB_TABLES()
self.changingLabels()
if DB_CHECKBOX() == "CHECKED":
self.ui.checkbox.setChecked(True)
try:
spotify_location = str(os.getenv('APPDATA')) + "/Spotify/Spotify.exe"
os.startfile(spotify_location)
except:
pass
else:
self.ui.checkbox.setChecked(False)
self.ui.combobox.setHidden(1)
Logo = resource_path("adblocker.ico")
self.tray_icon = QSystemTrayIcon(self)
self.icon = QIcon(Logo)
self.tray_icon.setIcon(self.icon)
show_action = QAction("Show", self)
quit_action = QAction("Exit", self)
hide_action = QAction("Hide", self)
show_action.triggered.connect(self.show)
hide_action.triggered.connect(self.hide)
quit_action.triggered.connect(self.beforeQuit)
quit_action.triggered.connect(qApp.quit)
tray_menu = QMenu()
tray_menu.addAction(show_action)
tray_menu.addAction(hide_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
self.ui.lbl_copyright.setOpenExternalLinks(True)
self.ui.checkbox.clicked.connect(self.checkboxControl)
self.ui.combobox.setCurrentIndex(-1)
self.ui.combobox.currentIndexChanged.connect(self.lockCounter_tool_two)
def checkboxControl(self):
if self.ui.checkbox.isChecked() == True:
DB_CHECKBOX_CHANGE("CHECKED")
elif self.ui.checkbox.isChecked() == False:
DB_CHECKBOX_CHANGE("NOT CHECKED")
def closeEvent(self, event):
event.ignore()
self.hide()
def beforeQuit(self):
DB_DISCONNECT()
import ctypes
from pycaw.pycaw import AudioUtilities
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(buff.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
try:
if 'Spotify' in session.Process.name():
volume = session.SimpleAudioVolume
volume.SetMute(0, None)
except AttributeError:
pass
def changingLabels(self):
self.ui.lbl_lasttime.setText(f"{GETTING_LAST_BLOCKED_TIME()}")
self.ui.lbl_counter.setText(f"{GETTING_BLOCK_COUNT()}")
def song(self, tuple):
if str(tuple[1]) != "" and str(tuple[0]) != "":
self.ui.lbl_song.setText(f"{str(tuple[1])} - {str(tuple[0])}")
else:
self.ui.lbl_song.setText("Nothing")
def blockCounter_tool_one(self, int):
if int == 1:
self.ui.combobox.setCurrentIndex(1)
elif int == 0:
self.ui.combobox.setCurrentIndex(0)
def lockCounter_tool_two(self):
if self.ui.combobox.currentText() == "b":
ADDING_BLOCK_COUNT()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = myApp()
win.show()
sys.exit(app.exec_())