-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (145 loc) · 4.64 KB
/
main.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
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
import datetime
import os
import time
from functools import partial
from operator import itemgetter
import requests
from dotenv import load_dotenv
from terminaltables import DoubleTable
LANGUAGES = [
'JavaScript',
'Python',
'Java',
'TypeScript',
'C#',
'PHP',
'C++',
'Shell',
'C',
'Ruby',
]
def get_hh_vacancies(language):
hh_api_url = 'https://api.hh.ru/vacancies'
payload = {
'text': f'Программист {language}',
'area': 1,
'only_with_salary': True,
'period': 30,
}
while True:
response = requests.get(hh_api_url, params=payload)
response.raise_for_status()
page_data = response.json()
page = page_data['page']
pages = page_data['pages']
if page >= pages:
break
payload['page'] = page + 1
yield page_data['items']
def get_sj_vacancies(language, api_key):
sj_api_url = 'https://api.superjob.ru/2.0/vacancies/'
headers = {
'X-Api-App-Id': api_key,
}
search_from = datetime.datetime.now() - datetime.timedelta(days=30)
unix_time = int(time.mktime(search_from.timetuple()))
payload = {
'date_published_from': unix_time,
'keyword': f'Программист {language}',
'town': 4,
'no_agreement': 1,
'page': 0,
}
while True:
response = requests.get(sj_api_url, headers=headers, params=payload)
response.raise_for_status()
page_data = response.json()
page = payload['page']
yield page_data['objects']
if not page_data['more']:
break
payload['page'] = page + 1
def predict_rub_salary(salary_from, salary_to):
if salary_from and salary_to:
return int((salary_from + salary_to) / 2)
elif salary_to:
return int(salary_to * 0.8)
elif salary_from:
return int(salary_from * 1.2)
def get_salary_from_hh(vacancy):
salary = vacancy['salary']
return salary['from'], salary['to'], salary['currency'] == 'RUR'
def get_salary_from_sj(vacancy):
sal_from = vacancy['payment_from']
sal_to = vacancy['payment_to']
return sal_from, sal_to, vacancy['currency'] == 'rub'
def get_found_vacancies(get_vacancies, get_salary, languages):
vacancies_found = {}
for lang in languages:
vacancies_per_lang = {
'vacancies_found': 0,
'vacancies_processed': 0,
'average_salary': 0,
}
average_salaries = []
for vacancies in get_vacancies(lang):
for vacancy in vacancies:
vacancies_per_lang['vacancies_found'] += 1
salary_from, salary_to, currency_in_rub = get_salary(vacancy)
if currency_in_rub:
salary = predict_rub_salary(salary_from, salary_to)
average_salaries.append(salary)
try:
vacancies_per_lang['average_salary'] = int(
sum(average_salaries) / len(average_salaries)
)
vacancies_per_lang['vacancies_processed'] = len(average_salaries)
except ZeroDivisionError:
vacancies_per_lang['average_salary'] = 0
vacancies_per_lang['vacancies_processed'] = 0
vacancies_found[lang] = vacancies_per_lang
return vacancies_found
def format_table(vacancy_statistics, table_name):
data = []
for key, value in vacancy_statistics.items():
data_string = [key]
data_string.extend(list(value.values()))
data.append(data_string)
data = sorted(data, key=itemgetter(3), reverse=True)
data.insert(
0,
[
'Язык программирования',
'Вакансий найдено',
'Вакансий обработано',
'Средняя зарплата',
],
)
table = DoubleTable(data, table_name)
table.justify_columns = {
0: 'left',
1: 'center',
2: 'center',
3: 'right'
}
return table.table
if __name__ == '__main__':
load_dotenv()
sj_api_key = os.getenv('SJ_SECRET_KEY')
get_found_sj_vacancies = partial(get_sj_vacancies, api_key=sj_api_key)
print('Collecting vacancies from HeadHunter...')
hh_vacancies = get_found_vacancies(
get_hh_vacancies,
get_salary_from_hh,
LANGUAGES,
)
print('Done!')
print('Collecting vacancies from SuperJob...')
sj_vacancies = get_found_vacancies(
get_found_sj_vacancies,
get_salary_from_sj,
LANGUAGES,
)
print('Done!')
print(format_table(hh_vacancies, ' HeadHunter Moscow '))
print(format_table(sj_vacancies, ' SuperJob Moscow '))