-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhh_parser.py
More file actions
130 lines (103 loc) · 3.66 KB
/
Copy pathhh_parser.py
File metadata and controls
130 lines (103 loc) · 3.66 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
import os
import sqlite3
import sys
from time import sleep
import requests
from environs import Env
from telegram import Bot
from tqdm import tqdm
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))
# import settings
# from database import db_processing
HH_API_URL = 'https://api.hh.ru/vacancies'
UPDATE_DELAY = 60*10
def clean_text(text: str) -> str:
trash = [
'<highlighttext>',
'</highlighttext>',
]
for item in trash:
text = ''.join(text.split(item))
return text
def convert_salary(salary: dict) -> str:
salary_from = f'от {salary["from"]} ' if salary["from"] else ''
salary_to = f'до {salary["to"]} ' if salary["to"] else ''
return f'{salary_from}{salary_to}({salary["currency"]})'
def run_hh_parser(hh_headers: dict,
db: sqlite3.Connection,
cursor: sqlite3.Cursor,
bot: Bot) -> None:
response = requests.get(
HH_API_URL,
headers=hh_headers,
params=settings.PYTHON_DEV_HH_PARAMS
)
response.raise_for_status()
vacancies = response.json()['items']
for vacancy in tqdm(vacancies, desc='processing vacancies'):
print(vacancy['name'])
if vacancy['type']['id'] != 'open':
print('47 CONTINUE')
continue
address = vacancy['address']
if address:
address = f"{address['city']}, {address['street']}, {address['building']}"
salary = vacancy['salary']
if salary:
salary = convert_salary(salary)
requirements = vacancy['snippet']['requirement']
if requirements:
requirements = clean_text(requirements)
responsibilities = vacancy['snippet']['responsibility']
if responsibilities:
responsibilities = clean_text(responsibilities)
parsed_vacancy = {
'id': vacancy['id'],
'title': vacancy['name'],
'employer': vacancy['employer']['name'],
'employer_url': vacancy['employer']['url'],
'salary': salary,
'address': address,
'requirements': requirements,
'responsibilities': responsibilities,
'vacancy_url': vacancy['alternate_url'],
'response_url': vacancy['apply_alternate_url'],
'created_at': vacancy['created_at'],
'responded': False,
'processed': False
}
print('PARSED')
check_vacancy = cursor.execute(
"SELECT id FROM vacancies WHERE id = ?",
(parsed_vacancy['id'],)
).fetchone()
print('CHECKED')
if not check_vacancy:
print('checked')
vacancy_object = db_processing.Vacancy(list(parsed_vacancy.values()))
print('created')
for telegram_id in env.list('USERS'):
print(telegram_id)
bot.send_message(
telegram_id,
text=vacancy_object.message(),
reply_markup=vacancy_object.make_tg_inline_keyboard()
)
print('sent')
db_processing.add_vacancy(
db,
cursor,
list(parsed_vacancy.values()),
)
print('inputed')
if __name__ == '__main__':
env = Env()
env.read_env()
db = sqlite3.connect(os.path.join(settings.DB_ROOT_FOLDER, env.str('DATABASE', 'vacancies.sqlite3')))
cursor = db.cursor()
bot = Bot(env.str("TELEGRAM_BOT_TOKEN"))
hh_headers = {'User-Agent': env.str('USER_AGENT')}
while True:
run_hh_parser(hh_headers, db, cursor, bot)
sleep(UPDATE_DELAY)