Upload 4 files
Browse files- Google.py +116 -0
- app.py +82 -0
- client_secret.json +1 -0
- requirements.txt +2 -0
Google.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import os
|
3 |
+
import datetime
|
4 |
+
from collections import namedtuple
|
5 |
+
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
|
6 |
+
from googleapiclient.discovery import build
|
7 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
8 |
+
from google.auth.transport.requests import Request
|
9 |
+
|
10 |
+
|
11 |
+
def Create_Service(client_secret_file, api_name, api_version, *scopes, prefix=''):
|
12 |
+
CLIENT_SECRET_FILE = client_secret_file
|
13 |
+
API_SERVICE_NAME = api_name
|
14 |
+
API_VERSION = api_version
|
15 |
+
SCOPES = [scope for scope in scopes[0]]
|
16 |
+
|
17 |
+
cred = None
|
18 |
+
working_dir = os.getcwd()
|
19 |
+
token_dir = 'token files'
|
20 |
+
pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}{prefix}.pickle'
|
21 |
+
|
22 |
+
### Check if token dir exists first, if not, create the folder
|
23 |
+
if not os.path.exists(os.path.join(working_dir, token_dir)):
|
24 |
+
os.mkdir(os.path.join(working_dir, token_dir))
|
25 |
+
|
26 |
+
if os.path.exists(os.path.join(working_dir, token_dir, pickle_file)):
|
27 |
+
with open(os.path.join(working_dir, token_dir, pickle_file), 'rb') as token:
|
28 |
+
cred = pickle.load(token)
|
29 |
+
|
30 |
+
if not cred or not cred.valid:
|
31 |
+
if cred and cred.expired and cred.refresh_token:
|
32 |
+
cred.refresh(Request())
|
33 |
+
else:
|
34 |
+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
|
35 |
+
cred = flow.run_local_server()
|
36 |
+
|
37 |
+
with open(os.path.join(working_dir, token_dir, pickle_file), 'wb') as token:
|
38 |
+
pickle.dump(cred, token)
|
39 |
+
|
40 |
+
try:
|
41 |
+
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
|
42 |
+
print(API_SERVICE_NAME, API_VERSION, 'service created successfully')
|
43 |
+
return service
|
44 |
+
except Exception as e:
|
45 |
+
print(e)
|
46 |
+
print(f'Failed to create service instance for {API_SERVICE_NAME}')
|
47 |
+
os.remove(os.path.join(working_dir, token_dir, pickle_file))
|
48 |
+
return None
|
49 |
+
|
50 |
+
|
51 |
+
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
|
52 |
+
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
|
53 |
+
return dt
|
54 |
+
|
55 |
+
|
56 |
+
class GoogleSheetsHelper:
|
57 |
+
# --> spreadsheets().batchUpdate()
|
58 |
+
Paste_Type = namedtuple('_Paste_Type',
|
59 |
+
('normal', 'value', 'format', 'without_borders',
|
60 |
+
'formula', 'date_validation', 'conditional_formatting')
|
61 |
+
)('PASTE_NORMAL', 'PASTE_VALUES', 'PASTE_FORMAT', 'PASTE_NO_BORDERS',
|
62 |
+
'PASTE_FORMULA', 'PASTE_DATA_VALIDATION', 'PASTE_CONDITIONAL_FORMATTING')
|
63 |
+
|
64 |
+
Paste_Orientation = namedtuple('_Paste_Orientation', ('normal', 'transpose'))('NORMAL', 'TRANSPOSE')
|
65 |
+
|
66 |
+
Merge_Type = namedtuple('_Merge_Type', ('merge_all', 'merge_columns', 'merge_rows')
|
67 |
+
)('MERGE_ALL', 'MERGE_COLUMNS', 'MERGE_ROWS')
|
68 |
+
|
69 |
+
Delimiter_Type = namedtuple('_Delimiter_Type', ('comma', 'semicolon', 'period', 'space', 'custom', 'auto_detect')
|
70 |
+
)('COMMA', 'SEMICOLON', 'PERIOD', 'SPACE', 'CUSTOM', 'AUTODETECT')
|
71 |
+
|
72 |
+
# --> Types
|
73 |
+
Dimension = namedtuple('_Dimension', ('rows', 'columns'))('ROWS', 'COLUMNS')
|
74 |
+
|
75 |
+
Value_Input_Option = namedtuple('_Value_Input_Option', ('raw', 'user_entered'))('RAW', 'USER_ENTERED')
|
76 |
+
|
77 |
+
Value_Render_Option = namedtuple('_Value_Render_Option', ["formatted", "unformatted", "formula"]
|
78 |
+
)("FORMATTED_VALUE", "UNFORMATTED_VALUE", "FORMULA")
|
79 |
+
|
80 |
+
@staticmethod
|
81 |
+
def define_cell_range(
|
82 |
+
sheet_id,
|
83 |
+
start_row_number=1, end_row_number=0,
|
84 |
+
start_column_number=None, end_column_number=0):
|
85 |
+
"""GridRange object"""
|
86 |
+
json_body = {
|
87 |
+
'sheetId': sheet_id,
|
88 |
+
'startRowIndex': start_row_number - 1,
|
89 |
+
'endRowIndex': end_row_number,
|
90 |
+
'startColumnIndex': start_column_number - 1,
|
91 |
+
'endColumnIndex': end_column_number
|
92 |
+
}
|
93 |
+
return json_body
|
94 |
+
|
95 |
+
@staticmethod
|
96 |
+
def define_dimension_range(sheet_id, dimension, start_index, end_index):
|
97 |
+
json_body = {
|
98 |
+
'sheetId': sheet_id,
|
99 |
+
'dimension': dimension,
|
100 |
+
'startIndex': start_index,
|
101 |
+
'endIndex': end_index
|
102 |
+
}
|
103 |
+
return json_body
|
104 |
+
|
105 |
+
|
106 |
+
class GoogleCalendarHelper:
|
107 |
+
...
|
108 |
+
|
109 |
+
|
110 |
+
class GoogleDriverHelper:
|
111 |
+
...
|
112 |
+
|
113 |
+
|
114 |
+
if __name__ == '__main__':
|
115 |
+
g = GoogleSheetsHelper()
|
116 |
+
print(g.Delimiter_Type)
|
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import smtplib
|
3 |
+
from email.mime.text import MIMEText
|
4 |
+
from email.mime.multipart import MIMEMultipart
|
5 |
+
import time
|
6 |
+
from google.oauth2 import service_account
|
7 |
+
from googleapiclient.discovery import build
|
8 |
+
import base64
|
9 |
+
from Google import Create_Service
|
10 |
+
url ="https://cloud.vast.ai/api/v0/bundles/?q=%7B%22disk_space%22%3A%7B%22gte%22%3A16.336194011315104%7D%2C%22show_incompatible%22%3A%7B%22eq%22%3Atrue%7D%2C%22reliability2%22%3A%7B%22gte%22%3A0.8846748789619375%7D%2C%22duration%22%3A%7B%22gte%22%3A8862.880832140607%7D%2C%22verified%22%3A%7B%22eq%22%3Atrue%7D%2C%22rentable%22%3A%7B%22eq%22%3Atrue%7D%2C%22dph_total%22%3A%7B%22lte%22%3A20.000000000000004%7D%2C%22num_gpus%22%3A%7B%22gte%22%3A1%2C%22lte%22%3A1%7D%2C%22gpu_ram%22%3A%7B%22gte%22%3A70239.74564077858%2C%22lte%22%3A205674.01375904563%7D%2C%22sort_option%22%3A%7B%220%22%3A%5B%22dph_total%22%2C%22asc%22%5D%2C%221%22%3A%5B%22total_flops%22%2C%22asc%22%5D%7D%2C%22gpu_name%22%3A%7B%22in%22%3A%5B%22Pro%20V620%22%2C%22RX%206900%20XT%22%2C%22RX%206800%20XT%22%2C%22RX%206800%22%2C%22RX%207900%20XTX%22%2C%22RX%207900%20GRE%22%2C%22RX%207900%20XT%22%2C%22RX%207900%20XTX%22%2C%22RX%207700%20XT%22%2C%22RX%207600%22%2C%22Pro%20W7900%22%2C%22Pro%20W7800%22%2C%22Radeon%20VII%22%2C%22Radeon%20Pro%20VII%22%2C%22InstinctMI250X%22%2C%22InstinctMI210%22%2C%22InstinctMI100%22%2C%22GH200%20SXM%22%2C%22H100%20PCIE%22%2C%22H100%20SXM%22%2C%22H100%20NVL%22%2C%22A100%20PCIE%22%2C%22A800%20PCIE%22%2C%22A100%20SXM4%22%2C%22A100X%22%2C%22A100%20SXM%22%2C%22RTX%204090%22%2C%22RTX%204090D%22%2C%22RTX%204080S%22%2C%22RTX%204080%22%2C%22RTX%204070S%20Ti%22%2C%22RTX%204070S%22%2C%22RTX%204070%20Ti%22%2C%22RTX%204070%22%2C%22RTX%204060%20Ti%22%2C%22RTX%204060%22%2C%22RTX%203090%20Ti%22%2C%22RTX%203090%22%2C%22RTX%203080%20Ti%22%2C%22RTX%203080%22%2C%22RTX%203070%20Ti%22%2C%22RTX%203070%22%2C%22RTX%203070%20laptop%22%2C%22RTX%203060%20Ti%22%2C%22RTX%203060%22%2C%22RTX%203060%20laptop%22%2C%22RTX%203050%22%2C%22RTX%202080%20Ti%22%2C%22RTX%202080S%22%2C%22RTX%202080%22%2C%22RTX%202070S%22%2C%22RTX%202070%22%2C%22RTX%202060S%22%2C%22RTX%202060%22%2C%22RTX%206000Ada%22%2C%22RTX%205000Ada%22%2C%22RTX%204500Ada%22%2C%22RTX%204000Ada%22%2C%22RTX%20A6000%22%2C%22RTX%20A5000%22%2C%22RTX%20A4500%22%2C%22RTX%20A4000%22%2C%22RTX%20A2000%22%2C%22Titan%20V%22%2C%22Titan%20X%22%2C%22Titan%20Xp%22%2C%22Titan%20RTX%22%2C%22A40%22%2C%22A30%22%2C%22A16%22%2C%22A10g%22%2C%22A10%22%2C%22L40S%22%2C%22L40%22%2C%22L4%22%2C%22Tesla%20K80%22%2C%22Tesla%20P100%22%2C%22Tesla%20P40%22%2C%22Tesla%20P4%22%2C%22Tesla%20V100%22%2C%22GTX%201660%20Ti%22%2C%22GTX%201660%20S%22%2C%22GTX%201660%22%2C%22GTX%201650%20S%22%2C%22GTX%201650%22%2C%22GTX%201080%20Ti%22%2C%22GTX%201080%22%2C%22GTX%201070%20Ti%22%2C%22GTX%201070%22%2C%22GTX%201060%206GB%22%2C%22GTX%201050%20Ti%22%2C%22GTX%201050%22%2C%22GTX%20980%20Ti%22%2C%22GTX%20980%22%2C%22GTX%20970%22%2C%22GTX%20960%22%2C%22GTX%20750%20Ti%22%2C%22GTX%20750%22%2C%22Q%20RTX%208000%22%2C%22Q%20RTX%206000%22%2C%22Q%20RTX%205000%22%2C%22Q%20RTX%204000%22%2C%22P104-100%22%2C%22P106-100%22%2C%22GP100%22%2C%22Quadro%20P6000%22%2C%22Quadro%20P5000%22%2C%22Quadro%20P4000%22%2C%22Quadro%20P2000%22%5D%7D%2C%22order%22%3A%5B%5B%22dph_total%22%2C%22asc%22%5D%2C%5B%22total_flops%22%2C%22asc%22%5D%5D%2C%22allocated_storage%22%3A16.336194011315104%2C%22limit%22%3A64%2C%22extra_ids%22%3A%5B%5D%2C%22type%22%3A%22ask%22%7D"
|
11 |
+
|
12 |
+
def send_email(subject, body):
|
13 |
+
sender_email = "[email protected]"
|
14 |
+
receiver_email = "[email protected]"
|
15 |
+
password = "hycw jsiw klia djbs"
|
16 |
+
|
17 |
+
# Thiết lập nội dung email
|
18 |
+
msg = MIMEMultipart()
|
19 |
+
msg['From'] = sender_email
|
20 |
+
msg['To'] = receiver_email
|
21 |
+
msg['Subject'] = subject
|
22 |
+
|
23 |
+
# Thêm nội dung vào email
|
24 |
+
msg.attach(MIMEText(body, 'plain'))
|
25 |
+
|
26 |
+
# Gửi email qua SMTP server
|
27 |
+
try:
|
28 |
+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
|
29 |
+
server.login(sender_email, password)
|
30 |
+
server.sendmail(sender_email, receiver_email, msg.as_string())
|
31 |
+
print("Email sent successfully.")
|
32 |
+
except Exception as e:
|
33 |
+
print(f"Failed to send email: {e}")
|
34 |
+
|
35 |
+
|
36 |
+
def send_email_via_gmail_api(subject, body, receiver_email):
|
37 |
+
CLIENT_SECRET_FILE = 'client_secret.json'
|
38 |
+
API_NAME = 'gmail'
|
39 |
+
API_VERSION = 'v1'
|
40 |
+
SCOPES = ['https://mail.google.com/']
|
41 |
+
|
42 |
+
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
|
43 |
+
|
44 |
+
emailMsg = body
|
45 |
+
mimeMessage = MIMEMultipart()
|
46 |
+
mimeMessage['to'] = receiver_email
|
47 |
+
mimeMessage['subject'] = subject
|
48 |
+
mimeMessage.attach(MIMEText(emailMsg, 'plain'))
|
49 |
+
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
|
50 |
+
|
51 |
+
message = service.users().messages().send(userId='me', body={'raw': raw_string}).execute()
|
52 |
+
print(message)
|
53 |
+
def check_url_for_value(url, target_value):
|
54 |
+
try:
|
55 |
+
response = requests.get(url)
|
56 |
+
response.raise_for_status()
|
57 |
+
data = response.json() # Giả sử dữ liệu nhận được là JSON
|
58 |
+
|
59 |
+
machine = [ i["machine_id"]for i in data['offers']]
|
60 |
+
print(machine)
|
61 |
+
# Kiểm tra nếu giá trị mong muốn có trong dữ liệu
|
62 |
+
for i in range(len(target_value)):
|
63 |
+
for j in range(len(machine)):
|
64 |
+
if machine[j] == target_value[i]:
|
65 |
+
send_email_via_gmail_api("Có Máy VAST AI", f"Nhanh lẹ lên vô chiếm máy {target_value[i]}!!!","[email protected]")
|
66 |
+
break
|
67 |
+
except Exception as e:
|
68 |
+
print(f"Failed to fetch data from URL: {e}")
|
69 |
+
|
70 |
+
|
71 |
+
def run_monitoring():
|
72 |
+
|
73 |
+
target_value = [27828,11611,28244]
|
74 |
+
|
75 |
+
while True:
|
76 |
+
check_url_for_value(url, target_value)
|
77 |
+
time.sleep(120) # Chờ 1 phút
|
78 |
+
# Press the green button in the gutter to run the script.
|
79 |
+
if __name__ == '__main__':
|
80 |
+
run_monitoring()
|
81 |
+
|
82 |
+
|
client_secret.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"installed":{"client_id":"529655390517-hetu3lc90fhqofavkdvt6v49fn9spm4i.apps.googleusercontent.com","project_id":"ghostfacenets-429308","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
google_auth_oauthlib
|