-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
330 lines (294 loc) · 8.27 KB
/
utilities.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Fecha: 10/November/2021 - Wednesday
# Autor: Virgilio Murillo Ochoa
# personal github: Virgilio-AI
# linkedin: https://www.linkedin.com/in/virgilio-murillo-ochoa-b29b59203
# contact: [email protected]
# python imports
import subprocess
import pyautogui as pg
import pandas as pd
from datetime import datetime as dt
import os
import time as t
import traceback
# commented so that the app works without the internet
import pyttsx3 as tts
# from gtts import gTTS
import presets
from selenium import webdriver
from bs4 import BeautifulSoup
from pytube import YouTube
from pytube import Channel
import requests
class Utilities():
def waitUntilFound(s,timeLimit,img):
counter=0
while 1:
counter+=0.2
if counter >= timeLimit:
print("not found")
return -1
try:
x,y=pg.locateCenterOnScreen(img,confidence=0.7)
print("FOUND")
print(x,y)
break
except Exception as e:
print(e)
t.sleep(0.2)
print("not founding")
pass
def clickUntilFound(s,timeLimit,img):
counter=0
while 1:
counter+=0.2
if counter == timeLimit:
print("not found")
return -1
try:
x,y=pg.locateCenterOnScreen(img,confidence=0.7)
pg.moveTo(x,y,0.1)
pg.click()
print(x,y)
break
except Exception as e:
print(e)
t.sleep(0.2)
pass
def moveWhenFound(s,timeLimit,img):
counter=0
while 1:
counter+=0.2
if counter == timeLimit:
print("not found")
return -1
try:
x,y=pg.locateCenterOnScreen(img,confidence=0.7)
pg.moveTo(x,y,0.1)
print(x,y)
break
except Exception as e:
print(e)
t.sleep(0.2)
pass
def moveClickedElement(s,timeLimit,img):
counter=0
while 1:
counter+=0.2
if counter == timeLimit:
print("not found")
return -1
try:
x,y = pg.locateCenterOnScreen(img,confidence=0.9)
pg.moveTo(x,y,0.1)
pg.mouseDown()
pg.drag(100,0,duration=0.5,button='left')
print(x,y)
pg.mouseUp()
break
except Exception as e:
print(e)
t.sleep(0.2)
pass
def moveToMonitorOnLeft(s):
pg.keyDown('win')
pg.keyDown('shift')
pg.keyDown('<')
pg.keyUp('<')
pg.keyUp('win')
pg.keyUp('shift')
def moveToMonitorOnRight(s):
pg.keyDown('win')
pg.keyDown('shift')
pg.keyDown('>')
pg.keyUp('>')
pg.keyUp('win')
pg.keyUp('shift')
def deleteSoundDirectory(s):
directory = 'sounds'
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
os.remove(f)
def forceCreateDirectory(s,parentDirectory): # ex: name/name2
subprocess.check_call(['mkdir', '-p', ''+parentDirectory+''])
def giveWarningNoVolChange(s,time,message):
os.system("pactl set-sink-mute @DEFAULT_SINK@ false ")
counter = 0
language = 'en'
nameOfFile = "sounds/" + message.replace(" ","_") + ".mp3"
eng = tts.init()
# for the rate
eng.setProperty('rate',125)
volume = eng.getProperty('volume') #getting to know current volume level (min=0 and max=1)
eng.setProperty('volume',1.5)
# voices = eng.getProperty('voices') #getting details of current voice
eng.setProperty('voice', 'english_rp+f3')
while counter < time:
os.system("notify-send \""+message+"\"")
eng.say(message)
eng.runAndWait()
t.sleep(1)
counter+=1
def giveWarning(s,time,message):
volumePercentage = subprocess.getoutput('pamixer --get-volume')
t.sleep(0.01)
os.system("pactl set-sink-mute @DEFAULT_SINK@ false ; pactl -- set-sink-volume 0 "+str(presets.alertVolume)+"%")
counter = 0
language = 'en'
nameOfFile = "sounds/" + message.replace(" ","_") + ".mp3"
eng = tts.init()
# for the rate
eng.setProperty('rate',125)
volume = eng.getProperty('volume') #getting to know current volume level (min=0 and max=1)
eng.setProperty('volume',1.5)
# voices = eng.getProperty('voices') #getting details of current voice
eng.setProperty('voice', 'english_rp+f3')
while counter < time:
os.system("notify-send \""+message+"\"")
eng.say(message)
eng.runAndWait()
t.sleep(1)
counter+=1
os.system("pactl -- set-sink-volume 0 "+volumePercentage+"%") # set the volume to 80%
t.sleep(0.01)
def closeWindow(s):
t.sleep(0.01)
pg.keyDown('win')
t.sleep(0.01)
pg.keyDown('shift')
t.sleep(0.01)
pg.keyDown('c')
t.sleep(0.01)
pg.keyUp('c')
t.sleep(0.01)
pg.keyUp('win')
t.sleep(0.01)
pg.keyUp('shift')
t.sleep(0.01)
# =============
# ==== get the time =====
# ============
def getDayOfTheWeek(s):
val = int(str(int(dt.today().strftime('%w'))))
if val == 0:
return 7
else:
return val
def getMonth(s):
return int(str(int(dt.today().strftime('%m'))))
def getHour(s):
return (int(dt.today().strftime('%H')),int(dt.today().strftime('%M')))
def getDayOfTheMonth(s):
return int(str(int(dt.today().strftime('%d'))))
def getYear(s):
return int(str(int(dt.today().strftime('%Y'))))
def getYearDay(s):
return int(str(int(dt.today().strftime('%j'))))
# %%
# to get an hour from a string that is in the csv
def getStringHour(s,strin):
return strin.split(':')[1],strin.split(':')[2]
# =============
# ==== check if avaliable =====
# =============
def checkIfActive(s):
dayOfTheWeek = s.getDayOfTheWeek()
if dayOfTheWeek == 1:
return presets.mondayPresets
if dayOfTheWeek == 2:
return presets.tuesdayPresets
if dayOfTheWeek == 3:
return presets.wednesdayPresets
if dayOfTheWeek == 4:
return presets.thursdayPresets
if dayOfTheWeek == 5:
return presets.fridayPresets
if dayOfTheWeek == 6:
return presets.saturdayPresets
if dayOfTheWeek == 7:
return presets.sundayPresets
# =============
# ==== utilities for interating with maria db =====
# =============
def mardb(s,stri):
return "mariadb --execute=\" use automation_suite ; "+str(stri)+"\""
def mardbs(s,stri):
return "mariadb --execute=' use automation_suite ; "+str(stri)+"'"
# =============
# ==== Internet connection =====
# =============
def InternetConnection(s):
url = "https://www.google.com"
timeout = 5
try:
request = requests.get(url, timeout=timeout)
return True
except (requests.ConnectionError, requests.Timeout) as exception:
return False
# =============
# ==== connecting to a web scrapper =====
# =============
def FullYoutubeVolume(s):
VolueIcon = "img_utilities/VolumeIcon.png"
VolumeDot = "img_utilities/VolumeDot.png"
s.moveWhenFound(20,VolueIcon)
s.moveClickedElement(20,VolumeDot)
def playYoutubeVideos(s,urls):
os.system("pactl -- set-sink-volume 0 80%") # set the volume to 80%
videos = urls
maxLen = 1800
sumLen=0
for video in videos:
channel_info = Channel(video[0])
channelMinTime= video[1]
channelMaxTime= video[2]
cont = 0
now = dt.now()
for url in channel_info.url_generator():
video_details = YouTube(url)
videoLen = video_details.length/60
vidTime=video_details.publish_date
publish_date = str(video_details.publish_date).split(' ')[0]
hoursSincePublished = (now-vidTime).total_seconds()/3600
print("url: " + str(video) )
print("channel min time: " + str(channelMinTime))
print("video len: " + str(videoLen))
print("hours since published: " + str(hoursSincePublished))
print("now: " + str(now) )
print("vidTime: " + str(vidTime) )
# print("vidInfo: " + str(video_details.vid_info) )
if hoursSincePublished < 40.0 and channelMinTime < videoLen and channelMaxTime > videoLen:
print("=====")
print(f'Video URL: {url}')
print(f'Video Title: {video_details.title}')
print(f'the video length is: {videoLen}')
print((now-vidTime).total_seconds()/3600)
print(f'publish date: {publish_date}')
print("=====")
print(video_details.length)
os.system('st -e sh -c "brave ' + url + '" &')
yutubeImage='img_utilities/youtubeImage.png'
s.waitUntilFound(20,yutubeImage)
print("=====")
print(video_details.length)
# for setting full volume
s.FullYoutubeVolume()
t.sleep(video_details.length)
sumLen+=video_details.length
print("=====")
print(video_details.length)
pg.keyDown('ctrl')
pg.press('w')
pg.keyUp('ctrl')
if maxLen < sumLen:
break
elif hoursSincePublished > 31:
break
cont+=1
if cont > 5:
break
def schedulePrint(s):
os.system('lp horariosDiarios/today.txt')
# playYoutubeVideos(presets.youtubeUrls_minTime)