-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.py
More file actions
50 lines (40 loc) · 1.55 KB
/
SQL.py
File metadata and controls
50 lines (40 loc) · 1.55 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
import psycopg2
import json
import schedule
import time
from config import host, user, password, db_name
def load_vacancies():
connection = None
try:
connection = psycopg2.connect(
host=host,
user=user,
password=password,
database=db_name,
port=5432
)
connection.autocommit = True
with connection.cursor() as cursor:
# Создаем таблицу (если она еще не существует)
print("[INFO] Table 'vc' created or already exists.")
# Читаем данные из JSON-файла
with open("vacancies.json", "r") as f:
vacancies = json.load(f)
# Вставляем данные в таблицу
for vacancy in vacancies:
cursor.execute(
"""INSERT INTO vc (Title, Company, Experience, URL) VALUES (%s, %s, %s, %s)""",
(vacancy["title"], vacancy["company"], vacancy["experience"], vacancy["url"])
)
print("[INFO] Data inserted into table 'vc'.")
except Exception as _ex:
print("[INFO] Error while working with PostgreSQL", _ex)
finally:
if connection:
connection.close()
print("[INFO] PostgreSQL connection closed")
# Запланируем выполнение функции load_vacancies() каждый день в 00:00
schedule.every().day.at("00:00").do(load_vacancies)
while True:
schedule.run_pending()
time.sleep(1)