1.6.0 parsing timezones
This commit is contained in:
@@ -2,6 +2,7 @@ from django.contrib import admin
|
||||
from sets.admin import Admin_Trans_BaseModel
|
||||
from .models import *
|
||||
from modeltranslation.admin import TranslationAdmin
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
class Admin_Country(Admin_Trans_BaseModel):
|
||||
fieldsets = [
|
||||
@@ -10,11 +11,20 @@ class Admin_Country(Admin_Trans_BaseModel):
|
||||
'fields': [
|
||||
'name', 'enable', 'short_code', 'code',
|
||||
]
|
||||
}]
|
||||
}],
|
||||
[_('Дополнительно'), {
|
||||
'classes': ['wide', 'collapse'],
|
||||
'fields': (
|
||||
'timezone',
|
||||
'geo_lat', 'geo_lon',
|
||||
'json_data',
|
||||
)
|
||||
}],
|
||||
]
|
||||
|
||||
list_display = [
|
||||
'id', 'name', 'name_en', 'name_ru',
|
||||
'timezone',
|
||||
'short_code', 'code',
|
||||
'enable', 'area_id', 'parsing_finished_DT',
|
||||
'order', 'modifiedDT', 'createDT']
|
||||
@@ -28,12 +38,21 @@ class Admin_City(Admin_Trans_BaseModel):
|
||||
'fields': [
|
||||
'name', 'enable', 'country',
|
||||
]
|
||||
}]
|
||||
}],
|
||||
[_('Дополнительно'), {
|
||||
'classes': ['wide', 'collapse'],
|
||||
'fields': (
|
||||
'timezone',
|
||||
'geo_lat', 'geo_lon',
|
||||
'json_data',
|
||||
)
|
||||
}],
|
||||
]
|
||||
|
||||
list_display = [
|
||||
'id', 'name', 'name_en', 'name_ru',
|
||||
'country',
|
||||
'timezone',
|
||||
'enable', 'area_id', 'parsing_finished_DT',
|
||||
'order', 'modifiedDT', 'createDT']
|
||||
search_fields = ['id', 'name_en', 'name_ru', 'country__name']
|
||||
@@ -50,12 +69,21 @@ class Admin_Airport(Admin_Trans_BaseModel):
|
||||
'international_name',
|
||||
# 'area_id'
|
||||
]
|
||||
}]
|
||||
}],
|
||||
[_('Дополнительно'), {
|
||||
'classes': ['wide', 'collapse'],
|
||||
'fields': (
|
||||
'timezone',
|
||||
'geo_lat', 'geo_lon',
|
||||
'json_data',
|
||||
)
|
||||
}],
|
||||
]
|
||||
|
||||
list_display = [
|
||||
'id', 'name', 'name_en', 'name_ru',
|
||||
'city', 'iata_code', 'icao_code',
|
||||
'timezone',
|
||||
'international_name',
|
||||
'enable', 'area_id', 'parsing_finished_DT',
|
||||
'order', 'modifiedDT', 'createDT']
|
||||
|
||||
@@ -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'])
|
||||
|
||||
@@ -15,6 +15,8 @@ class Country(BaseModel):
|
||||
geo_lat = models.CharField(max_length=20, verbose_name=_('GPS широта'), blank=True, null=True)
|
||||
geo_lon = models.CharField(max_length=20, verbose_name=_('GPS долгота'), blank=True, null=True)
|
||||
|
||||
timezone = models.CharField(max_length=250, verbose_name=_('Часовая зона'), blank=True, null=True)
|
||||
|
||||
area_id = models.BigIntegerField(blank=True, null=True)
|
||||
|
||||
parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)
|
||||
@@ -46,6 +48,8 @@ class City(BaseModel):
|
||||
|
||||
area_id = models.BigIntegerField(blank=True, null=True)
|
||||
|
||||
timezone = models.CharField(max_length=250, verbose_name=_('Часовая зона'), blank=True, null=True)
|
||||
|
||||
parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
@@ -83,6 +87,8 @@ class Airport(BaseModel):
|
||||
geo_lat = models.CharField(max_length=20, verbose_name=_('GPS широта'), blank=True, null=True)
|
||||
geo_lon = models.CharField(max_length=20, verbose_name=_('GPS долгота'), blank=True, null=True)
|
||||
|
||||
timezone = models.CharField(max_length=250, verbose_name=_('Часовая зона'), blank=True, null=True)
|
||||
|
||||
area_id = models.BigIntegerField(blank=True, null=True)
|
||||
|
||||
parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)
|
||||
|
||||
Reference in New Issue
Block a user