-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
194 lines (151 loc) · 6.68 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
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
import requests
from dotenv import load_dotenv
import os
from terminaltables import AsciiTable
def get_ansi_table(top_languages, title):
top_languages_table = [['Язык программирования', 'Вакансий найдено', 'Вакансий обработано', 'Средняя зарплата']]
for programming_language, statistic_values in top_languages.items():
top_languages_table.append([programming_language,
statistic_values["vacancies_found"],
statistic_values["vacancies_processed"],
statistic_values["average_salary"]]
)
table_instance = AsciiTable(top_languages_table, title)
table_instance.justify_columns[1] = 'right'
table_instance.justify_columns[2] = 'right'
table_instance.justify_columns[3] = 'right'
return table_instance.table
def get_average_salary(salary_from, salary_to, salary_currency):
if salary_currency != 'RUR' or (not salary_from and not salary_to):
return None
if salary_from and not salary_to:
return salary_from * 1.2
if not salary_from and salary_to:
return salary_to * 0.8
return (salary_from+salary_to)/2
def predict_rub_salary_hh(vacancie):
if str(vacancie["salary"]): return None
salary_from = vacancie["salary"]["from"]
salary_to = vacancie["salary"]["to"]
salary_currency = vacancie["salary"]["currency"]
return get_average_salary(salary_from, salary_to, salary_currency)
def predict_rub_salary_sj(vacancie):
salary_from = vacancie["payment_from"]
salary_to = vacancie["payment_to"]
salary_currency = vacancie["currency"]
return get_average_salary(salary_from, salary_to, salary_currency)
def fill_statistic_hh(languages):
top_languages_hh = {}
for lang in languages:
top_languages_hh.update({lang: {"vacancies_found": 0,
"vacancies_processed": 0,
"average_salary": 0,
}
})
moscow_id = "1"
industry_id = "1.221"
amount_days = "30"
payload = {'area': moscow_id,
'specialization': industry_id,
'period': amount_days,
"text" : f"'{' or '.join(languages)}'",
}
url = f"https://api.hh.ru/vacancies"
response = requests.get(url, params=payload)
response.raise_for_status()
page_objects = response.json()
found = page_objects["found"]
page_count = found // 20 + round(found % 20)
if page_count > 100: page_count = 99
page = 1
vacancies = page_objects['items']
while page <= page_count:
payload = {'area': moscow_id,
'page': page,
'specialization': industry_id,
'period': amount_days,
"text": f"'{' or '.join(languages)}'",
}
response = requests.get(url, params=payload)
response.raise_for_status()
page_objects = response.json()
page_payload = page_objects['items']
vacancies = vacancies + page_payload
page += 1
for vacancie in vacancies:
for programming_language, statistic_values in top_languages_hh.items():
if str(programming_language).lower() not in vacancie["name"].lower():
continue
statistic_values["vacancies_found"] = statistic_values["vacancies_found"] + 1
one_salary = predict_rub_salary_hh(vacancie)
if one_salary:
statistic_values["vacancies_processed"] = statistic_values["vacancies_processed"] + 1
statistic_values["average_salary"] = round((statistic_values["average_salary"] + one_salary) / 2, 2)
top_languages_hh[programming_language] = statistic_values
break
return top_languages_hh
def fill_statistic_sj(x_api_app_id, languages):
top_languages_sj = {}
for lang in languages:
top_languages_sj.update({lang: {"vacancies_found": 0,
"vacancies_processed": 0,
"average_salary": 0,
}
})
url = f'https://api.superjob.ru/2.0/vacancies/'
headers = {'X-Api-App-Id': x_api_app_id}
moscow_id= 4
industry_id = 48
place_search = 1
payload = {"town": moscow_id,
"catalogues": industry_id,
"keywords.srws": place_search,
"keywords.skwc": 'or',
"keywords.key": languages,
}
response = requests.get(url, headers=headers, params=payload)
response.raise_for_status()
page_objects = response.json()
found = page_objects["total"]
page_count = found // 20 + round(found % 20)
if page_count > 25: page_count = 24
page = 1
vacancies = page_objects['objects']
while page <= page_count:
payload = {"town": moscow_id,
"catalogues": industry_id,
'page': page,
"keywords.srws": place_search,
"keywords.skwc": 'or',
"keywords.key": languages,
}
response = requests.get(url, params=payload, headers=headers)
response.raise_for_status()
page_objects = response.json()
page_payload = page_objects['objects']
vacancies = vacancies + page_payload
page += 1
for vacancie in vacancies:
for programming_language, statistic_values in top_languages_sj.items():
if str(programming_language).lower() not in vacancie["profession"].lower():
continue
statistic_values["vacancies_found"] = statistic_values["vacancies_found"] + 1
one_salary = predict_rub_salary_sj(vacancie)
if one_salary:
statistic_values["vacancies_processed"] = statistic_values["vacancies_processed"] + 1
statistic_values["average_salary"] = round((statistic_values["average_salary"] + one_salary) / 2, 2)
top_languages_sj[programming_language] = statistic_values
break
return top_languages_sj
def main():
load_dotenv()
x_api_app_id = os.environ['SJ_X-API-APP-ID']
languages = ['JavaScript', 'Java', 'Python', 'Ruby', 'PHP', 'C++', 'C#', 'Scala']
top_languages_hh = fill_statistic_hh(languages)
top_languages_sj = fill_statistic_sj(x_api_app_id, languages)
print(get_ansi_table(top_languages_hh, 'HH Moscow'))
print()
print(get_ansi_table(top_languages_sj, 'SuperJob Moscow'))
print()
if __name__ == "__main__":
main()