-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-checker.py
More file actions
executable file
·229 lines (187 loc) · 9.38 KB
/
payment-checker.py
File metadata and controls
executable file
·229 lines (187 loc) · 9.38 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
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
#!/usr/bin/env python3
import calendar
import datetime
import pyotp
from googleapiclient.discovery import build
import oauth2client.client
from google.oauth2 import service_account
import yaml
import time
from common import *
from hetzner import hetzner
from redswitches import redswitches
from vsys import vsys
from scaleway import scaleway
from knownsrv import knownsrv
from prahost import prahost
from ovh import ovh
from worldstream import worldstream
import os
import requests
import textwrap
import click
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
def clear_google_sheet(sa_secrets_file, spreadsheet_id, sheet_name):
credentials = service_account.Credentials.from_service_account_file(sa_secrets_file, scopes=SCOPES)
sheets_service = build('sheets', 'v4', credentials=credentials)
# Clear the sheet starting from the second row
request = sheets_service.spreadsheets().values().clear(spreadsheetId=spreadsheet_id, range=sheet_name + "!A2:Z")
response = request.execute()
print("Cleared the Google Sheet")
def append_google_sheet(sa_secrets_file, spreadsheet_id, sheet_name, row_list):
credentials = service_account.Credentials.from_service_account_file(sa_secrets_file, scopes=SCOPES)
sheets_service = build('sheets', 'v4', credentials=credentials)
# Append the row to the sheet
request = sheets_service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=sheet_name + "!A:Z", valueInputOption="USER_ENTERED", body={"values": [row_list]})
response = request.execute()
print("Appended the row to the Google Sheet")
def remove_screenshots():
import os
# Remove all screenshots from the screenshots directory
# png or html files
for file in os.listdir("screenshots"):
if file.endswith(".png") or file.endswith(".html"):
os.remove("screenshots/" + file)
def alert_telegram(chat_id, token, date, type, login, details, days, status, header, attempts=None):
# Markdown alert text template
attempts_text = ""
if attempts is not None:
attempts_text = f"Attempts Used: {attempts}\n "
text = textwrap.dedent(
"""
{header}
Date Checked: {date}
Account Type: {type}
Login: {login}
Account Details: {details}
Days Remaining: {days}
Payment Status: {status}
{attempts_text}"""
).format(
header=header,
date=date,
type=type,
login=login,
details=details,
days=days,
status=status,
attempts_text=attempts_text
)
# Telegram API URL
url = "https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={text}".format(token=token, chat_id=chat_id, text=text)
requests.get(url)
@click.command()
@click.option("--config", required=True, help="Path to the config.yaml file")
def main(config):
with open(config, "r") as stream:
config = yaml.safe_load(stream)
# Change working directory to the cd from config file
os.chdir(config["cd"])
# Current date in the format of YYYY-MM-DD
current_date = datetime.datetime.now().strftime("%Y-%m-%d")
# Clear the Google Sheet
try:
clear_google_sheet(config["google_sheets"]["sa_secrets_file"], config["google_sheets"]["spreadsheet_id"], config["google_sheets"]["sheet_name"])
except Exception as e:
print("Error:", e)
alert_telegram(config["telegram"]["chat_id"], config["telegram"]["token"], current_date, "---", "---", "---", "---", "---", "ERROR: Google Sheet Clearing Failed")
exit(1)
# Remove all screenshots from the screenshots directory
remove_screenshots()
# Set this to False if any account has a payment issue
no_issues = True
# Iterate over the list of accounts
item_number = 0
for account in config["accounts"]:
item_number += 1
print("Checking account type {type} for the login {login}".format(**account))
# Unset the variables
account_details = "None"
days_remaining = "None"
payment_status = "None"
attempts_made = 0
# Set the proxy
if "proxy" in account:
proxy = account["proxy"]
else:
proxy = None
# If there is no 2FA secret, then set it to None
if "2fa" not in account:
account["2fa"] = None
# Get retry configuration with defaults
max_attempts = config.get("attempts", 1)
delay_between_attempts = config.get("attempt_delay", 0)
# Try multiple attempts
for attempt in range(1, max_attempts + 1):
try:
print(f"Attempt {attempt} of {max_attempts}")
if account["type"] == "Hetzner":
account_details, days_remaining, payment_status = hetzner(account["login"], account["password"], account["2fa"], item_number, proxy)
elif account["type"] == "RedSwitches":
account_details, days_remaining, payment_status = redswitches(account["login"], account["password"], account["2fa"], item_number, proxy)
elif account["type"] == "VSys":
account_details, days_remaining, payment_status = vsys(account["login"], account["password"], account["2fa"], item_number, proxy)
elif account["type"] == "Scaleway":
account_details, days_remaining, payment_status = scaleway(account["login"], account["password"], item_number, proxy)
elif account["type"] == "KnownSRV":
account_details, days_remaining, payment_status = knownsrv(account["login"], account["password"], account["2fa"], item_number, proxy)
elif account["type"] == "PraHost":
account_details, days_remaining, payment_status = prahost(account["login"], account["password"], account["2fa"], item_number, proxy)
elif account["type"] == "OVH":
account_details, days_remaining, payment_status = ovh(account["login"], account["password"], account["consumer_key"], item_number, proxy)
elif account["type"] == "Worldstream":
account_details, days_remaining, payment_status = worldstream(account["login"], item_number, proxy)
# Check if the provider function returned error values
if account_details == "Error" or payment_status == "Error":
print(f"Provider returned error values on attempt {attempt}")
# If this is not the last attempt, wait before retrying
if attempt < max_attempts:
print(f"Waiting {delay_between_attempts} seconds before next attempt...")
time.sleep(delay_between_attempts)
continue
else:
# Last attempt failed with error values
print(f"All {max_attempts} attempts failed")
attempts_made = attempt
else:
# Success - we got valid values
attempts_made = attempt
print(f"Successfully checked account on attempt {attempt}")
break
except Exception as e:
print(f"Exception on attempt {attempt}: {e}")
# If this is not the last attempt, wait before retrying
if attempt < max_attempts:
print(f"Waiting {delay_between_attempts} seconds before next attempt...")
time.sleep(delay_between_attempts)
else:
# Last attempt failed, set error values
print(f"All {max_attempts} attempts failed with exception")
account_details = "Error"
days_remaining = "Error"
payment_status = "Error"
attempts_made = attempt
# Prepare the row to be written to the Google Sheet
row_list = [
current_date,
account["type"],
account["login"],
account_details,
days_remaining,
payment_status
]
# Write the data to the Google Sheet
append_google_sheet(config["google_sheets"]["sa_secrets_file"], config["google_sheets"]["spreadsheet_id"], config["google_sheets"]["sheet_name"], row_list)
# Send an alert to the Telegram if status is not True
if payment_status != True:
alert_telegram(config["telegram"]["chat_id"], config["telegram"]["token"], current_date, account["type"], account["login"], account_details, days_remaining, payment_status, "WARNING: Payment Issue Detected", attempts_made)
# Also set the no_issues to False
no_issues = False
print("---")
# Take a pause between the accounts just in case
time.sleep(5)
# If there are no issues, then send a message to the Telegram
if no_issues:
alert_telegram(config["telegram"]["chat_id"], config["telegram"]["token"], current_date, "---", "---", "---", "---", "All accounts are OK", "INFO: All Accounts OK")
if __name__ == "__main__":
main()