-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (58 loc) · 2.33 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
from utils import get_or_create_metro_line, get_or_create_subway_station, get_or_create_city
from useragent import random_user_agent
from urllib.error import HTTPError
from contextlib import closing
from dotenv import load_dotenv
import urllib.request
import urllib.parse
import psycopg2
import json
import gzip
import os
load_dotenv()
def get_json(url, headers=None):
req = urllib.request.Request(
url,
data=None,
headers=headers if headers else {
'User-Agent': random_user_agent(),
'Accept': 'text/html,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
}
)
with urllib.request.urlopen(req) as response:
if response.getcode() != 200:
raise HTTPError(url, response.getcode(), None, {}, None)
return gzip.decompress(response.read()).decode('utf-8')
if __name__ == '__main__':
API_URL = 'https://api.hh.ru/metro/'
json_data = json.loads(get_json(API_URL))
towns = ['Москва', 'Санкт-Петербург']
total_towns = 0
total_lines = 0
total_stations = 0
with closing(psycopg2.connect(
dbname=os.environ['dbname'],
user=os.environ['user'],
password=os.environ['password'],
host=os.environ['host']
)) as conn:
with conn.cursor() as cursor:
conn.autocommit = True
for city in json_data:
if city['name'] in towns:
city_created, city_id = get_or_create_city(cursor, city['name'])
if city_created:
total_towns += 1
for line in city['lines']:
line_created, line_id = get_or_create_metro_line(cursor, line['name'], line['hex_color'])
if line_created:
total_lines += 1
for station in line['stations']:
created, _ = get_or_create_subway_station(cursor, station, city_id, line_id)
if created:
total_stations += 1
print('Total cities added:', total_towns)
print('Total metro lines added:', total_lines)
print('Total metro stations added:', total_stations)