-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data_sj.py
86 lines (62 loc) · 2.55 KB
/
get_data_sj.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
from tools import get_predict_salary, create_dict_language_statistics
import requests
def get_predict_rub_salary_sj(vacancy):
if vacancy['currency'] == 'rub':
return get_predict_salary(vacancy['payment_from'], vacancy['payment_to'])
def provide_headers_sj(secret_key):
return {
'X-Api-App-Id': secret_key
}
def provide_params_sj(page, area_id, text=''):
return {
'town': area_id,
'catalogues': 48, # Develop, programing
'count': 100, # vacancies on page
'page': page,
'keyword': text,
}
def get_statistics_language_sj(api_url, secret_key, area_id, language):
vacancies_found = 0
all_salaries_vacancies = []
page = 0
more = True
while more:
response = requests.get(api_url,
headers=provide_headers_sj(secret_key),
params=provide_params_sj(page, area_id, text=language))
response.raise_for_status()
page += 1
more = response.json()['more']
vacancies_found = response.json()['total']
vacancies_language = response.json()['objects']
salaries_vacancies = [get_predict_rub_salary_sj(vacancy) for vacancy in vacancies_language]
all_salaries_vacancies.extend(salaries_vacancies)
is_salary = list(filter(lambda x: x is not None, all_salaries_vacancies))
vacancies_processed = len(is_salary)
if vacancies_processed:
average_salary = sum(is_salary) // vacancies_processed
else:
average_salary = 0
return create_dict_language_statistics(language, vacancies_found, vacancies_processed, average_salary)
def parse_areas_sj(data, area):
for town in data['towns']:
if area.capitalize() == town['title']:
return town['id']
for region in data['regions']:
for town in region['towns']:
if area.lower() == town['title'].lower():
return town['id']
def get_area_id_sj(area):
url = 'https://api.superjob.ru/2.0/regions/combined/'
response = requests.get(url)
response.raise_for_status()
response = response.json()[0]
return parse_areas_sj(response, area)
def get_data_from_superjob(secret_key, city, programming_languages):
api_url = 'https://api.superjob.ru/2.0/vacancies/'
dict_languages_statistics = {}
area_id = get_area_id_sj(city)
if area_id:
for language in programming_languages:
dict_languages_statistics.update(get_statistics_language_sj(api_url, secret_key, area_id, language))
return dict_languages_statistics