1.6.0 parsing timezones
This commit is contained in:
@@ -3,7 +3,9 @@ from .models import *
|
||||
import hashlib, json
|
||||
from datetime import datetime, timedelta
|
||||
from django.db.models import Q
|
||||
from timezonefinder import TimezoneFinder
|
||||
|
||||
tzf = TimezoneFinder()
|
||||
|
||||
def search_cities_in_db(search_str):
|
||||
|
||||
@@ -57,20 +59,31 @@ def create_airports_by_airportsList(airportsList, city=None):
|
||||
if airport_Dict['iata']:
|
||||
kwargs.update({'iata_code': airport_Dict['iata']})
|
||||
airport = Airport.objects.get(**kwargs)
|
||||
if airport.geo_lat and airport.geo_lon and not airport.timezone:
|
||||
airport.timezone = tzf.timezone_at(
|
||||
lng=float(airport.geo_lon), lat=float(airport.geo_lat))
|
||||
airport.save(update_fields=['timezone'])
|
||||
print(f'airport {airport.international_name} - {airport.timezone}')
|
||||
except Airport.DoesNotExist:
|
||||
print(f' - - {airport_Dict["iata"]} не найден в БД > добавляем')
|
||||
except Exception as e:
|
||||
print(f'error = {str(e)}')
|
||||
|
||||
if not airport:
|
||||
geo_lat = float(airport_Dict['@lat'])
|
||||
geo_lon = float(airport_Dict['@lon'])
|
||||
tz = tzf.timezone_at(lng=geo_lon, lat=geo_lat)
|
||||
print(f'airport {airport_Dict["int_name"]} - {tz}')
|
||||
|
||||
airport_kwargs = {
|
||||
'city': city,
|
||||
|
||||
# 'name_ru': airport_Dict['name:ru'],
|
||||
# 'name_en': airport_Dict['name:en'],
|
||||
'timezone': tz,
|
||||
|
||||
'geo_lat': str(airport_Dict['@lat']),
|
||||
'geo_lon': str(airport_Dict['@lon']),
|
||||
'geo_lat': str(geo_lat),
|
||||
'geo_lon': str(geo_lon),
|
||||
|
||||
'international_name': airport_Dict['int_name'],
|
||||
|
||||
@@ -119,7 +132,10 @@ def parse_data():
|
||||
|
||||
country = Country.objects.get(**kwargs)
|
||||
|
||||
if country.parsing_finished_DT and (datetime.now() - country.parsing_finished_DT).days < 30:
|
||||
if (country.parsing_finished_DT
|
||||
and (datetime.now() - country.parsing_finished_DT).days < 30
|
||||
and country.timezone
|
||||
):
|
||||
print(f' + {country.name} - существует в БД, не требует парсинга')
|
||||
continue
|
||||
|
||||
@@ -194,6 +210,11 @@ def parse_data():
|
||||
else:
|
||||
print(f'error = {str(e)}')
|
||||
|
||||
geo_lat = float(city_Dict['@lat'])
|
||||
geo_lon = float(city_Dict['@lon'])
|
||||
tz = tzf.timezone_at(lng=geo_lon, lat=geo_lat)
|
||||
print(f'city {city_Dict["name:en"]} - {tz}')
|
||||
|
||||
# собираем данные
|
||||
city_kwargs = {
|
||||
'country': country,
|
||||
@@ -201,8 +222,11 @@ def parse_data():
|
||||
# 'name_ru': city_Dict['name:ru'],
|
||||
# 'name_en': city_Dict['name:en'],
|
||||
|
||||
'geo_lat': str(city_Dict['@lat']),
|
||||
'geo_lon': str(city_Dict['@lon']),
|
||||
'timezone': tz,
|
||||
|
||||
'geo_lat': str(geo_lat),
|
||||
'geo_lon': str(geo_lon),
|
||||
|
||||
}
|
||||
|
||||
if city_Dict['name:ru']:
|
||||
@@ -232,6 +256,9 @@ def parse_data():
|
||||
hash_data = hashlib.md5(json.dumps(country_Dict, sort_keys=True, ensure_ascii=True).encode('utf-8')).hexdigest()
|
||||
country.add_node_to_json_data({'hash': hash_data})
|
||||
|
||||
country.timezone = tzf.timezone_at(lng=float(country.geo_lon), lat=float(country.geo_lat))
|
||||
print(f'country {country.name} - {country.timezone}')
|
||||
|
||||
if 'parsing_status' in country_Dict and country_Dict['parsing_status'] == 'finished':
|
||||
country.parsing_finished_DT = datetime.now()
|
||||
country.save(update_fields=['parsing_finished_DT'])
|
||||
|
||||
Reference in New Issue
Block a user