wit.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:nsfw 作者: vied12 项目源码 文件源码
def get_air_quality(request):
    geolocator = Nominatim()
    context = request['context']
    entities = request['entities']
    loc = first_entity_value(entities, 'location')

    for context_key in (
        'missingLocation',
        'outOfGermany',
        'subscribed',
        'notSubscribed',
        'location',
        'station',
        'stationId',
        'lastAlertValue',
        'lastAlertDate',
        'kind',
        'clean',
        'notFound',
    ):
        try:
            del context[context_key]
        except KeyError:
            pass
    if not loc:
        context['missingLocation'] = True
    else:
        loc = geolocator.geocode(loc, language='en')
        if not loc:
            loc = geolocator.geocode('{}, Germany'.format(loc), language='en')
        if loc:
            closest_station = get_closest_station(loc.latitude, loc.longitude)
            # is subscribed ?
            if is_subscribed(request['session_id'], closest_station):
                context['subscribed'] = True
            else:
                context['notSubscribed'] = True
            # oldest alert we want
            max_date = datetime.datetime.now() - datetime.timedelta(days=2)
            last_alert = Alert.objects.filter(station=closest_station, report__date__gte=max_date).last()
            context['location'] = loc.address
            context['station'] = closest_station.name
            context['stationId'] = closest_station.pk
            cache.set(request['session_id'], closest_station.pk)
            if last_alert:
                context['lastAlertValue'] = last_alert.value
                context['lastAlertDate'] = last_alert.report.date.strftime('%x')
                context['kind'] = last_alert.report.kind
            else:
                context['clean'] = True
        else:
            context['notFound'] = True
    return context


# Setup Actions
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号