这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@liimee
Copy link
Contributor

@liimee liimee commented Sep 6, 2022

What does this PR do?

adds the weather.html template

Why is this change important?

How to test this PR locally?

Author's checklist

Related issues


So the DDG weather engine basically looks like this when using this template (so far):

# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""DuckDuckGo Weather"""

from json import loads
from urllib.parse import quote

from datetime import datetime
from flask_babel import gettext

about = {
    "website": 'https://duckduckgo.com/',
    "wikidata_id": 'Q12805',
    "official_api_documentation": None,
    "use_official_api": True,
    "require_api_key": False,
    "results": "JSON",
}

categories = ["others"]

url = "https://duckduckgo.com/js/spice/forecast/{query}/{lang}"


def request(query, params):
    params["url"] = url.format(query=quote(
        query), lang=params['language'].split('-')[0])

    return params


def f_to_c(temperature):
    return "%.2f" % ((temperature - 32) / 1.8)


def response(resp):
    results = []

    if resp.text.strip() == "ddg_spice_forecast();":
        return []

    result = loads(resp.text[resp.text.find(
        '\n') + 1: resp.text.rfind('\n') - 2])

    current = result["currently"]

    forecast_data = []
    last_date = None
    current_data = {}

    for time in result['hourly']['data']:
        current_time = datetime.fromtimestamp(time['time'])

        if last_date != current_time.date():
            if last_date is not None:
                forecast_data.append(current_data)

            today = next(day for day in result['daily']['data'] if datetime.fromtimestamp(
                day['time']).date() == current_time.date())

            current_data = {
                'date': current_time.strftime('%Y-%m-%d'),
                'metric': {
                    'min_temp': f_to_c(today['temperatureLow']),
                    'max_temp': f_to_c(today['temperatureHigh']),
                },
                'uv_index': today['uvIndex'],
                'sunrise': datetime.fromtimestamp(today['sunriseTime']).strftime('%H:%M'),
                'sunset': datetime.fromtimestamp(today['sunsetTime']).strftime('%H:%M'),
                'forecast': []
            }

        current_data['forecast'].append({
            'time': current_time.strftime('%H:%M'),
            'metric': {
                'temperature': f_to_c(time['temperature']),
                'feels_like': f_to_c(time['apparentTemperature']),
                'wind_speed': '%.2f' % (time['windSpeed'] * 1.6093440006147),
                'visibility': time['visibility']
            },
            'imperial': {
                'temperature': time['temperature'],
                'feels_like': time['apparentTemperature'],
                'wind_speed': time['windSpeed']
            },
            'condition': time['summary'],
            'wind_direction': time['windBearing'],
            'humidity': time['humidity'] * 100,
        })

        last_date = current_time.date()

    forecast_data.append(current_data)

    results.append({
        'template': 'weather.html',
        'location': result['flags']['ddg-location'],
        'currently': {
            'metric': {
                'temperature': f_to_c(current['temperature']),
                'feels_like': f_to_c(current['apparentTemperature']),
                'wind_speed': '%.2f' % (current['windSpeed'] * 1.6093440006147),
                'visibility': current['visibility']
            },
            'imperial': {
                'temperature': current['temperature'],
                'feels_like': current['apparentTemperature'],
                'wind_speed': current['windSpeed']
            },
            'condition': current['summary'],
            'wind_direction': current['windBearing'],
            'humidity': current['humidity'] * 100,
        },
        'forecast': forecast_data
    })

    return results

I did this a bit quickly so I believe there may be something I missed

@return42 return42 force-pushed the weather-t branch 2 times, most recently from e7ebbf9 to 6995851 Compare September 18, 2022 18:25
liimee and others added 2 commits September 18, 2022 20:32
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
@return42
Copy link
Member

Hi @liimee I am very sorry for the long delay (I had a lot of work the last weeks) ..

I just review this PR, I added ddg weather commit and rebased your branch ... today I have no more time, I will continue the review in the nest days.

@dalf
Copy link
Contributor

dalf commented Sep 23, 2022

@return42 found met.no: https://api.met.no/doc/locationforecast/datamodel
The early draft #1800 creates a data model based on that, however there is no template. We will iterate over this PR to create a model, and after that we will add the template.


About the template in this PR: I like the usage of <detail>.

In a similar way to DDG or Google, some tabs would ease the reading.

image

It is possible to create tab without javascript using label, input and CSS:
https://jsfiddle.net/dalf73/9jngup0z/40/

In the theme, there are actually some macros to do that, I'm not sure they are usable in this context. It is not an issue to create some specific CSS for the weather template.

For the template we can, we can use the icons from there (MIT license)


In the meantime:

@return42
Copy link
Member

return42 commented Sep 23, 2022

It is possible to create tab without javascript using

Before we progress in this PR, please let me implement the data model .. when we have a good data model, we can progress with UI and HTML tables .. in this PR.

I will come back to this PR .. give me the time to sort my things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants