-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathcore.py
More file actions
178 lines (150 loc) · 5.41 KB
/
core.py
File metadata and controls
178 lines (150 loc) · 5.41 KB
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
import os
import pathlib
import time
from platform import system
from urllib.parse import quote
from webbrowser import open
import pyperclip
import platform
import requests
from pyautogui import click, hotkey, locateOnScreen, moveTo, press, size, typewrite
from pywhatkit.core.exceptions import InternetException
WIDTH, HEIGHT = size()
def check_number(number: str) -> bool:
"""Checks the Number to see if contains the Country Code"""
return "+" in number or "_" in number
def close_tab(wait_time: int = 2) -> None:
"""Closes the Currently Opened Browser Tab"""
time.sleep(wait_time)
_system = system().lower()
if _system in ("windows", "linux"):
hotkey("ctrl", "w")
elif _system == "darwin":
hotkey("command", "w")
else:
raise Warning(f"{_system} not supported!")
press("enter")
def findtextbox() -> None:
"""click on text box"""
dir_path = os.path.dirname(os.path.realpath(__file__))
location = locateOnScreen(f"{dir_path}\\data\\pywhatkit_smile1.png")
try:
moveTo(location[0] + 150, location[1] + 5)
click()
except Exception:
location = locateOnScreen(f"{dir_path}\\data\\pywhatkit_smile.png")
moveTo(location[0] + 150, location[1] + 5)
click()
def find_link():
dir_path = os.path.dirname(os.path.realpath(__file__))
location = locateOnScreen(f"{dir_path}\\data\\link.png")
print(location)
try:
moveTo(location[0] + location[2]/2, location[1] + location[3]/2)
click()
except Exception:
location = locateOnScreen(f"{dir_path}\\data\\link2.png")
moveTo(location[0] + location[2]/2, location[1] + location[3]/2)
print(location)
click()
def find_document():
dir_path = os.path.dirname(os.path.realpath(__file__))
location = locateOnScreen(f"{dir_path}\\data\\document.png")
print(location)
moveTo(location[0] + location[2]/2, location[1] + location[3]/2)
click()
def find_photo_or_video():
dir_path = os.path.dirname(os.path.realpath(__file__))
location = locateOnScreen(f"{dir_path}\\data\\photo_or_video.png")
print(location)
moveTo(location[0] + location[2]/2, location[1] + location[3]/2)
click()
def check_connection() -> None:
"""Check the Internet connection of the Host Machine"""
try:
requests.get("https://google.com")
except requests.RequestException:
raise InternetException(
"Error while connecting to the Internet. Make sure you are connected to the Internet!"
)
def _web(receiver: str, message: str) -> None:
"""Opens WhatsApp Web based on the Receiver"""
if check_number(number=receiver):
open(
"https://web.whatsapp.com/send?phone="
+ receiver
+ "&text="
+ quote(message)
)
else:
open("https://web.whatsapp.com/accept?code=" + receiver)
def send_message(message: str, receiver: str, wait_time: int) -> None:
"""Parses and Sends the Message"""
_web(receiver=receiver, message=message)
time.sleep(wait_time)
if not check_number(number=receiver):
pyperclip.copy(message)
if platform.system() == "Darwin":
hotkey("command", "v")
else:
hotkey("ctrl", "v")
time.sleep(1)
press("enter")
def copy_image(path: str) -> None:
"""Copy the Image to Clipboard based on the Platform"""
_system = system().lower()
if _system == "linux":
if pathlib.Path(path).suffix in (".PNG", ".png"):
os.system(f"copyq copy image/png - < {path}")
elif pathlib.Path(path).suffix in (".jpg", ".JPG", ".jpeg", ".JPEG"):
os.system(f"copyq copy image/jpeg - < {path}")
else:
raise Exception(
f"File Format {pathlib.Path(path).suffix} is not Supported!"
)
elif _system == "windows":
from io import BytesIO
import win32clipboard # pip install pywin32
from PIL import Image
image = Image.open(path)
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
elif _system == "darwin":
if pathlib.Path(path).suffix in (".jpg", ".jpeg", ".JPG", ".JPEG"):
os.system(
f"osascript -e 'set the clipboard to (read (POSIX file \"{path}\") as JPEG picture)'"
)
else:
raise Exception(
f"File Format {pathlib.Path(path).suffix} is not Supported!"
)
else:
raise Exception(f"Unsupported System: {_system}")
def send_image(path: str, caption: str, receiver: str, wait_time: int) -> None:
"""Sends the Image to a Contact or a Group based on the Receiver"""
_web(message=caption, receiver=receiver)
time.sleep(7)
click(WIDTH / 2, HEIGHT / 2 + 15)
time.sleep(wait_time - 7)
copy_image(path=path)
if not check_number(number=receiver):
for char in caption:
if char == "\n":
hotkey("shift", "enter")
else:
typewrite(char)
else:
typewrite(" ")
if system().lower() == "darwin":
hotkey("command", "v")
else:
hotkey("ctrl", "v")
time.sleep(1)
findtextbox()
press("enter")