-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
97 lines (80 loc) · 2.86 KB
/
db.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
from mysql.connector import connect, Error
from datetime import datetime
import requests
from settings import DB_AUTH
class ErrorGetCountryCode(Exception):
pass
def current_yyyy_mm_dd():
return f"{datetime.now().year}-{datetime.now().month}-{datetime.now().day}"
def get_country_code(ip: str, attempt: int = 0):
if attempt > 3:
raise ErrorGetCountryCode
try:
response = requests.get(f"http://ip-api.com/json/{ip}?lang=ru")
ip_data = response.json()
return ip_data["countryCode"]
except KeyError:
return get_country_code(ip=ip, attempt=attempt+1)
def create_proxy_counter_row(ip: str):
try:
geo = get_country_code(ip=ip)
with connect(
host=DB_AUTH["host"],
user=DB_AUTH["user"],
password=DB_AUTH["password"]
) as connection:
with connection.cursor() as cursor:
cursor.execute("USE proxy_shop;")
cursor.execute(f"INSERT INTO proxy_counter VALUES "
f"('{ip}',"
f" '{geo}',"
f" 0"
");")
connection.commit()
return True
except Error as e:
print(e)
return False
def send_proxy(ip: str, port: str, login: str, password: str, status: str = "yes"):
try:
with connect(
host=DB_AUTH["host"],
user=DB_AUTH["user"],
password=DB_AUTH["password"]
) as connection:
with connection.cursor() as cursor:
cursor.execute("USE proxy_shop;")
cursor.execute(f"INSERT INTO proxy VALUES "
f"('{ip}',"
f" '{port}',"
f" '{login}',"
f" '{password}',"
f" '{status}'"
");")
connection.commit()
return True
except Error as e:
print(e)
return False
def create_expiration_date_row(ip: str, day_to_die: int = 30, current_date: str = current_yyyy_mm_dd()):
"""
current_date: str = "yyyy-mm-dd"
"""
try:
with connect(
host=DB_AUTH["host"],
user=DB_AUTH["user"],
password=DB_AUTH["password"]
) as connection:
with connection.cursor() as cursor:
cursor.execute("USE proxy_shop;")
cursor.execute(f"INSERT INTO expiration_date VALUES "
f"('{ip}',"
f" '{current_date}',"
f" {day_to_die}"
");")
connection.commit()
return True
except Error as e:
print(e)
return False