Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73df7a2b5d | ||
|
|
3612658323 | ||
|
|
c0503458e4 | ||
|
|
0baa1277ef | ||
|
|
5d025998b9 | ||
|
|
a14f49742b | ||
|
|
6178c694f9 | ||
|
|
2e9d04c10e | ||
|
|
ece1a58a27 | ||
|
|
a12a81e26d | ||
|
|
d214849e2e | ||
|
|
9ae9d1be14 | ||
|
|
65b6adbf16 | ||
|
|
2500ba19bd | ||
|
|
f257caf42c | ||
|
|
80d5c62480 | ||
|
|
ff675bc04b | ||
|
|
d47dcf69e6 | ||
|
|
91a1e4498c | ||
|
|
e6e97ed209 | ||
|
|
126d5310f5 | ||
|
|
ee3003c09a | ||
|
|
b556f2f948 | ||
|
|
d9016a0cb0 | ||
|
|
41914d7f74 | ||
|
|
de466de805 | ||
|
|
1bffafd6a3 | ||
|
|
46e72feb82 | ||
|
|
fdf6e0eac3 | ||
|
|
60651de186 | ||
|
|
bed8d7af7b | ||
|
|
6683b7b5b0 | ||
|
|
fa0edca522 | ||
|
|
f76f33b501 | ||
|
|
a6f6586969 | ||
|
|
1835935eaf | ||
|
|
5db61da32b | ||
|
|
d44c8076b4 | ||
|
|
24214ebc3a | ||
|
|
b5a8a1536c | ||
|
|
4aafe720dd | ||
|
|
7c2436fb57 | ||
|
|
d6487facf4 |
+1
-1
@@ -10,4 +10,4 @@ max_line_length = 120
|
||||
|
||||
[*.md]
|
||||
max_line_length = off
|
||||
trim_trailing_whitespace = false
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
DOCKER_REPO=git.shmyga.ru
|
||||
DOCKER_GROUP=infernalgames
|
||||
DOCKER_ROOT="$DOCKER_REPO/$DOCKER_GROUP"
|
||||
VERSION=$(grep -m 1 'version' ./pyproject.toml | grep -oP 'version\s*=\s*"\K[^"]+')
|
||||
PYTHON_VERSION=$(grep -m 1 'python' ./pyproject.toml | grep -oP 'python\s*=\s*"\^?\K[^"]+')
|
||||
DOCKER_PROJECTS=("gallery")
|
||||
DOCKER_ROOT="${DOCKER_REPO}/${DOCKER_GROUP}"
|
||||
DOCKER_TAG="${DOCKER_TAG:-latest}"
|
||||
|
||||
OPENWEATHER_KEY=""
|
||||
|
||||
@@ -1,2 +1,7 @@
|
||||
[presets.python.tasks.setup.playwright]
|
||||
commands = [".venv/bin/playwright install chromium --with-deps --only-shell"]
|
||||
modules = [".", "static"]
|
||||
|
||||
[presets.locales.tasks.build.locales]
|
||||
priority = 1
|
||||
conditions = ["{{ 'locales/ru/LC_MESSAGES' | file }}"]
|
||||
cwd = "locales/ru/LC_MESSAGES"
|
||||
commands = ["msgfmt messages.po"]
|
||||
|
||||
+9
-4
@@ -23,14 +23,19 @@ COPY static ./
|
||||
RUN npm run build
|
||||
|
||||
FROM python:3.14-slim
|
||||
ENV PATH="/app/.venv/bin:$PATH"
|
||||
WORKDIR /app
|
||||
|
||||
ENV PLAYWRIGHT_BROWSERS_PATH="/opt/playwright-browsers"
|
||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
||||
--mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||
pip install playwright && \
|
||||
playwright install --with-deps chromium
|
||||
|
||||
ENV PATH="/app/.venv/bin:$PATH"
|
||||
ENV TZ="Europe/Moscow"
|
||||
|
||||
COPY --from=builder /app ./
|
||||
RUN .venv/bin/playwright install chromium --with-deps --only-shell
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /app ./
|
||||
COPY --from=node-builder /app/dist ./static/dist
|
||||
|
||||
COPY gallery gallery/
|
||||
|
||||
@@ -1,13 +1,75 @@
|
||||
# API Gallery
|
||||
|
||||
Weather and TV program API
|
||||
Currently implemented APIs:
|
||||
|
||||

|
||||
- Weather forecast
|
||||
- TV program
|
||||
|
||||
## View
|
||||
## API
|
||||
|
||||
Swagger: https://api.shmyga.ru/docs
|
||||
|
||||
### Weather
|
||||
|
||||
#### Providers list
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/weather/providers
|
||||
```
|
||||
|
||||
#### Locations search
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/weather/{provider}/locations?query={query}
|
||||
```
|
||||
|
||||
#### Weather forecast
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/weather/{provider}/{location_id}/day/{date}
|
||||
https://api.shmyga.ru/api/weather/{provider}/{location_id}/days/{days_count}
|
||||
```
|
||||
|
||||
### TV program
|
||||
|
||||
#### Providers list
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/schedule/providers
|
||||
```
|
||||
|
||||
#### Channels search
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/schedule/{provider}/channels?query={query}
|
||||
```
|
||||
|
||||
#### TV program
|
||||
|
||||
```
|
||||
https://api.shmyga.ru/api/schedule/{provider}/{channel_id}/{date}
|
||||
```
|
||||
|
||||
## Providers
|
||||
|
||||
### Weather
|
||||
|
||||
| Provider | Site | API sample |
|
||||
| :- | :- | :- |
|
||||
| gismeteo | https://www.gismeteo.ru | https://api.shmyga.ru/api/weather/gismeteo/orel-4432/day/today |
|
||||
| openweather | https://openweathermap.org | https://api.shmyga.ru/api/weather/openweather/52.9685433%3A36.0692477/day/today |
|
||||
| yandex | https://yandex.ru/pogoda | https://api.shmyga.ru/api/weather/yandex/10/day/today |
|
||||
| rp5 | https://rp5.ru | https://api.shmyga.ru/api/weather/rp5/Погода_в_Орле%2C_Орловская_область/day/today |
|
||||
|
||||
### TV program
|
||||
|
||||
| Provider | Site | API sample |
|
||||
| :- | :- | :- |
|
||||
| matchtv | https://matchtv.ru/tvguide | https://api.shmyga.ru/api/schedule/matchtv/igra/today |
|
||||
| yandex | https://tv.yandex.ru | https://api.shmyga.ru/api/schedule/yandex/146/today |
|
||||
|
||||
## Demo
|
||||
|
||||
https://api.shmyga.ru
|
||||
|
||||
## Swagger
|
||||
|
||||
https://api.shmyga.ru/docs
|
||||

|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ caches.set_config(
|
||||
{
|
||||
"default": {
|
||||
"cache": "aiocache.SimpleMemoryCache",
|
||||
"serializer": {"class": "aiocache.serializers.StringSerializer"},
|
||||
"serializer": {"class": "aiocache.serializers.PickleSerializer"},
|
||||
},
|
||||
"redis": {
|
||||
"cache": "aiocache.RedisCache",
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from gallery.sketch.bundle import ApiBundle
|
||||
from gallery.util import root_path
|
||||
|
||||
from .route import api, doc, view
|
||||
from .route import api, view
|
||||
|
||||
|
||||
def build_app(api_bundle: ApiBundle) -> FastAPI:
|
||||
@@ -15,7 +16,12 @@ def build_app(api_bundle: ApiBundle) -> FastAPI:
|
||||
)
|
||||
app.state.api = api_bundle
|
||||
app.mount("/static", StaticFiles(directory=root_path / "static/dist"))
|
||||
doc.mount(app)
|
||||
app.include_router(api.router)
|
||||
api.app.state.api = api_bundle
|
||||
app.mount("/api", api.app)
|
||||
app.include_router(view.router)
|
||||
|
||||
@app.get("/docs")
|
||||
def get_docs():
|
||||
return RedirectResponse("/api/docs")
|
||||
|
||||
return app
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
|
||||
|
||||
def date_parser(date: str) -> datetime.date:
|
||||
if date == "today":
|
||||
return datetime.date.today()
|
||||
elif date == "tomorrow":
|
||||
return datetime.date.today() + datetime.timedelta(days=1)
|
||||
elif date.startswith("day-"):
|
||||
days = int(date.split("-")[-1])
|
||||
return datetime.date.today() + datetime.timedelta(days=days)
|
||||
else:
|
||||
try:
|
||||
return datetime.date.strptime(date, "%Y-%m-%d")
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail={"date": f"{exc}"},
|
||||
) from exc
|
||||
|
||||
|
||||
DateDepends = Annotated[datetime.date, Depends(date_parser)]
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Annotated
|
||||
from typing import Annotated, NamedTuple
|
||||
|
||||
from fastapi import Depends
|
||||
|
||||
@@ -7,3 +7,35 @@ from gallery.sketch.schedule.api import ScheduleApi
|
||||
from .api import api_resolver
|
||||
|
||||
ScheduleApiDepends = Annotated[ScheduleApi, Depends(api_resolver(ScheduleApi))]
|
||||
|
||||
|
||||
class ChannelDef(NamedTuple):
|
||||
provider: str
|
||||
id: str
|
||||
|
||||
|
||||
GROUP_CANNELS_MAP: dict[str, list[ChannelDef]] = {
|
||||
"central": [
|
||||
ChannelDef("yandex", "pervyy-16"),
|
||||
ChannelDef("yandex", "rossiya-1-31"),
|
||||
ChannelDef("yandex", "ntv-11"),
|
||||
ChannelDef("yandex", "sts-8"),
|
||||
],
|
||||
"sport": [
|
||||
ChannelDef("matchtv", "matchtv"),
|
||||
ChannelDef("matchtv", "igra"),
|
||||
ChannelDef("matchtv", "arena"),
|
||||
ChannelDef("matchtv", "futbol-1"),
|
||||
ChannelDef("matchtv", "futbol-2"),
|
||||
ChannelDef("matchtv", "futbol-3"),
|
||||
ChannelDef("matchtv", "strana"),
|
||||
ChannelDef("yandex", "start-103"),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def resolve_group_channels(group_id: str) -> list[ChannelDef]:
|
||||
return GROUP_CANNELS_MAP.get(group_id) or [ChannelDef(*item.split(":")) for item in group_id.split(",")]
|
||||
|
||||
|
||||
ScheduleGroupChannelsDepends = Annotated[list[ChannelDef], Depends(resolve_group_channels)]
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from gallery.easel.route import doc
|
||||
|
||||
from . import schedule, weather
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
router.include_router(weather.router)
|
||||
router.include_router(schedule.router)
|
||||
app = FastAPI(
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.include_router(weather.router)
|
||||
app.include_router(schedule.router)
|
||||
doc.mount(app, "/api")
|
||||
|
||||
@@ -1,15 +1,32 @@
|
||||
import datetime
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
from gallery.easel.depends.schedule import ScheduleApiDepends
|
||||
from gallery.easel.depends.schedule import (
|
||||
GROUP_CANNELS_MAP,
|
||||
ScheduleApiDepends,
|
||||
ScheduleGroupChannelsDepends,
|
||||
)
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
from gallery.sketch.schedule.model import Channel, Schedule
|
||||
|
||||
from ...depends.date import DateDepends
|
||||
|
||||
router = APIRouter(prefix="/schedule", tags=["Schedule"])
|
||||
|
||||
|
||||
@router.get("/groups")
|
||||
async def get_api_schedule_groups():
|
||||
return list(GROUP_CANNELS_MAP.keys())
|
||||
|
||||
|
||||
@router.get("/group/{group_id}/{date}")
|
||||
async def get_api_schedule_group_schedule(
|
||||
request: AppRequest, channels: ScheduleGroupChannelsDepends, date: DateDepends
|
||||
) -> list[Schedule]:
|
||||
api = request.app.state.api
|
||||
return [await api.get_api(ScheduleApi, channel.provider).get_schedule(channel.id, date) for channel in channels]
|
||||
|
||||
|
||||
@router.get("/providers")
|
||||
async def get_api_weather_providers(request: AppRequest) -> list[str]:
|
||||
return request.app.state.api.get_api_providers(ScheduleApi)
|
||||
@@ -22,6 +39,6 @@ async def find_api_schedule_channels(schedule_api: ScheduleApiDepends, query: st
|
||||
|
||||
@router.get("/{provider}/{channel}/{date}")
|
||||
async def get_api_schedule_channel_schedule(
|
||||
schedule_api: ScheduleApiDepends, channel: str, date: datetime.date
|
||||
schedule_api: ScheduleApiDepends, channel: str, date: DateDepends
|
||||
) -> Schedule:
|
||||
return await schedule_api.get_schedule(channel, date)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import datetime
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
@@ -7,6 +5,8 @@ from gallery.easel.depends.weather import WeatherApiDepends
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
from gallery.sketch.weather.model import Location, WeatherResponse
|
||||
|
||||
from ...depends.date import DateDepends
|
||||
|
||||
router = APIRouter(prefix="/weather", tags=["Weather"])
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ async def find_api_weather_locations(weather_api: WeatherApiDepends, query: str)
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}/day/{date}")
|
||||
async def get_api_weather_day(weather_api: WeatherApiDepends, location: str, date: datetime.date) -> WeatherResponse:
|
||||
async def get_api_weather_day(weather_api: WeatherApiDepends, location: str, date: DateDepends) -> WeatherResponse:
|
||||
return await weather_api.get_day(location, date)
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ from fastapi.openapi.docs import (
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
|
||||
def mount(app: FastAPI):
|
||||
def mount(app: FastAPI, prefix: str | None = None):
|
||||
openapi_url = prefix + app.openapi_url if prefix else app.openapi_url
|
||||
app.mount(
|
||||
"/docs/static",
|
||||
StaticFiles(directory=Path(__file__).parent / "static"),
|
||||
@@ -17,7 +18,7 @@ def mount(app: FastAPI):
|
||||
@app.get("/docs", include_in_schema=False)
|
||||
async def custom_swagger_ui_html():
|
||||
return get_swagger_ui_html(
|
||||
openapi_url=app.openapi_url,
|
||||
openapi_url=openapi_url,
|
||||
title=app.title + " - Swagger UI",
|
||||
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
|
||||
swagger_js_url="docs/static/swagger-ui-bundle.js",
|
||||
@@ -31,7 +32,7 @@ def mount(app: FastAPI):
|
||||
@app.get("/redoc", include_in_schema=False)
|
||||
async def redoc_html():
|
||||
return get_redoc_html(
|
||||
openapi_url=app.openapi_url,
|
||||
openapi_url=openapi_url,
|
||||
title=app.title + " - ReDoc",
|
||||
redoc_js_url="docs/static/redoc.standalone.js",
|
||||
)
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
<div class="app col-lg-8 mx-auto p-3 py-md-5">
|
||||
{% if not is_widget %}
|
||||
<header class="app-header pb-3 mb-5 border-bottom">
|
||||
<div>{{request.query_params.widget}}</div>
|
||||
<div class="link-list">
|
||||
<app-link href="/"
|
||||
icon="gear">API Gallery</app-link>
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
import datetime
|
||||
from enum import Enum
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class TagType(str, Enum):
|
||||
DAY = "day"
|
||||
DAYS = "days"
|
||||
|
||||
|
||||
class Tag(NamedTuple):
|
||||
type: TagType
|
||||
date: datetime.date
|
||||
days: int = 1
|
||||
|
||||
def __str__(self) -> str:
|
||||
if self.type == TagType.DAY:
|
||||
today = datetime.date.today()
|
||||
day = (self.date - today).days
|
||||
return f"day-{day}"
|
||||
elif self.type == TagType.DAYS:
|
||||
return f"days-{self.days}"
|
||||
else:
|
||||
raise ValueError(self.type)
|
||||
|
||||
|
||||
class TagUtil:
|
||||
@classmethod
|
||||
def parse_tag(cls, tag: str) -> Tag:
|
||||
if tag == "today":
|
||||
return Tag(TagType.DAY, datetime.date.today())
|
||||
elif tag == "tomorrow":
|
||||
return Tag(TagType.DAY, datetime.date.today() + datetime.timedelta(days=1))
|
||||
elif tag.startswith("day-"):
|
||||
days = int(tag.split("-")[-1])
|
||||
return Tag(
|
||||
TagType.DAY,
|
||||
datetime.date.today() + datetime.timedelta(days=days),
|
||||
)
|
||||
elif tag.startswith("days-"):
|
||||
days = int(tag.split("-")[-1])
|
||||
return Tag(TagType.DAYS, datetime.date.today(), days)
|
||||
raise ValueError(tag)
|
||||
|
||||
@classmethod
|
||||
def create_tag(
|
||||
cls,
|
||||
tag_type: TagType,
|
||||
date: datetime.date,
|
||||
day: int = 0,
|
||||
days: int = 10,
|
||||
) -> Tag:
|
||||
if tag_type == TagType.DAY:
|
||||
return Tag(tag_type, date + datetime.timedelta(days=day))
|
||||
if tag_type == TagType.DAYS:
|
||||
return Tag(tag_type, date, days)
|
||||
raise ValueError(tag_type)
|
||||
@@ -1,6 +1,7 @@
|
||||
import datetime
|
||||
import typing
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qsl, urlencode, urlsplit
|
||||
|
||||
from babel.dates import format_date
|
||||
from fastapi import Request
|
||||
@@ -10,7 +11,6 @@ from gallery.easel.core import AppRequest
|
||||
from gallery.version import __version__
|
||||
|
||||
from ...translation import _
|
||||
from .tag import TagUtil
|
||||
|
||||
ContextProcessor = typing.Callable[[AppRequest], dict[str, typing.Any]]
|
||||
|
||||
@@ -28,6 +28,26 @@ def base_context_processor(request: Request) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def build_url(
|
||||
request: Request, *, path: list[str] | None = None, up: int | None = None, query: dict | None = None
|
||||
) -> str:
|
||||
parts = urlsplit(str(request.url))
|
||||
if up:
|
||||
path = [".."] * up
|
||||
if path:
|
||||
path_parts = parts.path.split("/")
|
||||
for item in path:
|
||||
if item == "..":
|
||||
path_parts = path_parts[:-1]
|
||||
else:
|
||||
path_parts.append(str(item))
|
||||
parts = parts._replace(path="/".join(path_parts))
|
||||
if query:
|
||||
query_dict = {**dict(parse_qsl(parts.query)), **query}
|
||||
parts = parts._replace(query=urlencode(query_dict))
|
||||
return "/".join([""] + parts.geturl().split("/")[3:])
|
||||
|
||||
|
||||
def build_templates(
|
||||
templates_dir: Path | None = None, filters: dict | None = None, context_processor: ContextProcessor | None = None
|
||||
) -> Jinja2Templates:
|
||||
@@ -44,8 +64,8 @@ def build_templates(
|
||||
"version": __version__,
|
||||
"format_date": format_date,
|
||||
"datetime": datetime,
|
||||
"tag_util": TagUtil,
|
||||
"DATE_FORMAT": "E, d MMMM Y",
|
||||
"build_url": build_url,
|
||||
}
|
||||
)
|
||||
if filters:
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
</div>
|
||||
<h1>{{_("Docs")}}</h1>
|
||||
<div class="list-group mb-3">
|
||||
<app-link href="/docs"
|
||||
<app-link href="/api/docs"
|
||||
target="_blank"
|
||||
icon="file-earmark-text"
|
||||
class="list-group-item list-group-item-action">
|
||||
Swagger UI
|
||||
</app-link>
|
||||
<app-link href="/redoc"
|
||||
<app-link href="/api/redoc"
|
||||
target="_blank"
|
||||
icon="file-earmark-text"
|
||||
class="list-group-item list-group-item-action">
|
||||
|
||||
@@ -1,70 +1 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
from gallery.easel.depends.api import api_resolver
|
||||
from gallery.easel.depends.schedule import ScheduleApiDepends
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
|
||||
from ..common.utils.tag import TagType, TagUtil
|
||||
from ..common.utils.template import build_templates
|
||||
from .filters import timedelta_format
|
||||
|
||||
|
||||
def context_procesor(request: AppRequest) -> dict:
|
||||
return {
|
||||
"providers": request.app.state.api.get_api_providers(ScheduleApi),
|
||||
}
|
||||
|
||||
|
||||
templates = build_templates(
|
||||
Path(__file__).parent / "templates",
|
||||
{
|
||||
"timedelta_format": timedelta_format,
|
||||
},
|
||||
context_procesor,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/schedule")
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def get_schedule_index(request: AppRequest, provider: str | None = None, query: str | None = None):
|
||||
if query and provider:
|
||||
schedule_api = api_resolver(ScheduleApi)(request, provider)
|
||||
channels = await schedule_api.find_channels(query)
|
||||
else:
|
||||
channels = []
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="index.html",
|
||||
context={
|
||||
"channels": channels,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{provider}/{channel}", response_class=RedirectResponse)
|
||||
async def get_channel_default(channel: str):
|
||||
return RedirectResponse(f"{channel}/tag/today")
|
||||
|
||||
|
||||
@router.get("/{provider}/{channel}/tag/{tag}", response_class=HTMLResponse)
|
||||
async def get_channel_tag(request: AppRequest, schedule_api: ScheduleApiDepends, channel: str, tag: str):
|
||||
tag_value = TagUtil.parse_tag(tag)
|
||||
if tag_value.type == TagType.DAY:
|
||||
response = await schedule_api.get_schedule(channel, tag_value.date)
|
||||
else:
|
||||
raise ValueError(tag)
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="channel.html",
|
||||
context={
|
||||
"tag_util": TagUtil,
|
||||
"datetime": datetime,
|
||||
"response": response,
|
||||
},
|
||||
)
|
||||
from .route import router
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
from urllib import parse
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
from gallery.easel.depends.api import api_resolver
|
||||
from gallery.easel.depends.date import DateDepends
|
||||
from gallery.easel.depends.schedule import (
|
||||
GROUP_CANNELS_MAP,
|
||||
ScheduleApiDepends,
|
||||
ScheduleGroupChannelsDepends,
|
||||
)
|
||||
from gallery.sketch.bundle import ApiBundle
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
from gallery.sketch.schedule.model import Schedule
|
||||
|
||||
from ..common.utils.template import build_templates
|
||||
from .filters import timedelta_format
|
||||
|
||||
|
||||
def context_procesor(request: AppRequest) -> dict:
|
||||
return {
|
||||
"providers": request.app.state.api.get_api_providers(ScheduleApi),
|
||||
}
|
||||
|
||||
|
||||
templates = build_templates(
|
||||
Path(__file__).parent / "templates",
|
||||
{
|
||||
"timedelta_format": timedelta_format,
|
||||
},
|
||||
context_procesor,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/schedule")
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def get_schedule_index(request: AppRequest, provider: str | None = None, query: str | None = None):
|
||||
if query and provider:
|
||||
schedule_api = api_resolver(ScheduleApi)(request, provider)
|
||||
channels = await schedule_api.find_channels(query)
|
||||
else:
|
||||
channels = []
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="index.html",
|
||||
context={"channels": [item.model_dump() for item in channels], "groups": GROUP_CANNELS_MAP.keys()},
|
||||
)
|
||||
|
||||
|
||||
async def _get_group_schedule(api: ApiBundle, channels: list[tuple[str, str]], date: datetime.date) -> list[Schedule]:
|
||||
return [
|
||||
await api.get_api(ScheduleApi, provider).get_schedule(channel_id, date) for provider, channel_id in channels
|
||||
]
|
||||
|
||||
|
||||
@router.get("/group/{group_id}", response_class=RedirectResponse)
|
||||
async def get_group_default(group_id: str):
|
||||
return RedirectResponse(f"{parse.quote(group_id)}/today")
|
||||
|
||||
|
||||
@router.get("/group/{group_id}/{date}", response_class=HTMLResponse)
|
||||
async def get_group(request: AppRequest, channels: ScheduleGroupChannelsDepends, date: DateDepends):
|
||||
responses = [
|
||||
await request.app.state.api.get_api(ScheduleApi, channel.provider).get_schedule(channel.id, date)
|
||||
for channel in channels
|
||||
]
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="group.html",
|
||||
context={"responses": responses},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{provider}/{channel}", response_class=RedirectResponse)
|
||||
async def get_channel_default(channel: str):
|
||||
return RedirectResponse(f"{channel}/today")
|
||||
|
||||
|
||||
@router.get("/{provider}/{channel}/{date}", response_class=HTMLResponse)
|
||||
async def get_channel(request: AppRequest, schedule_api: ScheduleApiDepends, channel: str, date: DateDepends):
|
||||
response = await schedule_api.get_schedule(channel, date)
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="channel.html",
|
||||
context={"response": response},
|
||||
)
|
||||
@@ -1,4 +1,7 @@
|
||||
{% extends "base.html" %}
|
||||
{% set live = request.query_params.live == '1' %}
|
||||
{% set date = response.date %}
|
||||
|
||||
{% block title %}
|
||||
{{_("TV program")}} | {{response.channel.name}} | {{format_date(response.date, DATE_FORMAT,
|
||||
locale=request.state.language)}}
|
||||
@@ -10,19 +13,23 @@ locale=request.state.language)}}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h4>
|
||||
<a class="icon-link {{'disabled' if response.date == datetime.date.today() else ''}}"
|
||||
href="../tag/{{tag_util.create_tag('day', response.date, -1)}}">
|
||||
<h4 class="mb-4">
|
||||
<a class="icon-link {{'disabled' if date == datetime.date.today() else ''}}"
|
||||
href="{{build_url(request, path=['..', date - datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-left-square"></i>
|
||||
</a>
|
||||
<a class="icon-link"
|
||||
href="../../..">
|
||||
href="{{build_url(request, up=3)}}">
|
||||
<i class="bi bi-arrow-up-square"></i>
|
||||
</a>
|
||||
<a class="icon-link {{'text-secondary' if not live else ''}}"
|
||||
href="{{build_url(request, query={'live': 0 if live else 1})}}">
|
||||
<i class="bi bi-cast"></i>
|
||||
</a>
|
||||
<span class="badge rounded-pill text-bg-secondary fs-6">{{provider}}</span>
|
||||
<span>{{response.channel.name}} | {{format_date(response.date, DATE_FORMAT, locale=request.state.language)}}</span>
|
||||
<a class="icon-link"
|
||||
href="../tag/{{tag_util.create_tag('day', response.date, 1)}}">
|
||||
href="{{build_url(request, path=['..', date + datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-right-square"></i>
|
||||
</a>
|
||||
</h4>
|
||||
@@ -30,15 +37,14 @@ locale=request.state.language)}}
|
||||
<thead>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td style="width:100%"></td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for value in response.values %}
|
||||
<tr class="{{'table-success' if value.live else ''}}">
|
||||
<tr class="{{'d-none' if (live and not value.live) else 'table-success' if (not live and value.live) else ''}}">
|
||||
<td>
|
||||
<span>{{value.start.strftime('%H:%M')}}</span>
|
||||
<span class="small ms-1">({{(value.end - value.start) | timedelta_format}})</span>
|
||||
<span>{{value.start.strftime('%H:%M')}}</span> <span class="small ms-1">({{(value.end - value.start) | timedelta_format}})</span>
|
||||
</td>
|
||||
<td>{{value.label}}</td>
|
||||
</tr>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
{% extends "base.html" %}
|
||||
{% set live = request.query_params.live == '1' %}
|
||||
{% set date = responses[0].date %}
|
||||
|
||||
{% block title %}
|
||||
{{_("TV program")}}
|
||||
{% endblock %}
|
||||
|
||||
{% block header %}
|
||||
<app-link href="/schedule"
|
||||
icon="tv">{{_("TV program")}}</app-link>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h4 class="mb-4">
|
||||
<a class="icon-link {{'disabled' if date == datetime.date.today() else ''}}"
|
||||
href="{{build_url(request, path=['..', date - datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-left-square"></i>
|
||||
</a>
|
||||
<a class="icon-link"
|
||||
href="{{build_url(request, up=3)}}">
|
||||
<i class="bi bi-arrow-up-square"></i>
|
||||
</a>
|
||||
<a class="icon-link {{'text-secondary' if not live else ''}}"
|
||||
href="{{build_url(request, query={'live': 0 if live else 1})}}">
|
||||
<i class="bi bi-cast"></i>
|
||||
</a>
|
||||
<span>{{format_date(date, DATE_FORMAT, locale=request.state.language)}}</span>
|
||||
<a class="icon-link"
|
||||
href="{{build_url(request, path=['..', date + datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-right-square"></i>
|
||||
</a>
|
||||
</h4>
|
||||
{% for response in responses %}
|
||||
<h5>
|
||||
<span class="badge rounded-pill text-bg-secondary fs-6">{{response.channel.provider}}</span>
|
||||
<span>{{response.channel.name}}</span>
|
||||
</h5>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td style="width:100%"></td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for value in response.values %}
|
||||
<tr class="{{'d-none' if (live and not value.live) else 'table-success' if (not live and value.live) else ''}}">
|
||||
<td>
|
||||
<span>{{value.start.strftime('%H:%M')}}</span> <span class="small ms-1">({{(value.end - value.start) |
|
||||
timedelta_format}})</span>
|
||||
</td>
|
||||
<td>{{value.label}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
@@ -35,17 +35,26 @@
|
||||
</div>
|
||||
</form>
|
||||
{% if channels %}
|
||||
<ul id="channels"
|
||||
class="list-group mb-5">
|
||||
{% for channel in channels %}
|
||||
<schedule-channel channel="{{channel.model_dump() | tojson | forceescape}}"></schedule-channel>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<hr>
|
||||
<div class="mb-4">
|
||||
<schedule-channel-list value="{{channels | tojson | forceescape}}"
|
||||
onclick="scheduleChannelManager.store(element)"
|
||||
onadd="scheduleChannelManager.add(element)"></schedule-channel-list>
|
||||
<hr>
|
||||
</div>
|
||||
{% endif %}
|
||||
<ul id="storedChannels"
|
||||
class="list-group mb-5">
|
||||
</ul>
|
||||
<div class="mb-4">
|
||||
<h3>{{_("Stored channels")}}</h3>
|
||||
<schedule-channel-list id="storedChannels"
|
||||
onremove="scheduleChannelManager.remove(element)"></schedule-channel-list>
|
||||
</div>
|
||||
<div class="mb-4">
|
||||
<h3>{{_("Channel groups")}}</h3>
|
||||
<ul class="list-group">
|
||||
{% for group in groups %}
|
||||
<schedule-channel-group value="{{group | tojson | forceescape}}"></schedule-channel-group>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
<script>
|
||||
(function () {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
@@ -53,9 +62,8 @@
|
||||
if (searchQuery) {
|
||||
document.querySelector('#query').value = searchQuery;
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", (event) => {
|
||||
scheduleChannelManager.loadChannels('#storedChannels');
|
||||
scheduleChannelManager.load();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -1,102 +1 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
from gallery.easel.depends.api import api_resolver
|
||||
from gallery.easel.depends.weather import WeatherApiDepends
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
from gallery.sketch.weather.model import WeatherResponse
|
||||
|
||||
from ..common.utils.tag import TagType, TagUtil
|
||||
from ..common.utils.template import build_templates
|
||||
from .filters import cloudness_icon, weather_icon_svg, wind_direction_icon
|
||||
|
||||
|
||||
def context_procesor(request: AppRequest) -> dict:
|
||||
return {
|
||||
"providers": request.app.state.api.get_api_providers(WeatherApi),
|
||||
}
|
||||
|
||||
|
||||
templates = build_templates(
|
||||
Path(__file__).parent / "templates",
|
||||
{
|
||||
"wind_direction_icon": wind_direction_icon,
|
||||
"cloudness_icon": cloudness_icon,
|
||||
"weather_icon_svg": weather_icon_svg,
|
||||
},
|
||||
context_procesor,
|
||||
)
|
||||
|
||||
|
||||
def build_weather_response(request: AppRequest, response: WeatherResponse):
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="weather.html",
|
||||
context={
|
||||
"response": response,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
router = APIRouter(prefix="/weather")
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def get_weather_index(request: AppRequest, provider: str | None = None, query: str | None = None):
|
||||
if query and provider:
|
||||
weather_api = api_resolver(WeatherApi)(request, provider)
|
||||
locations = await weather_api.find_locations(query)
|
||||
else:
|
||||
locations = []
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="index.html",
|
||||
context={
|
||||
"locations": locations,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}", response_class=RedirectResponse)
|
||||
async def get_weather(location: str):
|
||||
return RedirectResponse(f"{location}/tag/today")
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}/day/{date}", response_class=HTMLResponse)
|
||||
async def get_weather_day(
|
||||
request: AppRequest,
|
||||
weather_api: WeatherApiDepends,
|
||||
location: str,
|
||||
date: datetime.date,
|
||||
):
|
||||
response = await weather_api.get_day(location, date)
|
||||
return build_weather_response(request, response)
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}/days/{days}", response_class=HTMLResponse)
|
||||
async def get_weather_days(request: AppRequest, weather_api: WeatherApiDepends, location: str, days: int):
|
||||
response = await weather_api.get_days(location, days)
|
||||
return build_weather_response(request, response)
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}/tag/{tag}", response_class=HTMLResponse)
|
||||
async def get_weather_tag(request: AppRequest, weather_api: WeatherApiDepends, location: str, tag: str):
|
||||
tag_value = TagUtil.parse_tag(tag)
|
||||
if tag_value.type == TagType.DAY:
|
||||
response = await weather_api.get_day(location, tag_value.date)
|
||||
elif tag_value.type == TagType.DAYS:
|
||||
response = await weather_api.get_days(location, tag_value.days)
|
||||
else:
|
||||
raise ValueError(tag)
|
||||
return build_weather_response(request, response)
|
||||
|
||||
|
||||
@router.get("/zmiyevka-184640/tag/{tag}", response_class=RedirectResponse)
|
||||
async def get_zmiyevka_weather_compat(request: AppRequest):
|
||||
path_parts = request.url.path.split("/")
|
||||
path_parts.insert(2, "gismeteo")
|
||||
return RedirectResponse("/".join(path_parts))
|
||||
from .route import router
|
||||
|
||||
@@ -7,17 +7,15 @@ from gallery.sketch.weather.model import (
|
||||
Precipitation,
|
||||
Sky,
|
||||
WindDirection,
|
||||
WindDirectionDeg,
|
||||
)
|
||||
from gallery.util import root_path
|
||||
|
||||
|
||||
def wind_direction_icon(wind_direction_deg: float) -> str:
|
||||
wind_direction = WindDirectionDeg(wind_direction_deg).direction
|
||||
if wind_direction == WindDirection.CALM:
|
||||
def wind_direction_icon(wind_direction: WindDirection | None) -> str:
|
||||
if wind_direction is None:
|
||||
return "wind-calm"
|
||||
else:
|
||||
return f"wind-from-{wind_direction.name.lower()}"
|
||||
return f"wind-from-{wind_direction.lower()}"
|
||||
|
||||
|
||||
def cloudness_icon(sky: Sky, date: datetime.datetime, period: str) -> list[str]:
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
|
||||
from gallery.easel.core import AppRequest
|
||||
from gallery.easel.depends.api import api_resolver
|
||||
from gallery.easel.depends.date import DateDepends
|
||||
from gallery.easel.depends.weather import WeatherApiDepends
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
from gallery.sketch.weather.model import WeatherResponse
|
||||
|
||||
from ..common.utils.template import build_templates
|
||||
from .filters import cloudness_icon, weather_icon_svg, wind_direction_icon
|
||||
|
||||
|
||||
def context_procesor(request: AppRequest) -> dict:
|
||||
return {
|
||||
"providers": request.app.state.api.get_api_providers(WeatherApi),
|
||||
}
|
||||
|
||||
|
||||
templates = build_templates(
|
||||
Path(__file__).parent / "templates",
|
||||
{
|
||||
"wind_direction_icon": wind_direction_icon,
|
||||
"cloudness_icon": cloudness_icon,
|
||||
"weather_icon_svg": weather_icon_svg,
|
||||
},
|
||||
context_procesor,
|
||||
)
|
||||
|
||||
|
||||
def build_weather_response(request: AppRequest, response: WeatherResponse):
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="weather.html",
|
||||
context={"response": response},
|
||||
)
|
||||
|
||||
|
||||
router = APIRouter(prefix="/weather")
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def get_weather_index(request: AppRequest, provider: str | None = None, query: str | None = None):
|
||||
if query and provider:
|
||||
weather_api = api_resolver(WeatherApi)(request, provider)
|
||||
locations = await weather_api.find_locations(query)
|
||||
else:
|
||||
locations = []
|
||||
return templates.TemplateResponse(
|
||||
request=request,
|
||||
name="index.html",
|
||||
context={"locations": [item.model_dump() for item in locations]},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}", response_class=RedirectResponse)
|
||||
async def get_weather(location: str):
|
||||
return RedirectResponse(f"{location}/day/today")
|
||||
|
||||
|
||||
@router.get("/{provider}/{location}/days/{days}", response_class=HTMLResponse)
|
||||
async def get_weather_days(request: AppRequest, weather_api: WeatherApiDepends, location: str, days: int):
|
||||
response = await weather_api.get_days(location, days)
|
||||
return build_weather_response(request, response)
|
||||
|
||||
|
||||
# TODO: tag mode route deprecated, used in tizen app
|
||||
@router.get("/{provider}/{location}/{mode:str}/{date}", response_class=HTMLResponse)
|
||||
async def get_weather_day(
|
||||
request: AppRequest,
|
||||
weather_api: WeatherApiDepends,
|
||||
location: str,
|
||||
date: DateDepends,
|
||||
mode: Literal["day", "tag"], # pylint:disable=unused-argument
|
||||
):
|
||||
response = await weather_api.get_day(location, date)
|
||||
return build_weather_response(request, response)
|
||||
@@ -35,17 +35,18 @@
|
||||
</div>
|
||||
</form>
|
||||
{% if locations %}
|
||||
<ul id="locations"
|
||||
class="list-group mb-5">
|
||||
{% for location in locations %}
|
||||
<weather-location location="{{location.model_dump() | tojson | forceescape}}"></weather-location>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<hr>
|
||||
<div class="mb-4">
|
||||
<weather-location-list value="{{locations | tojson | forceescape}}"
|
||||
onclick="weatherLocationManager.store(element)"
|
||||
onadd="weatherLocationManager.add(element)"></weather-location-list>
|
||||
<hr>
|
||||
</div>
|
||||
{% endif %}
|
||||
<ul id="storedLocations"
|
||||
class="list-group mb-5">
|
||||
</ul>
|
||||
<div class="mb-4">
|
||||
<h3>{{_("Stored locations")}}</h3>
|
||||
<weather-location-list id="storedLocations"
|
||||
onremove="weatherLocationManager.remove(element)"></weather-location-list>
|
||||
</div>
|
||||
<script>
|
||||
(function () {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
@@ -55,7 +56,7 @@
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", (event) => {
|
||||
weatherLocationManager.loadLocations('#storedLocations');
|
||||
weatherLocationManager.load();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}{{_("Weather")}} | {{response.location}} | {{response.date.strftime('%a, %d %B %Y')}}{% endblock %}
|
||||
{% set date = response.date %}
|
||||
|
||||
{% block title %}{{_("Weather")}} | {{response.location}} | {{format_date(response.date, DATE_FORMAT,
|
||||
locale=request.state.language)}}{% endblock %}
|
||||
|
||||
{% block header %}
|
||||
<app-link href="/weather"
|
||||
@@ -9,18 +12,18 @@
|
||||
{% block content %}
|
||||
<h4>
|
||||
{% if response.period == 'day' %}
|
||||
<a class="icon-link {{'disabled' if response.date == datetime.date.today() else ''}}"
|
||||
href="../tag/{{tag_util.create_tag('day', response.date, -1)}}">
|
||||
<a class="icon-link {{'disabled' if date == datetime.date.today() else ''}}"
|
||||
href="{{build_url(request, path=['..', date - datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-left-square"></i>
|
||||
</a>
|
||||
<a class="icon-link"
|
||||
href="../tag/days-10">
|
||||
href="{{build_url(request, path=['..', '..', 'days', '10'])}}">
|
||||
<i class="bi bi-arrow-up-square"></i>
|
||||
</a>
|
||||
<span class="badge rounded-pill text-bg-secondary fs-6">{{provider}}</span>
|
||||
<span>{{response.location}} | {{format_date(response.date, DATE_FORMAT, locale=request.state.language)}}</span>
|
||||
<a class="icon-link"
|
||||
href="../tag/{{tag_util.create_tag('day', response.date, 1)}}">
|
||||
href="{{build_url(request, path=['..', date + datetime.timedelta(days=1)])}}">
|
||||
<i class="bi bi-arrow-right-square"></i>
|
||||
</a>
|
||||
{% endif %}
|
||||
@@ -47,7 +50,7 @@
|
||||
{% if response.period == 'days' %}
|
||||
<td class="date {{'now' if value.date.date() == datetime.date.today() else ''}}">
|
||||
<span class="value {{'text-danger' if value.date.weekday() in [5,6] else ''}}">
|
||||
<a href="../tag/{{tag_util.create_tag('day', value.date.date())}}">
|
||||
<a href="{{build_url(request, path=['..', '..', 'day', value.date.date()])}}">
|
||||
{{format_date(value.date, 'E d', locale=request.state.language)}}
|
||||
</a>
|
||||
</span>
|
||||
@@ -104,8 +107,8 @@
|
||||
{% for value in response.values %}
|
||||
<td class="wind"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{{ value.wind_direction }}">
|
||||
<div class="wi-svg wi-s wi-{{value.wind_direction | wind_direction_icon}}"
|
||||
data-bs-title="{{ value.wind.direction }}">
|
||||
<div class="wi-svg wi-s wi-{{value.wind.direction | wind_direction_icon}}"
|
||||
style="margin: 0 auto;">{{ 'wind-deg' | weather_icon_svg }}</div>
|
||||
</td>
|
||||
{% endfor %}
|
||||
@@ -120,11 +123,11 @@
|
||||
<tr>
|
||||
{% for value in response.values %}
|
||||
<td class="wind"
|
||||
style="background-color: rgba(128, 128, 128, {{value.wind_speed * 0.05}});">
|
||||
<span class="speed">{{value.wind_speed}}</span>
|
||||
{% if value.wind_gust != value.wind_speed and value.wind_gust > 0 %}
|
||||
style="background-color: rgba(128, 128, 128, {{value.wind.speed * 0.05}});">
|
||||
<span class="speed">{{value.wind.speed}}</span>
|
||||
{% if value.wind.gust != value.wind.speed and value.wind.gust > 0 %}
|
||||
<span class="gust">
|
||||
({{value.wind_gust}})
|
||||
({{value.wind.gust}})
|
||||
</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
|
||||
+6
-4
@@ -5,9 +5,10 @@ import uvicorn
|
||||
|
||||
from gallery.easel import build_app
|
||||
from gallery.painting.gismeteo.api import GismeteoApi
|
||||
from gallery.painting.matchtv.api import MatchTvApi
|
||||
from gallery.painting.matchtv.api import MatchTVApi
|
||||
from gallery.painting.openweather.api import OpenWeatherApi
|
||||
from gallery.painting.yandextv.api import YandexTvApi
|
||||
from gallery.painting.rp5.api import RP5WeatherApi
|
||||
from gallery.painting.yandextv.api import YandexTVApi
|
||||
from gallery.painting.yandexweather.api import YandexWeatherApi
|
||||
from gallery.sketch.bundle import ApiBundle
|
||||
from gallery.sketch.schedule.cached import CachedScheduleApi
|
||||
@@ -15,11 +16,12 @@ from gallery.sketch.weather.cached import CachedWeatherApi
|
||||
|
||||
api = ApiBundle(
|
||||
[
|
||||
CachedScheduleApi(YandexTvApi()),
|
||||
CachedScheduleApi(MatchTvApi()),
|
||||
CachedScheduleApi(YandexTVApi()),
|
||||
CachedScheduleApi(MatchTVApi()),
|
||||
CachedWeatherApi(GismeteoApi()),
|
||||
CachedWeatherApi(OpenWeatherApi()),
|
||||
CachedWeatherApi(YandexWeatherApi()),
|
||||
CachedWeatherApi(RP5WeatherApi()),
|
||||
]
|
||||
)
|
||||
app = build_app(api)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "gismeteo"
|
||||
|
||||
@@ -1,25 +1,32 @@
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
from os import environ
|
||||
from typing import Any
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from gallery.sketch.source import ApiSource
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
from gallery.sketch.weather.model import Location, WeatherResponse, WeatherValue
|
||||
from gallery.sketch.weather.util import merge_weather_values
|
||||
from gallery.sketch.weather.model import Location, WeatherResponse
|
||||
|
||||
from . import datehelp
|
||||
from .parser import DAYS_PARSER, LOCATION_PARSER, ONE_DAY_PARSER, ROW_PARSERS
|
||||
from . import PROVIDER
|
||||
from .parser import ForecastData, GismeteoForecastParser, GismeteoLocationParser
|
||||
|
||||
logger = logging.getLogger("gismeteo")
|
||||
|
||||
def format_date(date: datetime.date) -> str:
|
||||
today = datetime.date.today()
|
||||
days = (date - today).days
|
||||
if days == 0:
|
||||
return "today"
|
||||
elif days == 1:
|
||||
return "tomorrow"
|
||||
else:
|
||||
return f"{days + 1}-day"
|
||||
|
||||
|
||||
class GismeteoApi(WeatherApi):
|
||||
PROVIDER = "gismeteo"
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
forecast_parser = GismeteoForecastParser()
|
||||
location_parser = GismeteoLocationParser()
|
||||
|
||||
def _build_source(self) -> ApiSource:
|
||||
return ApiSource(
|
||||
@@ -27,79 +34,14 @@ class GismeteoApi(WeatherApi):
|
||||
cookies={"xauth": auth} if (auth := environ.get("GISMETEO_AUTH")) else None,
|
||||
)
|
||||
|
||||
def _parse_oneday(self, date: datetime.date, data: str) -> WeatherResponse:
|
||||
result: list[dict[str, Any]] = []
|
||||
soup = BeautifulSoup(data, features="html.parser")
|
||||
location = LOCATION_PARSER.parse_location(data)
|
||||
widget = ONE_DAY_PARSER.parse_widget(soup)
|
||||
for parser in ROW_PARSERS:
|
||||
for index, value in enumerate(parser.parse_row(widget)):
|
||||
while len(result) < index + 1:
|
||||
result.append({})
|
||||
result[index][parser.KEY] = value
|
||||
values = [WeatherValue(**item) for item in result]
|
||||
# group values by 3 hours
|
||||
if len(values) > 8:
|
||||
n = 3
|
||||
values = [
|
||||
merge_weather_values(values[i].date, values[i : i + n], "average") for i in range(0, len(values), n)
|
||||
]
|
||||
return WeatherResponse(
|
||||
location=location or "n/a",
|
||||
date=date,
|
||||
period="day",
|
||||
values=values,
|
||||
)
|
||||
|
||||
def _parse_manydays(self, data: str) -> WeatherResponse:
|
||||
result: list[dict[str, Any]] = []
|
||||
soup = BeautifulSoup(data, features="html.parser")
|
||||
location = LOCATION_PARSER.parse_location(data)
|
||||
widget = DAYS_PARSER.parse_widget(soup)
|
||||
for parser in ROW_PARSERS:
|
||||
for index, value in enumerate(parser.parse_row(widget)):
|
||||
while len(result) < index + 1:
|
||||
result.append({})
|
||||
result[index][parser.KEY] = value
|
||||
values = [WeatherValue(**item) for item in result]
|
||||
return WeatherResponse(
|
||||
location=location or "n/a",
|
||||
date=datetime.date.today(),
|
||||
period="days",
|
||||
values=values,
|
||||
)
|
||||
|
||||
async def find_locations(self, query: str) -> list[Location]:
|
||||
geo = "ru"
|
||||
latitude = 52.968498
|
||||
longitude = 36.0695
|
||||
data = json.loads(
|
||||
await self.source.request(
|
||||
data = await self.source.request(
|
||||
f"mq/city/q/?q={query}&geo={geo}&latitude={latitude}&longitude={longitude}&limit=10"
|
||||
)
|
||||
)
|
||||
result = []
|
||||
for item in data["data"]:
|
||||
result.append(
|
||||
Location(
|
||||
id=f"{item['slug']}-{item['id']}",
|
||||
name=item["translations"]["kk"]["city"]["name"],
|
||||
provider=self.provider,
|
||||
lat=item["coordinates"]["latitude"],
|
||||
lon=item["coordinates"]["longitude"],
|
||||
country=item["translations"]["kk"]["country"]["name"],
|
||||
country_code=item["country"]["code"].lower(),
|
||||
district=(
|
||||
item["translations"]["kk"]["district"]["name"] if item["translations"]["kk"]["district"] else ""
|
||||
),
|
||||
subdistrict=(
|
||||
item["translations"]["kk"]["subdistrict"]["name"]
|
||||
if "subdistrict" in item["translations"]["kk"]
|
||||
else ""
|
||||
),
|
||||
)
|
||||
)
|
||||
return result
|
||||
return self.location_parser.parse(data)
|
||||
|
||||
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
||||
max_date = datetime.date.today() + datetime.timedelta(days=9)
|
||||
@@ -107,9 +49,11 @@ class GismeteoApi(WeatherApi):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail={"max_date": max_date.strftime("%Y-%m-%d")}
|
||||
)
|
||||
data = await self.source.request(f"weather-{location_id}/{datehelp.dump(date)}")
|
||||
return self._parse_oneday(date, data)
|
||||
data = await self.source.request(f"weather-{location_id}/{format_date(date)}")
|
||||
context = ForecastData(date, "day", data, 8)
|
||||
return self.forecast_parser.parse(context)
|
||||
|
||||
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
|
||||
data = await self.source.request(f"weather-{location_id}/{days}-days")
|
||||
return self._parse_manydays(data)
|
||||
context = ForecastData(datetime.date.today(), "days", data)
|
||||
return self.forecast_parser.parse(context)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
from typing import Generic, Iterable, Optional, TypeVar
|
||||
|
||||
from bs4 import Tag
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class WidgetParser:
|
||||
def parse_widget(self, tag: Tag) -> Tag:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class BaseWidgetParser(WidgetParser):
|
||||
SELECT: str
|
||||
|
||||
def __init__(self, select: Optional[str] = None):
|
||||
super().__init__()
|
||||
self._select = select or self.SELECT
|
||||
|
||||
def parse_widget(self, tag: Tag) -> Tag:
|
||||
widget = tag.select_one(self._select)
|
||||
if widget is None:
|
||||
raise ValueError(self._select)
|
||||
return widget
|
||||
|
||||
|
||||
class RowParser(Generic[T]):
|
||||
KEY: str
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[T]:
|
||||
raise NotImplementedError
|
||||
@@ -1,39 +0,0 @@
|
||||
import datetime
|
||||
from enum import Enum
|
||||
|
||||
import dateparser
|
||||
import dateparser.date_parser
|
||||
|
||||
|
||||
class Day(str, Enum):
|
||||
TODAY = "today"
|
||||
TOMORROW = "tomorrow"
|
||||
DAY_3 = "3-day"
|
||||
DAY_4 = "4-day"
|
||||
DAY_5 = "5-day"
|
||||
|
||||
|
||||
def parse(value: str) -> datetime.date:
|
||||
if value in ["today", "mock"]:
|
||||
return datetime.date.today()
|
||||
elif value == "tomorrow":
|
||||
return datetime.date.today() + datetime.timedelta(days=1)
|
||||
elif value.endswith("-day"):
|
||||
days = int(value.split("-")[0]) - 1
|
||||
return datetime.date.today() + datetime.timedelta(days=days)
|
||||
else:
|
||||
date = dateparser.parse(value)
|
||||
if date is None:
|
||||
raise ValueError(value)
|
||||
return date.date()
|
||||
|
||||
|
||||
def dump(date: datetime.date) -> str:
|
||||
today = datetime.date.today()
|
||||
days = (date - today).days
|
||||
if days == 0:
|
||||
return "today"
|
||||
elif days == 1:
|
||||
return "tomorrow"
|
||||
else:
|
||||
return f"{days + 1}-day"
|
||||
@@ -1,65 +1,62 @@
|
||||
import datetime
|
||||
import logging
|
||||
import re
|
||||
from typing import Iterable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal
|
||||
|
||||
import dateparser
|
||||
from bs4 import Tag
|
||||
|
||||
from gallery.sketch.parse.core import ParsedContext, ParseError, Parser
|
||||
from gallery.sketch.parse.date import DATE_PARSER
|
||||
from gallery.sketch.parse.html import HTMLContextParser, TableParser, TableValueParser
|
||||
from gallery.sketch.parse.json import JSONParser
|
||||
from gallery.sketch.weather.model import (
|
||||
Cloudness,
|
||||
Location,
|
||||
Precipitation,
|
||||
Sky,
|
||||
WeatherResponse,
|
||||
WeatherValue,
|
||||
WindDirection,
|
||||
)
|
||||
from gallery.sketch.weather.parser import ParseError, parse_wind_direction
|
||||
from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER
|
||||
from gallery.sketch.weather.util import fix_dates_sequence, merge_weather_values
|
||||
|
||||
from .core import BaseWidgetParser, RowParser
|
||||
from . import PROVIDER
|
||||
|
||||
logger = logging.getLogger("gismeteo")
|
||||
|
||||
ONE_DAY_PARSER = BaseWidgetParser(".widget.widget-oneday .widget-items")
|
||||
DAYS_PARSER = BaseWidgetParser(".widget.widget-days .widget-items")
|
||||
logger = logging.getLogger(PROVIDER)
|
||||
|
||||
|
||||
class LocationParser:
|
||||
class LocationNameParser(Parser[str, str]):
|
||||
PATTERN = re.compile('{"ru":{"city":{"name":"(.*?)"')
|
||||
|
||||
def parse_location(self, data: str) -> str | None:
|
||||
def parse(self, data: str) -> str:
|
||||
match = self.PATTERN.search(data)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
raise ParseError("Location not found")
|
||||
|
||||
|
||||
LOCATION_PARSER = LocationParser()
|
||||
class TimeParser(TableValueParser[datetime.datetime]):
|
||||
selector = ".widget-row.widget-row-datetime-time > .row-item > time-value"
|
||||
model_key = "date"
|
||||
|
||||
def extract(self, data: Tag) -> datetime.datetime:
|
||||
timestamp = int(data.attrs["timestamp"])
|
||||
return datetime.datetime.fromtimestamp(timestamp)
|
||||
|
||||
|
||||
class DateParser(RowParser[datetime.datetime]):
|
||||
KEY = "date"
|
||||
class DateParser(TableValueParser[datetime.datetime]):
|
||||
selector = ".widget-row.widget-row-date > .row-item"
|
||||
model_key = "date"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[datetime.datetime]:
|
||||
datetime_time_row = tag.select_one(".widget-row.widget-row-datetime-time")
|
||||
if datetime_time_row:
|
||||
for item in datetime_time_row.select(".row-item > time-value"):
|
||||
timestamp = int(item.attrs["timestamp"])
|
||||
time = datetime.datetime.fromtimestamp(timestamp)
|
||||
yield time
|
||||
else:
|
||||
month = None
|
||||
for item in tag.select(".widget-row.widget-row-date > .row-item"):
|
||||
date_str = item.text
|
||||
date = dateparser.parse(date_str, languages=["ru"])
|
||||
if date is None:
|
||||
raise ParseError.invalid_date(date_str)
|
||||
if month and month > date.month:
|
||||
date = date.replace(month=month)
|
||||
month = date.month
|
||||
yield date
|
||||
def extract(self, data: Tag) -> datetime.datetime:
|
||||
return DATE_PARSER.parse(data.text)
|
||||
|
||||
|
||||
class SkyParser(RowParser[Sky]):
|
||||
KEY = "sky"
|
||||
class SkyParser(TableValueParser[Sky]):
|
||||
selector = ".widget-row[data-row=icon-tooltip] > .row-item"
|
||||
model_key = "sky"
|
||||
|
||||
CLOUDNESS_MAP: dict[str, Cloudness] = {
|
||||
"ясно": Cloudness.CLEAR,
|
||||
@@ -97,9 +94,8 @@ class SkyParser(RowParser[Sky]):
|
||||
result_values = values - flag_values
|
||||
return result_values, len(result_values) < len(values)
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[Sky]:
|
||||
for item in tag.select(".widget-row[data-row=icon-tooltip] > .row-item"):
|
||||
sky_str = item.attrs["data-tooltip"]
|
||||
def extract(self, data: Tag) -> Sky:
|
||||
sky_str = data.attrs["data-tooltip"]
|
||||
values = {item.strip().lower() for item in sky_str.split(",")}
|
||||
cloudness = Cloudness.CLEAR
|
||||
precipitation = Precipitation.NO
|
||||
@@ -117,7 +113,7 @@ class SkyParser(RowParser[Sky]):
|
||||
break
|
||||
if values:
|
||||
logger.warning("unknown sky values: %s:", values)
|
||||
yield Sky(
|
||||
return Sky(
|
||||
cloudness=cloudness,
|
||||
precipitation=precipitation,
|
||||
thunder=thunder,
|
||||
@@ -125,71 +121,69 @@ class SkyParser(RowParser[Sky]):
|
||||
)
|
||||
|
||||
|
||||
class TemperatureParser(RowParser[list[int]]):
|
||||
KEY = "temperature"
|
||||
class TemperatureParser(TableValueParser[list[int]]):
|
||||
selector = ".widget-row-chart[data-row=temperature-air] > .chart > .values > .value"
|
||||
model_key = "temperature"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[list[int]]:
|
||||
for item in tag.select(".widget-row-chart[data-row=temperature-air] > .chart > .values > .value"):
|
||||
yield [int(value.attrs["value"]) for value in item.select("temperature-value")]
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(value.attrs["value"]) for value in data.select("temperature-value")]
|
||||
|
||||
|
||||
class WindSpeedParser(RowParser[int]):
|
||||
KEY = "wind_speed"
|
||||
class WindSpeedParser(TableValueParser[int]):
|
||||
selector = ".widget-row-wind > .row-item > .wind-speed"
|
||||
model_key = "wind.speed"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[int]:
|
||||
for item in tag.select(".widget-row-wind > .row-item > .wind-speed > speed-value"):
|
||||
yield int(item.attrs["value"])
|
||||
def extract(self, data: Tag) -> int:
|
||||
value = data.select_one("speed-value")
|
||||
return int(value.attrs["value"]) if value else 0
|
||||
|
||||
|
||||
class WindGustParser(RowParser[int]):
|
||||
KEY = "wind_gust"
|
||||
class WindGustParser(TableValueParser[int]):
|
||||
selector = ".widget-row-wind > .row-item > .wind-gust"
|
||||
model_key = "wind.gust"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[int]:
|
||||
for item in tag.select(".widget-row-wind > .row-item > .wind-gust"):
|
||||
value = item.select_one("speed-value")
|
||||
yield int(value.attrs["value"]) if value else 0
|
||||
def extract(self, data: Tag) -> int:
|
||||
value = data.select_one("speed-value")
|
||||
return int(value.attrs["value"]) if value else 0
|
||||
|
||||
|
||||
class WindDirectionParser(RowParser[WindDirection]):
|
||||
KEY = "wind_direction"
|
||||
class WindDirectionParser(TableValueParser[WindDirection | None]):
|
||||
selector = ".widget-row-wind > .row-item > .wind-speed > .wind-direction"
|
||||
model_key = "wind.direction"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[float]:
|
||||
for item in tag.select(".widget-row-wind > .row-item > .wind-speed > .wind-direction"):
|
||||
wind_direction_str = item.text.lower().strip()
|
||||
yield parse_wind_direction(wind_direction_str)
|
||||
def extract(self, data: Tag) -> WindDirection | None:
|
||||
return WIND_DIRECTION_PARSER.parse(data.text.strip())
|
||||
|
||||
|
||||
class PrecipitationParser(RowParser[float]):
|
||||
KEY = "precipitation"
|
||||
class PrecipitationParser(TableValueParser[float]):
|
||||
selector = ".widget-row[data-row=precipitation-bars] > .row-item"
|
||||
model_key = "precipitation"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[float]:
|
||||
for item in tag.select(".widget-row[data-row=precipitation-bars] > .row-item"):
|
||||
value = item.select_one("precipitation-value")
|
||||
if value:
|
||||
yield float(value.attrs["value"])
|
||||
else:
|
||||
yield 0
|
||||
def extract(self, data: Tag) -> float:
|
||||
value = data.select_one("precipitation-value")
|
||||
return float(value.attrs["value"]) if value else 0
|
||||
|
||||
|
||||
class PressureParser(RowParser[list[int]]):
|
||||
KEY = "pressure"
|
||||
class PressureParser(TableValueParser[list[int]]):
|
||||
selector = ".widget-row-chart[data-row=pressure] > .chart > .values > .value"
|
||||
model_key = "pressure"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[list[int]]:
|
||||
for item in tag.select(".widget-row-chart[data-row=pressure] > .chart > .values > .value"):
|
||||
yield [int(value.attrs["value"]) for value in item.select("pressure-value")]
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(value.attrs["value"]) for value in data.select("pressure-value")]
|
||||
|
||||
|
||||
class HumidityParser(RowParser[int]):
|
||||
KEY = "humidity"
|
||||
class HumidityParser(TableValueParser[int]):
|
||||
selector = ".widget-row[data-row=humidity] > .row-item, .widget-row[data-row=humidity-avg] > .row-item"
|
||||
model_key = "humidity"
|
||||
|
||||
def parse_row(self, tag: Tag) -> Iterable[int]:
|
||||
for item in tag.select(
|
||||
".widget-row[data-row=humidity] > .row-item, .widget-row[data-row=humidity-avg] > .row-item"
|
||||
):
|
||||
yield int(item.text)
|
||||
def extract(self, data: Tag) -> int:
|
||||
return int(data.text)
|
||||
|
||||
|
||||
ROW_PARSERS: list[RowParser] = [
|
||||
class GismeteoTableParser(TableParser[WeatherValue]):
|
||||
selector = ".widget .widget-items"
|
||||
value_parsers = [
|
||||
TimeParser(),
|
||||
DateParser(),
|
||||
SkyParser(),
|
||||
TemperatureParser(),
|
||||
@@ -199,6 +193,64 @@ ROW_PARSERS: list[RowParser] = [
|
||||
PrecipitationParser(),
|
||||
PressureParser(),
|
||||
HumidityParser(),
|
||||
]
|
||||
]
|
||||
|
||||
ROW_PARSERS_MAP: dict[str, RowParser] = {parser.KEY: parser for parser in ROW_PARSERS}
|
||||
def create_model(self, data: dict) -> WeatherValue:
|
||||
return WeatherValue.model_validate(data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ForecastData:
|
||||
date: datetime.date
|
||||
period: Literal["day", "days"]
|
||||
data: str
|
||||
values: int | None = None
|
||||
|
||||
|
||||
class GismeteoForecastParser(HTMLContextParser[ForecastData, WeatherResponse]):
|
||||
location_parser = LocationNameParser()
|
||||
table_parser = GismeteoTableParser()
|
||||
|
||||
def extract(self, data: ParsedContext[ForecastData, Tag]) -> WeatherResponse:
|
||||
location = self.location_parser.parse(data.context.data)
|
||||
values = self.table_parser.parse(data.data)
|
||||
if data.context.values and len(values) > data.context.values:
|
||||
n = int(len((values)) / data.context.values)
|
||||
values = [
|
||||
merge_weather_values(values[i].date, values[i : i + n], "average") for i in range(0, len(values), n)
|
||||
]
|
||||
if data.context.period == "days":
|
||||
fix_dates_sequence(values)
|
||||
return WeatherResponse(
|
||||
location=location,
|
||||
date=data.context.date,
|
||||
period=data.context.period,
|
||||
values=values,
|
||||
)
|
||||
|
||||
|
||||
class GismeteoLocationParser(JSONParser[list[Location]]):
|
||||
|
||||
def extract(self, data: Any) -> list[Location]:
|
||||
result = []
|
||||
for item in data["data"]:
|
||||
result.append(
|
||||
Location(
|
||||
id=f"{item['slug']}-{item['id']}",
|
||||
name=item["translations"]["kk"]["city"]["name"],
|
||||
provider=PROVIDER,
|
||||
lat=item["coordinates"]["latitude"],
|
||||
lon=item["coordinates"]["longitude"],
|
||||
country=item["translations"]["kk"]["country"]["name"],
|
||||
country_code=item["country"]["code"].lower(),
|
||||
district=(
|
||||
item["translations"]["kk"]["district"]["name"] if item["translations"]["kk"]["district"] else ""
|
||||
),
|
||||
subdistrict=(
|
||||
item["translations"]["kk"]["subdistrict"]["name"]
|
||||
if "subdistrict" in item["translations"]["kk"]
|
||||
else ""
|
||||
),
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "matchtv"
|
||||
|
||||
@@ -1,57 +1,33 @@
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
|
||||
from gallery.sketch.schedule.model import Channel, Schedule
|
||||
from gallery.sketch.source import ApiSource
|
||||
|
||||
logger = logging.getLogger("matchtv")
|
||||
from . import PROVIDER
|
||||
from .parser import (
|
||||
ChannelData,
|
||||
MatchTVChannelParser,
|
||||
MatchTVScheduleParser,
|
||||
ScheduleData,
|
||||
)
|
||||
|
||||
|
||||
class MatchTvApi(ScheduleApi):
|
||||
PROVIDER = "matchtv"
|
||||
class MatchTVApi(ScheduleApi):
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
schedule_parser = MatchTVScheduleParser()
|
||||
channel_parser = MatchTVChannelParser()
|
||||
|
||||
def _build_source(self) -> ApiSource:
|
||||
return ApiSource("https://matchtv.ru")
|
||||
|
||||
async def find_channels(self, query: str) -> list[Channel]:
|
||||
endpoint = "api/v1/channels"
|
||||
data = json.loads(await self.source.request(endpoint))
|
||||
result = []
|
||||
query = query.lower()
|
||||
for item in data["result"]:
|
||||
if query in item["name"].lower() or query in item["alias"]:
|
||||
channel_id = item["alias"]
|
||||
name = item["name"].split("|")[0].strip()
|
||||
result.append(Channel(id=channel_id, name=name, provider=self.provider))
|
||||
return result
|
||||
data = await self.source.request(endpoint)
|
||||
return self.channel_parser.parse(ChannelData(query, data))
|
||||
|
||||
async def get_schedule(self, channel_id: str, date: datetime.date) -> Schedule:
|
||||
endpoint = f"api/v1/channels/{channel_id}/tv-schedule?date={date:%Y%m%d}"
|
||||
data = json.loads(await self.source.request(endpoint))
|
||||
channel_data = data["result"]["channels"][0]
|
||||
channel = Channel(id=channel_data["alias"], name=channel_data["name"], provider=self.provider)
|
||||
values = []
|
||||
current_day = datetime.datetime.combine(date.today(), datetime.datetime.min.time())
|
||||
end = current_day + datetime.timedelta(days=1, hours=6)
|
||||
prev_value: ScheduleValue | None = None
|
||||
for item in channel_data["schedule"]:
|
||||
title = item["title"]
|
||||
time_str = item["time"]
|
||||
hours, minutes = map(int, time_str.split(":"))
|
||||
item_date = current_day.replace(hour=hours, minute=minutes)
|
||||
if prev_value is not None and item_date.hour < prev_value.start.hour:
|
||||
current_day += datetime.timedelta(days=1)
|
||||
item_date += datetime.timedelta(days=1)
|
||||
live = "Прямая трансляция" in title
|
||||
value = ScheduleValue(start=item_date, end=end, label=title, live=live)
|
||||
values.append(value)
|
||||
if prev_value is not None:
|
||||
prev_value.end = item_date
|
||||
prev_value = value
|
||||
return Schedule(
|
||||
channel=channel,
|
||||
date=date,
|
||||
values=values,
|
||||
)
|
||||
data = await self.source.request(endpoint)
|
||||
return self.schedule_parser.parse(ScheduleData(date, data))
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import datetime
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from gallery.sketch.parse.core import ParsedContext, Parser
|
||||
from gallery.sketch.parse.json import JSONContextParser
|
||||
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
|
||||
|
||||
from . import PROVIDER
|
||||
|
||||
|
||||
class DateParser(Parser[str, datetime.date]):
|
||||
def parse(self, data: str) -> datetime.date:
|
||||
matched = re.compile('{\\"date\\":(\\d+)}').search(data)
|
||||
return datetime.date.strptime(matched.group(1), data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScheduleData:
|
||||
date: datetime.date
|
||||
data: str
|
||||
|
||||
|
||||
class MatchTVScheduleParser(JSONContextParser[ScheduleData, Schedule]):
|
||||
date_parser = DateParser()
|
||||
|
||||
def extract(self, data: ParsedContext[ScheduleData, Any]) -> Schedule:
|
||||
channel_data = data.data["result"]["channels"][0]
|
||||
channel = Channel(id=channel_data["alias"], name=channel_data["name"], provider=PROVIDER)
|
||||
values = []
|
||||
start = datetime.datetime.combine(data.context.date, datetime.time.min)
|
||||
end = start + datetime.timedelta(days=1, hours=6)
|
||||
prev_value: ScheduleValue | None = None
|
||||
for item in channel_data["schedule"]:
|
||||
title = item["title"]
|
||||
time_str = item["time"]
|
||||
hours, minutes = map(int, time_str.split(":"))
|
||||
item_date = start.replace(hour=hours, minute=minutes)
|
||||
if prev_value is not None and item_date.hour < prev_value.start.hour:
|
||||
start += datetime.timedelta(days=1)
|
||||
item_date += datetime.timedelta(days=1)
|
||||
live = "Прямая трансляция" in title
|
||||
value = ScheduleValue(start=item_date, end=end, label=title, live=live)
|
||||
values.append(value)
|
||||
if prev_value is not None:
|
||||
prev_value.end = item_date
|
||||
prev_value = value
|
||||
return Schedule(
|
||||
channel=channel,
|
||||
date=data.context.date,
|
||||
values=values,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChannelData:
|
||||
query: str
|
||||
data: str
|
||||
|
||||
|
||||
class MatchTVChannelParser(JSONContextParser[ChannelData, list[Channel]]):
|
||||
def extract(self, data: ParsedContext[ChannelData, Any]) -> list[Channel]:
|
||||
result = []
|
||||
query = data.context.query.lower()
|
||||
for item in data.data["result"]:
|
||||
if query in item["name"].lower() or query in item["alias"]:
|
||||
channel_id = item["alias"]
|
||||
name = item["name"].split("|")[0].strip()
|
||||
result.append(Channel(id=channel_id, name=name, provider=PROVIDER))
|
||||
return result
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "openweather"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import datetime
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from itertools import groupby
|
||||
from os import environ
|
||||
|
||||
from aiocache import cached
|
||||
@@ -10,16 +9,17 @@ from gallery.sketch.weather.model import Location, WeatherResponse, WeatherValue
|
||||
from gallery.sketch.weather.util import merge_weather_values
|
||||
from gallery.util import TimeUnit
|
||||
|
||||
from .openweather import Forecast
|
||||
from . import PROVIDER
|
||||
from .openweather import Location as OpenWeatherLocation
|
||||
from .openweather import OpenWeather
|
||||
from .parser import FORECAST_ITEM_PARSER
|
||||
|
||||
logger = logging.getLogger("openweather")
|
||||
from .parser import OpenWeatherForecastParser, OpenWeatherLocationParser
|
||||
|
||||
|
||||
class OpenWeatherApi(WeatherApi[OpenWeather]):
|
||||
PROVIDER = "openweather"
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
forecast_parser = OpenWeatherForecastParser()
|
||||
location_parser = OpenWeatherLocationParser()
|
||||
|
||||
def _build_source(self) -> OpenWeather:
|
||||
return OpenWeather(environ["OPENWEATHER_KEY"])
|
||||
@@ -41,34 +41,17 @@ class OpenWeatherApi(WeatherApi[OpenWeather]):
|
||||
alias="redis",
|
||||
ttl=TimeUnit.HOUR,
|
||||
)
|
||||
async def _get_location_forecast(self, location_id: str) -> Forecast:
|
||||
return await self.source.get_forecast(*self._parse_location(location_id))
|
||||
async def _get_location_forecast(self, location_id: str) -> list[WeatherValue]:
|
||||
content = await self.source.get_forecast(*self._parse_location(location_id))
|
||||
return self.forecast_parser.parse(content)
|
||||
|
||||
async def find_locations(self, query: str) -> list[Location]:
|
||||
result = await self.source.find_locations(query)
|
||||
return [
|
||||
Location(
|
||||
id=f"{item.lat}:{item.lon}",
|
||||
name=item.name,
|
||||
provider=self.provider,
|
||||
lat=item.lat,
|
||||
lon=item.lon,
|
||||
country=item.country,
|
||||
country_code=item.country.lower(),
|
||||
district=item.state or "",
|
||||
subdistrict="",
|
||||
)
|
||||
for item in result
|
||||
]
|
||||
content = await self.source.find_locations(query)
|
||||
return self.location_parser.parse(content)
|
||||
|
||||
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
||||
location: OpenWeatherLocation = await self._get_location(location_id)
|
||||
data: Forecast = await self._get_location_forecast(location_id)
|
||||
values = []
|
||||
for item in data.list:
|
||||
value = FORECAST_ITEM_PARSER.parse(item)
|
||||
if value.date.date() == date:
|
||||
values.append(value)
|
||||
values = [value for value in await self._get_location_forecast(location_id) if value.date.date() == date]
|
||||
return WeatherResponse(
|
||||
location=location.name,
|
||||
date=date,
|
||||
@@ -78,13 +61,13 @@ class OpenWeatherApi(WeatherApi[OpenWeather]):
|
||||
|
||||
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
|
||||
location: OpenWeatherLocation = await self._get_location(location_id)
|
||||
data: Forecast = await self._get_location_forecast(location_id)
|
||||
values_by_date: dict[datetime.datetime, list[WeatherValue]] = defaultdict(list)
|
||||
for item in data.list:
|
||||
value = FORECAST_ITEM_PARSER.parse(item)
|
||||
item_date = value.date.replace(hour=0, minute=0)
|
||||
values_by_date[item_date].append(value)
|
||||
values = [merge_weather_values(date, values) for date, values in values_by_date.items()]
|
||||
values = [
|
||||
merge_weather_values(datetime.datetime.combine(k, datetime.datetime.min.time()), list(g))
|
||||
for k, g in groupby(
|
||||
sorted(await self._get_location_forecast(location_id), key=lambda item: item.date.date()),
|
||||
lambda item: item.date.date(),
|
||||
)
|
||||
]
|
||||
return WeatherResponse(
|
||||
location=location.name,
|
||||
date=datetime.date.today(),
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import datetime
|
||||
|
||||
from gallery.sketch.weather.model import Cloudness, Precipitation, WeatherValue
|
||||
from gallery.sketch.weather.util import build_weather_value
|
||||
from gallery.sketch.parse.core import Parser
|
||||
from gallery.sketch.weather.model import (
|
||||
Cloudness,
|
||||
Location,
|
||||
Precipitation,
|
||||
WeatherValue,
|
||||
)
|
||||
from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER
|
||||
from gallery.sketch.weather.util import convert_hPa_to_mmHg
|
||||
|
||||
from .openweather import ForecastItem
|
||||
from .openweather import Forecast, ForecastItem
|
||||
from .openweather import Location as OpenWeatherLocation
|
||||
|
||||
|
||||
class ForecastItemParser:
|
||||
class ForecastItemParser(Parser[ForecastItem, list[WeatherValue]]):
|
||||
CLOUDNESS_MAP: dict[str, Cloudness] = {
|
||||
"clear sky": Cloudness.CLEAR,
|
||||
"few clouds": Cloudness.PARTLY_CLOUDY,
|
||||
@@ -22,23 +30,54 @@ class ForecastItemParser:
|
||||
"heavy rain": Precipitation.SHOWER,
|
||||
}
|
||||
|
||||
def parse(self, item: ForecastItem) -> WeatherValue:
|
||||
item_date = datetime.datetime.fromtimestamp(item.dt, datetime.UTC)
|
||||
item_date = item_date.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None).replace(tzinfo=None)
|
||||
value = build_weather_value(item_date)
|
||||
# TODO parse temperature interval flag
|
||||
value.temperature = [round(item.main.temp)]
|
||||
# value.temperature = [round(item.main.temp_max), round(item.main.temp_min)]
|
||||
value.pressure = [round(item.main.pressure * 0.75006)]
|
||||
value.humidity = item.main.humidity
|
||||
value.wind_speed = round(item.wind.speed)
|
||||
value.wind_gust = round(item.wind.gust)
|
||||
value.wind_direction = item.wind.deg
|
||||
value.sky.cloudness = self.CLOUDNESS_MAP.get(item.weather[0].description, Cloudness.CLEAR)
|
||||
value.sky.precipitation = self.PRECIPITATION_MAP.get(item.weather[0].description, Precipitation.NO)
|
||||
if item.rain:
|
||||
value.precipitation = round(item.rain.interval_3h, 1)
|
||||
return value
|
||||
def parse(self, data: ForecastItem) -> WeatherValue:
|
||||
date = (
|
||||
datetime.datetime.fromtimestamp(data.dt, datetime.UTC)
|
||||
.replace(tzinfo=datetime.timezone.utc)
|
||||
.astimezone(tz=None)
|
||||
.replace(tzinfo=None)
|
||||
)
|
||||
return WeatherValue.model_validate(
|
||||
{
|
||||
"date": date,
|
||||
"sky": {
|
||||
"cloudness": self.CLOUDNESS_MAP.get(data.weather[0].description, Cloudness.CLEAR),
|
||||
"precipitation": self.PRECIPITATION_MAP.get(data.weather[0].description, Precipitation.NO),
|
||||
},
|
||||
"wind": {
|
||||
"speed": round(data.wind.speed),
|
||||
"gust": round(data.wind.gust),
|
||||
"direction": WIND_DIRECTION_PARSER.parse(data.wind.deg),
|
||||
},
|
||||
"temperature": [round(data.main.temp)], # [round(data.main.temp_max), round(data.main.temp_min)]
|
||||
"precipitation": round(data.rain.interval_3h, 1) if data.rain else 0,
|
||||
"pressure": [convert_hPa_to_mmHg(data.main.pressure)],
|
||||
"humidity": data.main.humidity,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
FORECAST_ITEM_PARSER = ForecastItemParser()
|
||||
class OpenWeatherForecastParser(Parser[Forecast, list[WeatherValue]]):
|
||||
item_parser = ForecastItemParser()
|
||||
|
||||
def parse(self, data: Forecast) -> list[WeatherValue]:
|
||||
return [self.item_parser.parse(item) for item in data.list]
|
||||
|
||||
|
||||
class OpenWeatherLocationParser(Parser[list[OpenWeatherLocation], list[Location]]):
|
||||
|
||||
def parse(self, data: list[OpenWeatherLocation]) -> list[Location]:
|
||||
return [
|
||||
Location(
|
||||
id=f"{item.lat}:{item.lon}",
|
||||
name=item.name,
|
||||
provider="openweather",
|
||||
lat=item.lat,
|
||||
lon=item.lon,
|
||||
country=item.country,
|
||||
country_code=item.country.lower(),
|
||||
district=item.state or "",
|
||||
subdistrict="",
|
||||
)
|
||||
for item in data
|
||||
]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "rp5"
|
||||
@@ -0,0 +1,62 @@
|
||||
import datetime
|
||||
|
||||
import aiohttp
|
||||
from aiocache import cached
|
||||
|
||||
from gallery.sketch.headers import build_headers
|
||||
from gallery.sketch.source import ApiSource
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
from gallery.sketch.weather.model import (
|
||||
Location,
|
||||
WeatherResponse,
|
||||
)
|
||||
from gallery.sketch.weather.util import merge_weather_values
|
||||
from gallery.util import TimeUnit
|
||||
|
||||
from . import PROVIDER
|
||||
from .parser import RP5ForecastParser, RP5LocationParser
|
||||
|
||||
|
||||
class RP5WeatherApi(WeatherApi):
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
forecast_parser = RP5ForecastParser()
|
||||
location_parser = RP5LocationParser()
|
||||
|
||||
def _build_source(self) -> ApiSource:
|
||||
return ApiSource("https://rp5.ru", headers=build_headers("rp5.ru"))
|
||||
|
||||
async def find_locations(self, query: str) -> list[Location]:
|
||||
endpoint = "docs/search/ru"
|
||||
data = aiohttp.FormData({"searchStr": query})
|
||||
content = await self.source.request(endpoint, method="POST", data=data)
|
||||
return self.location_parser.parse(content)
|
||||
|
||||
@cached(
|
||||
key_builder=lambda fun, self, location_id: f"api.weather.{self.provider}.source.{location_id}.forecast",
|
||||
alias="redis",
|
||||
ttl=TimeUnit.HOUR,
|
||||
)
|
||||
async def _get_location_forecast(self, location_id: str) -> list[WeatherResponse]:
|
||||
endpoint = f"{location_id}"
|
||||
content = await self.source.request(endpoint)
|
||||
return self.forecast_parser.parse(content)
|
||||
|
||||
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
||||
forecast = await self._get_location_forecast(location_id)
|
||||
for response in forecast:
|
||||
if response.date == date:
|
||||
return response
|
||||
if date == datetime.date.today():
|
||||
return WeatherResponse(location=location_id, date=date, period="day", values=[])
|
||||
raise ValueError(f"Unsupported date: {date}")
|
||||
|
||||
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
|
||||
forecast = await self._get_location_forecast(location_id)
|
||||
values = [merge_weather_values(item.date, item.values) for item in forecast]
|
||||
return WeatherResponse(
|
||||
location=forecast[0].location,
|
||||
date=datetime.date.today(),
|
||||
period="days",
|
||||
values=values,
|
||||
)
|
||||
@@ -0,0 +1,218 @@
|
||||
import datetime
|
||||
import re
|
||||
|
||||
from bs4 import Tag
|
||||
|
||||
from gallery.sketch.parse.date import DATE_PARSER
|
||||
from gallery.sketch.parse.html import (
|
||||
HTMLParser,
|
||||
TableParser,
|
||||
TableValueParser,
|
||||
TagIterParser,
|
||||
TagParser,
|
||||
)
|
||||
from gallery.sketch.weather.model import (
|
||||
Cloudness,
|
||||
Location,
|
||||
Precipitation,
|
||||
WeatherResponse,
|
||||
WeatherValue,
|
||||
WindDirection,
|
||||
)
|
||||
from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER
|
||||
from gallery.sketch.weather.util import fix_dates_sequence
|
||||
|
||||
from . import PROVIDER
|
||||
|
||||
|
||||
class LocationNameParser(TagParser[str]):
|
||||
selector = ".intoLeftNavi > .verticalBottom > a"
|
||||
|
||||
def extract(self, data: Tag) -> str:
|
||||
return data.text.strip()
|
||||
|
||||
|
||||
class DatesParser(TagIterParser[tuple[datetime.datetime, int]]):
|
||||
selector = "#forecastTable > tr.forecastDate > td"
|
||||
|
||||
def extract(self, data: Tag) -> tuple[datetime.datetime, int]:
|
||||
colspan = int(data.attrs["colspan"])
|
||||
index = data.parent.index(data)
|
||||
if index == 1:
|
||||
colspan -= 1
|
||||
date_str = ",".join(data.text.strip().split(",")[-2:])
|
||||
date = DATE_PARSER.parse(date_str)
|
||||
return (date, colspan)
|
||||
|
||||
|
||||
class TimeParser(TableValueParser[datetime.datetime]):
|
||||
selector = "tr.forecastTime > td:not(:first-of-type)"
|
||||
model_key = "date"
|
||||
|
||||
def extract(self, data: Tag) -> datetime.datetime:
|
||||
hour = int(data.text.strip())
|
||||
return datetime.datetime.min.replace(hour=hour)
|
||||
|
||||
|
||||
class CloudnessParser(TableValueParser[Cloudness]):
|
||||
selector = "tr:nth-of-type(3) > td:not(:first-of-type)"
|
||||
model_key = "sky.cloudness"
|
||||
|
||||
CLOUDNESS_MAP = {
|
||||
"Ясно": Cloudness.CLEAR,
|
||||
"Малооблачно": Cloudness.PARTLY_CLOUDY,
|
||||
"Облачно с прояснениями": Cloudness.PARTLY_CLOUDY,
|
||||
"Переменная облачность": Cloudness.PARTLY_CLOUDY,
|
||||
"Небольшая облачность": Cloudness.CLOUDY,
|
||||
"Облачно": Cloudness.CLOUDY,
|
||||
"Значительная облачность": Cloudness.MAINLY_CLOUDY,
|
||||
"Пасмурная погода": Cloudness.MAINLY_CLOUDY,
|
||||
}
|
||||
|
||||
def extract(self, data: Tag) -> Cloudness:
|
||||
onmouseover = data.select_one(".cc_0").next_element.attrs["onmouseover"]
|
||||
cloudness_str = re.compile("<b>(.*?)</b>").search(onmouseover).group(1).strip()
|
||||
return self.CLOUDNESS_MAP[cloudness_str]
|
||||
|
||||
|
||||
class PrecipitationParser(TableValueParser[tuple[Precipitation, float]]):
|
||||
selector = "tr:nth-of-type(4) > td:not(:first-of-type)"
|
||||
model_key = ["sky.precipitation", "precipitation"]
|
||||
|
||||
PRECIPITATION_MAP = {
|
||||
"Явления погоды отсутствуют": Precipitation.NO,
|
||||
"Cлабый дождь": Precipitation.SMALL_RAIN,
|
||||
"Умеренный дождь": Precipitation.RAIN,
|
||||
}
|
||||
|
||||
def extract(self, data: Tag) -> tuple[Precipitation, float]:
|
||||
onmouseover = data.select_one(".pr_0").attrs["onmouseover"]
|
||||
precipitation_str = onmouseover.split(",")[1].strip(" '")
|
||||
precipitation_value_matched = re.compile("(\\d+\\.\\d+) мм").search(precipitation_str)
|
||||
precipitation_value = 0
|
||||
if precipitation_value_matched:
|
||||
# one hour value
|
||||
precipitation_value = float(precipitation_value_matched.group(1)) * 6
|
||||
if " (" in precipitation_str:
|
||||
precipitation_str = precipitation_str.split(" (")[0]
|
||||
return self.PRECIPITATION_MAP[precipitation_str], precipitation_value
|
||||
|
||||
|
||||
class TemperatureParser(TableValueParser[list[int]]):
|
||||
selector = "tr:nth-of-type(5) > td:not(:first-of-type)"
|
||||
model_key = "temperature"
|
||||
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(data.select_one(".t_0").text.lstrip("+").strip())]
|
||||
|
||||
|
||||
class PressureParser(TableValueParser[list[int]]):
|
||||
selector = "tr:nth-last-of-type(8) > td:not(:first-of-type)"
|
||||
model_key = "pressure"
|
||||
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(data.select_one(".p_0").text.strip())]
|
||||
|
||||
|
||||
class WindSpeedParser(TableValueParser[int]):
|
||||
selector = "tr:nth-last-of-type(7) > td:not(:first-of-type)"
|
||||
model_key = "wind.speed"
|
||||
|
||||
def extract(self, data: Tag) -> int:
|
||||
element = data.select_one(".wv_0")
|
||||
if element:
|
||||
return int(element.text.strip() or "0")
|
||||
return 0
|
||||
|
||||
|
||||
class WindGustParser(WindSpeedParser):
|
||||
selector = "tr:nth-last-of-type(6) > td:not(:first-of-type)"
|
||||
model_key = "wind.gust"
|
||||
|
||||
|
||||
class WindDirectionParser(TableValueParser[WindDirection | None]):
|
||||
selector = "tr:nth-last-of-type(5) > td:not(:first-of-type)"
|
||||
model_key = "wind.direction"
|
||||
|
||||
def extract(self, data: Tag) -> WindDirection | None:
|
||||
return WIND_DIRECTION_PARSER.parse(data.text.strip())
|
||||
|
||||
|
||||
class HumidityParser(TableValueParser[int]):
|
||||
selector = "tr:nth-last-of-type(4) > td:not(:first-of-type)"
|
||||
model_key = "humidity"
|
||||
|
||||
def extract(self, data: Tag) -> int:
|
||||
return int(data.text.strip())
|
||||
|
||||
|
||||
class RP5TableParser(TableParser[WeatherValue]):
|
||||
selector = "#forecastTable"
|
||||
value_parsers = [
|
||||
TimeParser(),
|
||||
CloudnessParser(),
|
||||
PrecipitationParser(),
|
||||
TemperatureParser(),
|
||||
PressureParser(),
|
||||
WindSpeedParser(),
|
||||
WindGustParser(),
|
||||
WindDirectionParser(),
|
||||
HumidityParser(),
|
||||
]
|
||||
|
||||
def create_model(self, data: dict) -> WeatherValue:
|
||||
return WeatherValue.model_validate(data)
|
||||
|
||||
|
||||
class RP5ForecastParser(HTMLParser[list[WeatherResponse]]):
|
||||
location_parser = LocationNameParser()
|
||||
dates_parser = DatesParser()
|
||||
table_parser = RP5TableParser()
|
||||
|
||||
def extract(self, data: Tag) -> list[WeatherResponse]:
|
||||
location = self.location_parser.parse(data)
|
||||
dates = self.dates_parser.parse(data)
|
||||
values = self.table_parser.parse(data)
|
||||
responses = []
|
||||
for date, count in dates:
|
||||
item_values, values = values[:count], values[count:]
|
||||
for value in item_values:
|
||||
value.date = date.replace(hour=value.date.hour)
|
||||
responses.append(
|
||||
WeatherResponse(
|
||||
location=location,
|
||||
date=date,
|
||||
period="day",
|
||||
values=item_values,
|
||||
)
|
||||
)
|
||||
fix_dates_sequence(responses)
|
||||
return responses
|
||||
|
||||
|
||||
class RP5LocationParser(HTMLParser[list[Location]]):
|
||||
selector = ".searchResults"
|
||||
|
||||
def extract(self, data: Tag) -> list[Location]:
|
||||
result = []
|
||||
for row in data.select("tr")[1:]:
|
||||
cells = row.select("td")
|
||||
name = cells[0].text.strip()
|
||||
location_id = cells[0].select_one("a").attrs["href"].lstrip("/")
|
||||
country = cells[1].text.strip()
|
||||
district = cells[2].text.strip()
|
||||
subdistrict = cells[3].text.strip()
|
||||
result.append(
|
||||
Location(
|
||||
id=location_id,
|
||||
name=name,
|
||||
provider=PROVIDER,
|
||||
lat=0,
|
||||
lon=0,
|
||||
country=country,
|
||||
country_code="ru", # TODO: contry_code from country
|
||||
district=district,
|
||||
subdistrict=subdistrict,
|
||||
)
|
||||
)
|
||||
return result
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "yandex"
|
||||
|
||||
@@ -1,92 +1,34 @@
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from gallery.sketch.headers import build_headers
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
|
||||
from gallery.sketch.schedule.model import Channel, Schedule
|
||||
from gallery.sketch.source import ApiSource
|
||||
|
||||
logger = logging.getLogger("matchtv")
|
||||
from . import PROVIDER
|
||||
from .parser import ScheduleData, YandexTVChannelParser, YandexTVScheduleParser
|
||||
|
||||
|
||||
HEADERS: dict[str, str] = {
|
||||
"Accept": (
|
||||
"text/html,"
|
||||
"application/xhtml+xml,"
|
||||
"application/xml;q=0.9,"
|
||||
"image/avif,image/webp,"
|
||||
"image/apng,*/*;q=0.8,"
|
||||
"application/signed-exchange;v=b3;q=0.9"
|
||||
),
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"Accept-Language": "en-US,en;q=0.9",
|
||||
"Connection": "keep-alive",
|
||||
"Host": "tv.yandex.ru",
|
||||
"sec-ch-ua": '"Chromium";v="100", " Not A;Brand";v="99"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"Linux"',
|
||||
"Sec-Fetch-Dest": "document",
|
||||
"Sec-Fetch-Mode": "navigate",
|
||||
"Sec-Fetch-Site": "none",
|
||||
"Sec-Fetch-User": "?1",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (X11; Linux x86_64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/100.0.4896.133 "
|
||||
"Safari/537.36"
|
||||
),
|
||||
}
|
||||
class YandexTVApi(ScheduleApi):
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
schedule_parser = YandexTVScheduleParser()
|
||||
channel_parser = YandexTVChannelParser()
|
||||
|
||||
class YandexTvApi(ScheduleApi):
|
||||
PROVIDER = "yandextv"
|
||||
lr = 213 # magic id
|
||||
|
||||
def _build_source(self) -> ApiSource:
|
||||
return ApiSource("https://tv.yandex.ru", headers=HEADERS)
|
||||
return ApiSource("https://tv.yandex.ru", headers=build_headers("tv.yandex.ru"))
|
||||
|
||||
async def find_channels(self, query: str) -> list[Channel]:
|
||||
url = (
|
||||
"https://suggest-multi.yandex.ru/suggest-tv2?"
|
||||
f"v=4&uil=ru&lr=10&count_channels=4&count_programs=4&sn=50&part={query}"
|
||||
f"v=4&uil=ru&lr={self.lr}&count_channels=4&count_programs=4&sn=50&part={query}"
|
||||
)
|
||||
_, values = json.loads(await self.source.request(url))
|
||||
result = []
|
||||
for _, name, content in values:
|
||||
if content["label"] == "Каналы":
|
||||
channel_id = content["url"].split("/")[-1]
|
||||
result.append(Channel(id=channel_id, name=name, provider=self.provider))
|
||||
return result
|
||||
data = await self.source.request(url)
|
||||
return self.channel_parser.parse(data)
|
||||
|
||||
async def get_schedule(self, channel_id: str, date: datetime.date) -> Schedule:
|
||||
endpoint = f"channels/{channel_id}?date={date:%Y-%m-%d}"
|
||||
endpoint = f"{self.lr}/channel/{channel_id}?date={date:%Y-%m-%d}"
|
||||
data = await self.source.request(endpoint)
|
||||
soup = BeautifulSoup(data, features="html.parser")
|
||||
if soup.select_one(".CheckboxCaptcha") is not None:
|
||||
raise RuntimeError("Captcha")
|
||||
values = []
|
||||
channel_name = soup.select_one(".channel-header__text").text.strip()
|
||||
current_day = datetime.datetime.combine(date.today(), datetime.datetime.min.time())
|
||||
end = current_day + datetime.timedelta(days=1, hours=6)
|
||||
prev_value: ScheduleValue | None = None
|
||||
for item in soup.select(".channel-schedule .channel-schedule__event"):
|
||||
title = item.select_one(".channel-schedule__title").text.strip()
|
||||
time_str = item.select_one(".channel-schedule__time").text.strip()
|
||||
hours, minutes = map(int, time_str.split(":"))
|
||||
item_date = current_day.replace(hour=hours, minute=minutes)
|
||||
if prev_value is not None and item_date.hour < prev_value.start.hour:
|
||||
current_day += datetime.timedelta(days=1)
|
||||
item_date += datetime.timedelta(days=1)
|
||||
live = item.select_one(".channel-schedule__info .icon_live") is not None
|
||||
value = ScheduleValue(start=item_date, end=end, label=title, live=live)
|
||||
values.append(value)
|
||||
if prev_value is not None:
|
||||
prev_value.end = item_date
|
||||
prev_value = value
|
||||
return Schedule(
|
||||
channel=Channel(id=channel_id, name=channel_name, provider=self.provider),
|
||||
date=date,
|
||||
values=values,
|
||||
)
|
||||
return self.schedule_parser.parse(ScheduleData(date, channel_id, data))
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import datetime
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from bs4 import Tag
|
||||
|
||||
from gallery.sketch.parse.core import ParsedContext
|
||||
from gallery.sketch.parse.json import JSONParser
|
||||
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
|
||||
|
||||
from ...sketch.parse.html import HTMLContextParser
|
||||
from . import PROVIDER
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScheduleData:
|
||||
date: datetime.date
|
||||
channel_id: str
|
||||
data: str
|
||||
|
||||
|
||||
class YandexTVScheduleParser(HTMLContextParser[ScheduleData, Schedule]):
|
||||
def extract(self, data: ParsedContext[ScheduleData, Tag]) -> Schedule:
|
||||
if data.data.select_one(".CheckboxCaptcha") is not None:
|
||||
raise RuntimeError("Captcha")
|
||||
values = []
|
||||
channel_name = data.data.select_one(".channel-header__text").text.strip()
|
||||
current_day = datetime.datetime.combine(data.context.date, datetime.datetime.min.time())
|
||||
end = current_day + datetime.timedelta(days=1, hours=6)
|
||||
prev_value: ScheduleValue | None = None
|
||||
for item in data.data.select(".channel-schedule .channel-schedule__event"):
|
||||
title = item.select_one(".channel-schedule__title").text.strip()
|
||||
time_str = item.select_one(".channel-schedule__time").text.strip()
|
||||
hours, minutes = map(int, time_str.split(":"))
|
||||
item_date = current_day.replace(hour=hours, minute=minutes)
|
||||
if prev_value is not None and item_date.hour < prev_value.start.hour:
|
||||
current_day += datetime.timedelta(days=1)
|
||||
item_date += datetime.timedelta(days=1)
|
||||
live = item.select_one(".channel-schedule__info .icon_live") is not None
|
||||
value = ScheduleValue(start=item_date, end=end, label=title, live=live)
|
||||
values.append(value)
|
||||
if prev_value is not None:
|
||||
prev_value.end = item_date
|
||||
prev_value = value
|
||||
return Schedule(
|
||||
channel=Channel(id=data.context.channel_id, name=channel_name, provider=PROVIDER),
|
||||
date=data.context.date,
|
||||
values=values,
|
||||
)
|
||||
|
||||
|
||||
class YandexTVChannelParser(JSONParser[list[Channel]]):
|
||||
def extract(self, data: Any) -> list[Channel]:
|
||||
_, values = data
|
||||
result = []
|
||||
for _, name, content in values:
|
||||
if content["label"] == "Каналы":
|
||||
channel_id = content["url"].split("/")[-1]
|
||||
result.append(Channel(id=channel_id, name=name, provider=PROVIDER))
|
||||
return result
|
||||
@@ -0,0 +1 @@
|
||||
PROVIDER = "yandex"
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
|
||||
from aiocache import cached
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from gallery.sketch.source import ApiSource
|
||||
from gallery.sketch.weather.api import WeatherApi
|
||||
@@ -14,13 +11,15 @@ from gallery.sketch.weather.model import (
|
||||
from gallery.sketch.weather.util import merge_weather_values
|
||||
from gallery.util import TimeUnit
|
||||
|
||||
from .parser import parse_article
|
||||
|
||||
logger = logging.getLogger("yandexweather")
|
||||
from . import PROVIDER
|
||||
from .parser import YandexForecastParser, YandexLocationParser
|
||||
|
||||
|
||||
class YandexWeatherApi(WeatherApi):
|
||||
PROVIDER = "yandex"
|
||||
PROVIDER = PROVIDER
|
||||
|
||||
forecast_parser = YandexForecastParser()
|
||||
location_parser = YandexLocationParser()
|
||||
|
||||
def _build_source(self) -> ApiSource:
|
||||
return ApiSource("https://yandex.ru")
|
||||
@@ -33,29 +32,12 @@ class YandexWeatherApi(WeatherApi):
|
||||
async def _get_location_forecast(self, location_id: str) -> list[WeatherResponse]:
|
||||
endpoint = f"pogoda/ru/{location_id}/details"
|
||||
content = await self.source.request(endpoint, mode="browser")
|
||||
soup = BeautifulSoup(content, features="html.parser")
|
||||
location = soup.select_one("[class^=Suggest_placeholder]").text.strip()
|
||||
articles = soup.select("article[class^=AppForecastDay]")
|
||||
return [parse_article(article, location) for article in articles]
|
||||
return self.forecast_parser.parse(content)
|
||||
|
||||
async def find_locations(self, query: str) -> list[Location]:
|
||||
endpoint = f"weather/api/suggest?part={query}&lang=ru&type=weather"
|
||||
response = await self.source.request(endpoint)
|
||||
data = json.loads(response)
|
||||
return [
|
||||
Location(
|
||||
id=str(item["geoId"]),
|
||||
name=item["title"],
|
||||
provider=self.provider,
|
||||
lat=item["coords"]["lat"],
|
||||
lon=item["coords"]["lon"],
|
||||
country="ru",
|
||||
country_code="ru",
|
||||
district=item["description"],
|
||||
subdistrict="",
|
||||
)
|
||||
for item in data
|
||||
]
|
||||
return self.location_parser.parse(response)
|
||||
|
||||
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
||||
forecast = await self._get_location_forecast(location_id)
|
||||
|
||||
@@ -1,29 +1,77 @@
|
||||
import datetime
|
||||
import logging
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
import dateparser
|
||||
from bs4 import Tag
|
||||
|
||||
from gallery.sketch.weather.model import Cloudness, Precipitation, Sky, WeatherResponse
|
||||
from gallery.sketch.weather.parser import ParseError, parse_wind_direction
|
||||
from gallery.sketch.weather.util import build_weather_value
|
||||
from gallery.sketch.parse.date import DATE_PARSER
|
||||
from gallery.sketch.parse.html import (
|
||||
HTMLParser,
|
||||
TableParser,
|
||||
TableValueParser,
|
||||
TagParser,
|
||||
)
|
||||
from gallery.sketch.parse.json import JSONParser
|
||||
from gallery.sketch.weather.model import (
|
||||
Cloudness,
|
||||
Location,
|
||||
Precipitation,
|
||||
Sky,
|
||||
WeatherResponse,
|
||||
WeatherValue,
|
||||
WindDirection,
|
||||
)
|
||||
from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER
|
||||
|
||||
logger = logging.getLogger("yandexweather")
|
||||
from . import PROVIDER
|
||||
|
||||
logger = logging.getLogger(PROVIDER)
|
||||
|
||||
|
||||
class DayPart(Enum):
|
||||
MORNING = ("Утром", 6)
|
||||
AFTERNOON = ("Днём", 12)
|
||||
EVENING = ("Вечером", 18)
|
||||
NIGHT = ("Ночью", 0)
|
||||
class LocationNameParser(TagParser[str]):
|
||||
selector = "[class^=Suggest_placeholder]"
|
||||
|
||||
def __init__(self, title: str, hour: int):
|
||||
super().__init__()
|
||||
self.title = title
|
||||
self.hour = hour
|
||||
def extract(self, data: Tag) -> str:
|
||||
return data.text.strip()
|
||||
|
||||
|
||||
def parse_sky(sky: str) -> Sky:
|
||||
class DateParser(TagParser[datetime.date]):
|
||||
selector = "[class^=AppForecastDayHeader_dayTitle]"
|
||||
|
||||
def extract(self, data: Tag) -> datetime.date:
|
||||
return DATE_PARSER.parse(data.text.split(",")[-1].strip())
|
||||
|
||||
|
||||
class TimeParser(TableValueParser[datetime.datetime]):
|
||||
selector = "[style$='-part']"
|
||||
model_key = "date"
|
||||
|
||||
HOUR_MAP = {
|
||||
"Утром": 6,
|
||||
"Днём": 12,
|
||||
"Вечером": 18,
|
||||
"Ночью": 0,
|
||||
}
|
||||
|
||||
def extract(self, data: Tag) -> datetime.datetime:
|
||||
hour = self.HOUR_MAP[data.text.strip()]
|
||||
return datetime.datetime.min.replace(hour=hour)
|
||||
|
||||
|
||||
class TemperatureParser(TableValueParser[list[int]]):
|
||||
selector = "[style$='-temp']"
|
||||
model_key = "temperature"
|
||||
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(data.text.strip().rstrip("°"))]
|
||||
|
||||
|
||||
class SkyParser(TableValueParser[Sky]):
|
||||
selector = "[style$='-text']"
|
||||
model_key = "sky"
|
||||
|
||||
def extract(self, data: Tag) -> Sky:
|
||||
sky = data.text.strip()
|
||||
cloudness = Cloudness.CLEAR
|
||||
precipitation = Precipitation.NO
|
||||
thunder = False
|
||||
@@ -52,40 +100,87 @@ def parse_sky(sky: str) -> Sky:
|
||||
)
|
||||
|
||||
|
||||
def parse_article(article: Tag, location: str) -> WeatherResponse:
|
||||
date_str = article.select_one("[class^=AppForecastDayHeader_dayTitle]").text.split(",")[-1].strip()
|
||||
date = dateparser.parse(date_str, languages=["ru"])
|
||||
if date is None:
|
||||
raise ParseError.invalid_date(date_str)
|
||||
values = []
|
||||
for day_part in DayPart:
|
||||
value = build_weather_value(date.replace(hour=day_part.hour))
|
||||
day_part_tag = article.find(string=day_part.title)
|
||||
temperature_tag = day_part_tag.next_element
|
||||
temperature = int(temperature_tag.text.strip().rstrip("°"))
|
||||
value.temperature.append(temperature)
|
||||
sky_tag = temperature_tag.next_element.next_element.next_element
|
||||
sky_str = sky_tag.text.strip()
|
||||
value.sky = parse_sky(sky_str)
|
||||
temperature_tag = sky_tag.next_element.next_element
|
||||
temperature = int(temperature_tag.text.strip().rstrip("°"))
|
||||
value.temperature.append(temperature)
|
||||
wind_speed_tag = temperature_tag.next_element.next_element
|
||||
wind_speed = int(wind_speed_tag.text.strip())
|
||||
value.wind_speed = wind_speed
|
||||
wind_direction_tag = wind_speed_tag.next_element.next_element.next_element.next_element
|
||||
wind_direction_str = wind_direction_tag.text.strip()
|
||||
value.wind_direction = parse_wind_direction(wind_direction_str)
|
||||
humidity_tag = wind_direction_tag.next_element.next_element.next_element.next_element
|
||||
humidity = int(humidity_tag.text.strip().rstrip("%"))
|
||||
value.humidity = humidity
|
||||
pressure_tag = humidity_tag.next_element.next_element
|
||||
pressure = int(pressure_tag.text.strip())
|
||||
value.pressure.append(pressure)
|
||||
values.append(value)
|
||||
return WeatherResponse(
|
||||
class WindSpeedParser(TableValueParser[int]):
|
||||
selector = "[style$='-wind']:not([class*=AppForecastDayPart_wind_formatted])"
|
||||
model_key = "wind.speed"
|
||||
|
||||
def extract(self, data: Tag) -> int:
|
||||
return int(data.text.strip())
|
||||
|
||||
|
||||
class WindDirectionParser(TableValueParser[WindDirection | None]):
|
||||
selector = "[style$='-dir']"
|
||||
model_key = "wind.direction"
|
||||
|
||||
def extract(self, data: Tag) -> WindDirection | None:
|
||||
return WIND_DIRECTION_PARSER.parse(data.text.strip())
|
||||
|
||||
|
||||
class HumidityParser(TableValueParser[int]):
|
||||
selector = "[style$='-hum']"
|
||||
model_key = "humidity"
|
||||
|
||||
def extract(self, data: Tag) -> int:
|
||||
return int(data.text.strip().rstrip("%"))
|
||||
|
||||
|
||||
class PressureParser(TableValueParser[list[int]]):
|
||||
selector = "[style$='-press']"
|
||||
model_key = "pressure"
|
||||
|
||||
def extract(self, data: Tag) -> list[int]:
|
||||
return [int(data.text.strip().rstrip("%"))]
|
||||
|
||||
|
||||
class YandexTableParser(TableParser[WeatherValue]):
|
||||
selector = ""
|
||||
value_parsers = [
|
||||
TimeParser(),
|
||||
TemperatureParser(),
|
||||
SkyParser(),
|
||||
WindSpeedParser(),
|
||||
WindDirectionParser(),
|
||||
HumidityParser(),
|
||||
PressureParser(),
|
||||
]
|
||||
|
||||
def create_model(self, data: dict) -> WeatherValue:
|
||||
return WeatherValue.model_validate(data)
|
||||
|
||||
|
||||
class YandexForecastParser(HTMLParser[list[WeatherResponse]]):
|
||||
table_parser = YandexTableParser()
|
||||
date_parser = DateParser()
|
||||
location_parser = LocationNameParser()
|
||||
|
||||
def extract(self, data: Tag) -> list[WeatherResponse]:
|
||||
location = self.location_parser.parse(data)
|
||||
articles = data.select("article[class^=AppForecastDay]")
|
||||
return [
|
||||
WeatherResponse(
|
||||
location=location,
|
||||
date=date,
|
||||
date=self.date_parser.parse(item),
|
||||
period="day",
|
||||
values=values,
|
||||
values=self.table_parser.parse(item),
|
||||
)
|
||||
for item in articles
|
||||
]
|
||||
|
||||
|
||||
class YandexLocationParser(JSONParser[list[Location]]):
|
||||
|
||||
def extract(self, data: Any) -> list[Location]:
|
||||
return [
|
||||
Location(
|
||||
id=str(item["geoId"]),
|
||||
name=item["title"],
|
||||
provider=PROVIDER,
|
||||
lat=item["coords"]["lat"],
|
||||
lon=item["coords"]["lon"],
|
||||
country="ru",
|
||||
country_code="ru",
|
||||
district=item["description"],
|
||||
subdistrict="",
|
||||
)
|
||||
for item in data
|
||||
]
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
DEFAULT_USER_AGENT = (
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36"
|
||||
)
|
||||
|
||||
DEFAULT_ACCEPT = (
|
||||
"text/html,"
|
||||
"application/xhtml+xml,"
|
||||
"application/xml;q=0.9,"
|
||||
"image/avif,"
|
||||
"image/webp,"
|
||||
"image/apng,"
|
||||
"*/*;q=0.8,"
|
||||
"application/signed-exchange;v=b3;q=0.7"
|
||||
)
|
||||
|
||||
DEFAULT_HEADERS = {
|
||||
"Accept": DEFAULT_ACCEPT,
|
||||
"Accept-Encoding": "gzip, deflate, br, zstd",
|
||||
"Accept-Language": "en-US,en;q=0.9,ru-RU;q=0.8,ru;q=0.7,uk;q=0.6",
|
||||
"Cache-Control": "max-age=0",
|
||||
"Connection": "keep-alive",
|
||||
"Host": None,
|
||||
"Referer": None,
|
||||
"Sec-Fetch-Dest": "document",
|
||||
"Sec-Fetch-Mode": "navigate",
|
||||
"Sec-Fetch-Site": "none",
|
||||
"Sec-Fetch-User": "?1",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
"User-Agent": DEFAULT_USER_AGENT,
|
||||
"sec-ch-ua": '"Not;A=Brand";v="8", "Chromium";v="150", "Google Chrome";v="150"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"Linux"',
|
||||
}
|
||||
|
||||
|
||||
def build_headers(host: str, *, user_agent: str | None = None) -> dict[str, str]:
|
||||
result = DEFAULT_HEADERS.copy()
|
||||
result["Host"] = host
|
||||
result["Referer"] = f"https://{host}/"
|
||||
if user_agent is not None:
|
||||
result["User-Agent"] = user_agent
|
||||
return result
|
||||
@@ -0,0 +1,49 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generic, NamedTuple, Protocol, TypeVar
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
D = TypeVar("D")
|
||||
R = TypeVar("R")
|
||||
|
||||
|
||||
class Parser[D, R](Protocol):
|
||||
def parse(self, data: D) -> R: ...
|
||||
|
||||
|
||||
P = TypeVar("P")
|
||||
|
||||
|
||||
class PrepareParser(ABC, Parser[D, R], Generic[D, P, R]):
|
||||
|
||||
@abstractmethod
|
||||
def prepare(self, data: D) -> P: ...
|
||||
|
||||
@abstractmethod
|
||||
def extract(self, data: P) -> R: ...
|
||||
|
||||
def parse(self, data: D) -> R:
|
||||
return self.extract(self.prepare(data))
|
||||
|
||||
|
||||
class Context(Protocol):
|
||||
data: str
|
||||
|
||||
|
||||
C = TypeVar("C", bound=Context)
|
||||
|
||||
|
||||
class ParsedContext(NamedTuple, Generic[C, D]):
|
||||
context: C
|
||||
data: D
|
||||
|
||||
|
||||
class ContextParser(PrepareParser[C, ParsedContext[C, P], R], Generic[C, P, R]):
|
||||
@abstractmethod
|
||||
def prepare_context(self, data: str) -> P: ...
|
||||
|
||||
def prepare(self, data: C) -> ParsedContext[C, P]:
|
||||
return ParsedContext(context=data, data=self.prepare_context(data.data))
|
||||
@@ -0,0 +1,16 @@
|
||||
import datetime
|
||||
|
||||
import dateparser
|
||||
|
||||
from .core import ParseError, Parser
|
||||
|
||||
|
||||
class DateParser(Parser[str, datetime.datetime]):
|
||||
def parse(self, data: str) -> datetime.datetime:
|
||||
date = dateparser.parse(data, languages=["ru"])
|
||||
if date is None:
|
||||
raise ParseError("Invalid date", data)
|
||||
return date
|
||||
|
||||
|
||||
DATE_PARSER = DateParser()
|
||||
@@ -0,0 +1,83 @@
|
||||
from typing import Generic, Iterable
|
||||
|
||||
from bs4 import BeautifulSoup, Tag
|
||||
|
||||
from .core import C, ContextParser, ParseError, Parser, PrepareParser, R
|
||||
from .util import set_dict_nested_value
|
||||
|
||||
|
||||
def _resolve_tag(source: Tag, selector: str | None = None) -> Tag:
|
||||
if selector:
|
||||
tag = source.select_one(selector)
|
||||
if tag is None:
|
||||
raise ParseError("Tag not found", selector)
|
||||
return tag
|
||||
return source
|
||||
|
||||
|
||||
class _HTMLParser(Parser[str, BeautifulSoup]):
|
||||
def parse(self, data: str) -> BeautifulSoup:
|
||||
return BeautifulSoup(data, features="html.parser")
|
||||
|
||||
|
||||
HTML_PARSER = _HTMLParser()
|
||||
|
||||
|
||||
class HTMLParser(PrepareParser[str, Tag, R], Generic[R]):
|
||||
selector: str | None = None
|
||||
|
||||
def prepare(self, data: str) -> Tag:
|
||||
return _resolve_tag(HTML_PARSER.parse(data), self.selector)
|
||||
|
||||
|
||||
class HTMLContextParser(ContextParser[C, Tag, R], Generic[C, R]):
|
||||
selector: str | None = None
|
||||
|
||||
def prepare_context(self, data: str) -> Tag:
|
||||
return _resolve_tag(HTML_PARSER.parse(data), self.selector)
|
||||
|
||||
|
||||
class TagParser(PrepareParser[Tag, Tag, R], Generic[R]):
|
||||
selector: str | None = None
|
||||
|
||||
def prepare(self, data: Tag) -> Tag:
|
||||
return _resolve_tag(data, self.selector)
|
||||
|
||||
|
||||
class TagIterParser(Parser[Tag, Iterable[R]], Generic[R]):
|
||||
selector: str
|
||||
|
||||
def extract(self, data: Tag) -> R:
|
||||
raise NotImplementedError
|
||||
|
||||
def parse(self, data: Tag) -> Iterable[R]:
|
||||
tags = data.select(self.selector)
|
||||
for tag in tags:
|
||||
yield self.extract(tag)
|
||||
|
||||
|
||||
class TableValueParser(TagIterParser[R], Generic[R]):
|
||||
model_key: str | list[str]
|
||||
|
||||
def extract(self, data: Tag) -> R:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class TableParser(TagParser[list[R]], Generic[R]):
|
||||
value_parsers: list[TableValueParser]
|
||||
|
||||
def create_model(self, data: dict) -> R:
|
||||
raise NotImplementedError
|
||||
|
||||
def extract(self, data: Tag) -> list[R]:
|
||||
results = []
|
||||
for parser in self.value_parsers:
|
||||
for index, value in enumerate(parser.parse(data)):
|
||||
while index > len(results) - 1:
|
||||
results.append({})
|
||||
if isinstance(parser.model_key, list):
|
||||
for model_key_index, model_key in enumerate(parser.model_key):
|
||||
set_dict_nested_value(results[index], model_key, value[model_key_index])
|
||||
else:
|
||||
set_dict_nested_value(results[index], parser.model_key, value)
|
||||
return [self.create_model(item) for item in results]
|
||||
@@ -0,0 +1,22 @@
|
||||
import json
|
||||
from typing import Any, Generic
|
||||
|
||||
from .core import C, ContextParser, Parser, PrepareParser, R
|
||||
|
||||
|
||||
class _JSONParser(Parser[str, Any]):
|
||||
def parse(self, data: str) -> Any:
|
||||
return json.loads(data)
|
||||
|
||||
|
||||
JSON_PARSER = _JSONParser()
|
||||
|
||||
|
||||
class JSONParser(PrepareParser[str, Any, R], Generic[R]):
|
||||
def prepare(self, data: str) -> Any:
|
||||
return JSON_PARSER.parse(data)
|
||||
|
||||
|
||||
class JSONContextParser(ContextParser[C, Any, R], Generic[C, R]):
|
||||
def prepare_context(self, data: str) -> Any:
|
||||
return JSON_PARSER.parse(data)
|
||||
@@ -0,0 +1,17 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
def set_nested_attr(target: Any, key: str, value: Any):
|
||||
parts = key.split(".")
|
||||
for part in parts[:-1]:
|
||||
target = getattr(target, part)
|
||||
setattr(target, parts[-1], value)
|
||||
|
||||
|
||||
def set_dict_nested_value(target: dict, key: str, value: Any):
|
||||
parts = key.split(".")
|
||||
for part in parts[:-1]:
|
||||
if part not in target:
|
||||
target[part] = {}
|
||||
target = target[part]
|
||||
target[parts[-1]] = value
|
||||
+29
-15
@@ -1,5 +1,6 @@
|
||||
import datetime
|
||||
import logging
|
||||
from typing import Literal
|
||||
from typing import Any, Literal
|
||||
|
||||
import aiohttp
|
||||
from playwright.async_api import async_playwright
|
||||
@@ -9,52 +10,65 @@ logger = logging.getLogger("source")
|
||||
RequestMode = Literal["base", "browser"]
|
||||
|
||||
DEFAULT_REQUEST_MODE: RequestMode = "base"
|
||||
DEFAULT_METHOD: str = "GET"
|
||||
|
||||
|
||||
class ApiSource:
|
||||
DEFAULT_USER_AGENT = (
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
|
||||
)
|
||||
DEFAULT_TIMEOUT = 30.0
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url,
|
||||
user_agent: str = DEFAULT_USER_AGENT,
|
||||
timeout: float = DEFAULT_TIMEOUT,
|
||||
cookies: dict[str, str] | None = None,
|
||||
headers: dict[str, str] | None = None,
|
||||
):
|
||||
self._base_url = base_url
|
||||
self._user_agent = user_agent
|
||||
self._timeout = timeout
|
||||
self._cookies = cookies
|
||||
self._headers = headers
|
||||
|
||||
async def _base_request(self, url: str) -> str:
|
||||
headers = {"User-Agent": self._user_agent, **(self._headers or {})}
|
||||
async def _base_request(
|
||||
self,
|
||||
url: str,
|
||||
method: str = DEFAULT_METHOD,
|
||||
data: Any | None = None,
|
||||
) -> str:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=headers,
|
||||
headers=self._headers,
|
||||
cookies=self._cookies,
|
||||
raise_for_status=True,
|
||||
) as session:
|
||||
async with session.request("GET", url, timeout=self._timeout) as response:
|
||||
async with session.request(method, url, timeout=self._timeout, data=data) as response:
|
||||
return await response.text()
|
||||
|
||||
async def _browser_request(self, url: str) -> str:
|
||||
# pylint:disable=unused-argument
|
||||
async def _browser_request(
|
||||
self,
|
||||
url: str,
|
||||
method: str = DEFAULT_METHOD,
|
||||
data: Any | None = None,
|
||||
) -> str:
|
||||
async with async_playwright() as p:
|
||||
browser = await p.chromium.launch()
|
||||
page = await browser.new_page()
|
||||
await page.goto(url, wait_until="networkidle")
|
||||
await page.goto(url, timeout=datetime.timedelta(seconds=self._timeout), wait_until="networkidle")
|
||||
content = await page.content()
|
||||
await browser.close()
|
||||
return content
|
||||
|
||||
async def request(self, endpoint: str, /, mode: RequestMode = DEFAULT_REQUEST_MODE) -> str:
|
||||
async def request(
|
||||
self,
|
||||
endpoint: str,
|
||||
/,
|
||||
mode: RequestMode = DEFAULT_REQUEST_MODE,
|
||||
method: str = DEFAULT_METHOD,
|
||||
data: Any | None = None,
|
||||
) -> str:
|
||||
if endpoint.startswith("https:"):
|
||||
url = endpoint
|
||||
else:
|
||||
url = f"{self._base_url}/{endpoint}"
|
||||
logger.info(url)
|
||||
method = getattr(self, f"_{mode}_request")
|
||||
return await method(url)
|
||||
fun = getattr(self, f"_{mode}_request")
|
||||
return await fun(url, method, data)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import datetime
|
||||
from enum import StrEnum, auto
|
||||
from typing import Self
|
||||
from typing import Literal, Self
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -51,14 +51,13 @@ Precipitation.values = list(Precipitation)
|
||||
|
||||
|
||||
class Sky(Model):
|
||||
cloudness: Cloudness
|
||||
precipitation: Precipitation
|
||||
thunder: bool
|
||||
fog: bool
|
||||
cloudness: Cloudness = Cloudness.CLEAR
|
||||
precipitation: Precipitation = Precipitation.NO
|
||||
thunder: bool = False
|
||||
fog: bool = False
|
||||
|
||||
|
||||
class WindDirection(StrEnum):
|
||||
CALM = auto()
|
||||
N = auto()
|
||||
NE = auto()
|
||||
E = auto()
|
||||
@@ -69,69 +68,24 @@ class WindDirection(StrEnum):
|
||||
NW = auto()
|
||||
|
||||
|
||||
class WindDirectionDeg(float):
|
||||
@property
|
||||
def direction(self) -> WindDirection:
|
||||
return self.to_direction()
|
||||
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self
|
||||
|
||||
# pylint:disable=too-many-return-statements
|
||||
def to_direction(self) -> WindDirection:
|
||||
if self == -1:
|
||||
return WindDirection.CALM
|
||||
elif self > 337.5 or self <= 22.25:
|
||||
return WindDirection.N
|
||||
elif self <= 67.5:
|
||||
return WindDirection.NE
|
||||
elif self <= 112.5:
|
||||
return WindDirection.E
|
||||
elif self <= 157.5:
|
||||
return WindDirection.SE
|
||||
elif self <= 202.5:
|
||||
return WindDirection.S
|
||||
elif self <= 247.5:
|
||||
return WindDirection.SW
|
||||
elif self <= 292.5:
|
||||
return WindDirection.W
|
||||
elif self <= 337.5:
|
||||
return WindDirection.NW
|
||||
else:
|
||||
raise ValueError(self)
|
||||
|
||||
@classmethod
|
||||
def from_direction(cls, direction: WindDirection) -> Self:
|
||||
return cls(
|
||||
{
|
||||
WindDirection.CALM: -1,
|
||||
WindDirection.N: 0,
|
||||
WindDirection.NE: 45,
|
||||
WindDirection.E: 90,
|
||||
WindDirection.SE: 135,
|
||||
WindDirection.S: 180,
|
||||
WindDirection.SW: 225,
|
||||
WindDirection.W: 270,
|
||||
WindDirection.NW: 315,
|
||||
}[direction]
|
||||
)
|
||||
class Wind(Model):
|
||||
speed: int = 0
|
||||
gust: int = 0
|
||||
direction: WindDirection | None = None
|
||||
|
||||
|
||||
class WeatherValue(Model):
|
||||
date: datetime.datetime
|
||||
sky: Sky
|
||||
temperature: list[int]
|
||||
wind_speed: int
|
||||
wind_gust: int
|
||||
wind_direction: float
|
||||
precipitation: float
|
||||
pressure: list[int] = Field(default=[])
|
||||
humidity: int = Field(default=-1)
|
||||
sky: Sky = Field(default_factory=Sky)
|
||||
wind: Wind = Field(default_factory=Wind)
|
||||
temperature: list[int] = Field(default_factory=list)
|
||||
precipitation: float = 0
|
||||
pressure: list[int] = Field(default_factory=list)
|
||||
humidity: int = -1
|
||||
|
||||
|
||||
class WeatherResponse(Model):
|
||||
location: str
|
||||
date: datetime.date
|
||||
period: str
|
||||
period: Literal["day", "days"] = "day"
|
||||
values: list[WeatherValue]
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
from gallery.sketch.parse.core import Parser
|
||||
|
||||
from ..model import WindDirection
|
||||
|
||||
|
||||
class WindDirectionParser(Parser[str | int | float, WindDirection | None]):
|
||||
WIND_DIRECTION_MAP: dict[str, WindDirection | None] = {
|
||||
"с": WindDirection.N,
|
||||
"св": WindDirection.NE,
|
||||
"в": WindDirection.E,
|
||||
"юв": WindDirection.SE,
|
||||
"ю": WindDirection.S,
|
||||
"юз": WindDirection.SW,
|
||||
"з": WindDirection.W,
|
||||
"сз": WindDirection.NW,
|
||||
}
|
||||
|
||||
# pylint:disable=too-many-return-statements
|
||||
def _parse_number(self, value: int | float) -> WindDirection | None:
|
||||
if value > 337.5 or value <= 22.25:
|
||||
return WindDirection.N
|
||||
elif value <= 67.5:
|
||||
return WindDirection.NE
|
||||
elif value <= 112.5:
|
||||
return WindDirection.E
|
||||
elif value <= 157.5:
|
||||
return WindDirection.SE
|
||||
elif value <= 202.5:
|
||||
return WindDirection.S
|
||||
elif value <= 247.5:
|
||||
return WindDirection.SW
|
||||
elif value <= 292.5:
|
||||
return WindDirection.W
|
||||
elif value <= 337.5:
|
||||
return WindDirection.NW
|
||||
else:
|
||||
return None
|
||||
|
||||
def _parse_str(self, value: str) -> WindDirection | None:
|
||||
return self.WIND_DIRECTION_MAP.get(value.replace("-", "").lower())
|
||||
|
||||
def parse(self, data: str | int | float) -> WindDirection | None:
|
||||
if isinstance(data, str):
|
||||
return self._parse_str(data)
|
||||
else:
|
||||
return self._parse_number(data)
|
||||
|
||||
|
||||
WIND_DIRECTION_PARSER = WindDirectionParser()
|
||||
@@ -1,28 +0,0 @@
|
||||
from typing import Self
|
||||
|
||||
from gallery.sketch.weather.model import WindDirection, WindDirectionDeg
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
@classmethod
|
||||
def invalid_date(cls, date_str: str) -> Self:
|
||||
return cls("Invalid date string", date_str)
|
||||
|
||||
|
||||
WIND_DIRECTION_MAP: dict[str, WindDirection] = {
|
||||
"—": WindDirection.CALM,
|
||||
"": WindDirection.CALM,
|
||||
"штиль": WindDirection.CALM,
|
||||
"с": WindDirection.N,
|
||||
"св": WindDirection.NE,
|
||||
"в": WindDirection.E,
|
||||
"юв": WindDirection.SE,
|
||||
"ю": WindDirection.S,
|
||||
"юз": WindDirection.SW,
|
||||
"з": WindDirection.W,
|
||||
"сз": WindDirection.NW,
|
||||
}
|
||||
|
||||
|
||||
def parse_wind_direction(value: str) -> WindDirectionDeg:
|
||||
return WindDirectionDeg.from_direction(WIND_DIRECTION_MAP[value.lower()])
|
||||
@@ -1,33 +1,20 @@
|
||||
import datetime
|
||||
import statistics
|
||||
from typing import Literal
|
||||
from typing import Counter, Literal, overload
|
||||
|
||||
from .model import Cloudness, Precipitation, Sky, WeatherValue, WindDirectionDeg
|
||||
from .model import Cloudness, Precipitation, WeatherResponse, WeatherValue
|
||||
|
||||
MergeMode = Literal["interval", "average"]
|
||||
|
||||
|
||||
def build_weather_value(date: datetime.datetime) -> WeatherValue:
|
||||
return WeatherValue(
|
||||
date=date,
|
||||
sky=Sky(
|
||||
cloudness=Cloudness.CLEAR,
|
||||
precipitation=Precipitation.NO,
|
||||
thunder=False,
|
||||
fog=False,
|
||||
),
|
||||
temperature=[],
|
||||
wind_speed=0,
|
||||
wind_gust=0,
|
||||
wind_direction=WindDirectionDeg(-1),
|
||||
precipitation=0,
|
||||
pressure=[],
|
||||
humidity=-1,
|
||||
)
|
||||
|
||||
|
||||
def _merge_values(values: list[int], mode: MergeMode) -> list[int]:
|
||||
return [max(values), min(values)] if mode == "interval" else [round(sum(values) / len(values))]
|
||||
if mode == "interval":
|
||||
if (max_value := max(values)) == (min_value := min(values)):
|
||||
return [max_value]
|
||||
else:
|
||||
return [max_value, min_value]
|
||||
else:
|
||||
return [round(sum(values) / len(values))]
|
||||
|
||||
|
||||
def merge_weather_values(
|
||||
@@ -35,7 +22,7 @@ def merge_weather_values(
|
||||
values: list[WeatherValue],
|
||||
mode: MergeMode = "interval",
|
||||
) -> WeatherValue:
|
||||
result = build_weather_value(date)
|
||||
result = WeatherValue(date=date)
|
||||
temperatures = []
|
||||
pressures = []
|
||||
humidities = []
|
||||
@@ -49,23 +36,43 @@ def merge_weather_values(
|
||||
temperatures += value.temperature
|
||||
pressures += value.pressure
|
||||
humidities.append(value.humidity)
|
||||
wind_speeds.append(value.wind_speed)
|
||||
wind_gusts.append(value.wind_gust)
|
||||
wind_directions.append(value.wind_direction)
|
||||
wind_speeds.append(value.wind.speed)
|
||||
wind_gusts.append(value.wind.gust)
|
||||
wind_directions.append(value.wind.direction)
|
||||
cloudnesses.append(value.sky.cloudness)
|
||||
precipitations.append(value.sky.precipitation)
|
||||
precipitation += value.precipitation
|
||||
result.temperature = _merge_values(temperatures, mode)
|
||||
result.pressure = _merge_values(pressures, mode)
|
||||
result.humidity = round(statistics.mean(humidities))
|
||||
result.wind_speed = round(statistics.mean(wind_speeds))
|
||||
result.wind_gust = round(statistics.mean(wind_gusts))
|
||||
result.wind_direction = statistics.mean(wind_directions)
|
||||
result.wind.speed = round(statistics.mean(wind_speeds))
|
||||
result.wind.gust = round(statistics.mean(wind_gusts))
|
||||
result.wind.direction = Counter(wind_directions).most_common(1)[0][0]
|
||||
for item in cloudnesses:
|
||||
# TODO: Instance of 'FieldInfo' has no 'cloudness' member (no-member)
|
||||
# pylint:disable=no-member
|
||||
if Cloudness.values.index(item) > Cloudness.values.index(result.sky.cloudness):
|
||||
result.sky.cloudness = item
|
||||
for item in precipitations:
|
||||
# TODO: Instance of 'FieldInfo' has no 'precipitation' member (no-member)
|
||||
# pylint:disable=no-member
|
||||
if Precipitation.values.index(item) > Precipitation.values.index(result.sky.precipitation):
|
||||
result.sky.precipitation = item
|
||||
result.precipitation = precipitation
|
||||
return result
|
||||
|
||||
|
||||
def convert_hPa_to_mmHg(value: int) -> int:
|
||||
return round(value / 1.333)
|
||||
|
||||
|
||||
@overload
|
||||
def fix_dates_sequence(items: list[WeatherValue]): ...
|
||||
@overload
|
||||
def fix_dates_sequence(items: list[WeatherResponse]): ...
|
||||
def fix_dates_sequence(items):
|
||||
date = None
|
||||
for item in items:
|
||||
if date and (item.date - date).total_seconds() < 0:
|
||||
item.date = date + datetime.timedelta(days=1)
|
||||
date = item.date
|
||||
|
||||
@@ -69,9 +69,18 @@ msgstr "Давление, мм рт. ст."
|
||||
msgid "Humidity, %"
|
||||
msgstr "Влажность, %"
|
||||
|
||||
msgid "Stored locations"
|
||||
msgstr "Сохранённые локации"
|
||||
|
||||
# tv
|
||||
msgid "TV program"
|
||||
msgstr "Телепрограмма"
|
||||
|
||||
msgid "Live broadcasts"
|
||||
msgstr "Прямые трансляции"
|
||||
|
||||
msgid "Stored channels"
|
||||
msgstr "Сохранённые каналы"
|
||||
|
||||
msgid "Channel groups"
|
||||
msgstr "Группы каналов"
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[project]
|
||||
name = "gallery"
|
||||
description = ""
|
||||
version = "0.6.1"
|
||||
version = "0.10.0"
|
||||
readme = "README.md"
|
||||
authors = [{ name = "shmyga", email = "shmyga.z@gmail.com" }]
|
||||
requires-python = '>=3.14,<4.0'
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
docker compose -f docker-compose-develop.yaml up --build --watch
|
||||
@@ -1,55 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
. .env
|
||||
|
||||
build () {
|
||||
echo "build: $1"
|
||||
. "$1/.env"
|
||||
for PROJECT in "${DOCKER_PROJECTS[@]}"; do
|
||||
IFS=: read -r PROJECT_NAME PROJECT_TARGET <<< "$PROJECT"
|
||||
ARGS=("build")
|
||||
for ARG in ${DOCKER_ARGS[@]}; do
|
||||
ARGS+=("--build-arg" "$ARG")
|
||||
done
|
||||
if [ -n "$PROJECT_TARGET" ]; then
|
||||
ARGS+=("--target" "$PROJECT_TARGET")
|
||||
fi
|
||||
ARGS+=("-t" "$DOCKER_GROUP/$PROJECT_NAME" ".")
|
||||
ARGS+=("-f" "$1/Dockerfile")
|
||||
echo "${ARGS[@]}"
|
||||
docker "${ARGS[@]}"
|
||||
done
|
||||
}
|
||||
|
||||
publish () {
|
||||
echo "publish: $1"
|
||||
. "$1/.env"
|
||||
for PROJECT in "${DOCKER_PROJECTS[@]}"; do
|
||||
IFS=: read -r PROJECT_NAME PROJECT_TARGET <<< "$PROJECT"
|
||||
docker tag $DOCKER_GROUP/$PROJECT_NAME $DOCKER_ROOT/$PROJECT_NAME:$VERSION
|
||||
docker tag $DOCKER_GROUP/$PROJECT_NAME $DOCKER_ROOT/$PROJECT_NAME:$DOCKER_TAG
|
||||
docker push $DOCKER_ROOT/$PROJECT_NAME:$VERSION
|
||||
docker push $DOCKER_ROOT/$PROJECT_NAME:$DOCKER_TAG
|
||||
done
|
||||
}
|
||||
|
||||
save () {
|
||||
echo "save: $1"
|
||||
. "$1/.env"
|
||||
mkdir -p "$1/dist"
|
||||
for PROJECT in "${DOCKER_PROJECTS[@]}"; do
|
||||
IFS=: read -r PROJECT_NAME PROJECT_TARGET <<< "$PROJECT"
|
||||
docker save --output "$1/dist/$PROJECT_NAME-$VERSION.tar" "$DOCKER_GROUP/$PROJECT_NAME"
|
||||
done
|
||||
}
|
||||
|
||||
DEFAULT_TARGETS="."
|
||||
TARGETS="${@-$DEFAULT_TARGETS}"
|
||||
|
||||
DOCKER_ACTION="${DOCKER_ACTION-build}"
|
||||
|
||||
for TARGET in $TARGETS; do
|
||||
$DOCKER_ACTION "$TARGET"
|
||||
done
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
TARGET="gallery"
|
||||
|
||||
poetry run isort $TARGET
|
||||
poetry run black $TARGET -q
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
TARGET="gallery"
|
||||
|
||||
poetry run pylint $TARGET -sn
|
||||
poetry run isort $TARGET --check-only
|
||||
poetry run black $TARGET -q --check --diff
|
||||
@@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
cd locales/ru/LC_MESSAGES || exit
|
||||
msgfmt messages.po
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
# env
|
||||
if [[ ! -f .env ]]; then
|
||||
cp .env-base .env
|
||||
fi
|
||||
source .env
|
||||
|
||||
# python
|
||||
poetry env use ${PYTHON_VERSION}
|
||||
poetry install
|
||||
.venv/bin/playwright install chromium --with-deps --only-shell
|
||||
|
||||
# static
|
||||
cd static || exit
|
||||
|
||||
if [[ -f $HOME/.nvm/nvm.sh ]]; then
|
||||
source "$HOME/.nvm/nvm.sh"
|
||||
nvm use
|
||||
fi
|
||||
npm ci
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
OPENWEATHER_KEY="" poetry run pytest tests
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
cd "$(dirname $(dirname "$0"))" || exit
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 [version]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -z "$(git status -s)" ]]; then
|
||||
echo "Uncomitted changes"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
poetry version $1
|
||||
(cd static && npm version $1 --allow-same-version)
|
||||
git add .
|
||||
git commit -m "ci(version): $1"
|
||||
git tag $1
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "gallery",
|
||||
"version": "0.6.1",
|
||||
"version": "0.10.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "gallery",
|
||||
"version": "0.6.1",
|
||||
"version": "0.10.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@popperjs/core": "^2.11.8",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gallery",
|
||||
"version": "0.6.1",
|
||||
"version": "0.10.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "webpack --mode production",
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
export interface Type<T> extends Function {
|
||||
new (...args: any[]): T;
|
||||
}
|
||||
|
||||
export abstract class ValueElement<T, O> extends HTMLElement {
|
||||
static observedAttributes = ["value"];
|
||||
|
||||
private _value: T | null = null;
|
||||
|
||||
get value(): T | null {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set value(value: T | null) {
|
||||
if (value) {
|
||||
this.setAttribute("value", JSON.stringify(value));
|
||||
} else if (this.hasAttribute("value")) {
|
||||
this.removeAttribute("value");
|
||||
}
|
||||
}
|
||||
|
||||
options: O | null;
|
||||
|
||||
constructor(value: T | null = null, options: O | null = null) {
|
||||
super();
|
||||
if (value) {
|
||||
this.value = value;
|
||||
}
|
||||
this.options = options;
|
||||
this.onInit();
|
||||
}
|
||||
|
||||
onInit() {}
|
||||
|
||||
beforeRefresh() {}
|
||||
|
||||
onRefresh() {}
|
||||
|
||||
abstract buildHTML(): string;
|
||||
|
||||
refresh() {
|
||||
const rawValue = this.getAttribute("value");
|
||||
if (rawValue) {
|
||||
this._value = JSON.parse(rawValue);
|
||||
} else {
|
||||
this._value = null;
|
||||
}
|
||||
if (this.className) {
|
||||
this.classList = this.className;
|
||||
}
|
||||
this.beforeRefresh();
|
||||
this.innerHTML = this.buildHTML();
|
||||
this.onRefresh();
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
||||
setTimeout(() => this.refresh());
|
||||
}
|
||||
}
|
||||
|
||||
export interface ListOptions<T> {
|
||||
onClick?: (element: ListItemElement<T>) => void;
|
||||
onAdd?: (element: ListItemElement<T>) => void;
|
||||
onRemove?: (element: ListItemElement<T>) => void;
|
||||
}
|
||||
|
||||
export abstract class ListItemElement<T> extends ValueElement<T, ListOptions<T>> {
|
||||
abstract getHref(value: T): string;
|
||||
|
||||
abstract getContent(value: T): string;
|
||||
|
||||
className = "list-group-item list-group-item-action";
|
||||
|
||||
onInit(): void {
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
||||
this.handleAddClick = this.handleAddClick.bind(this);
|
||||
}
|
||||
|
||||
buildHTML(): string {
|
||||
if (!this.value) {
|
||||
return "";
|
||||
}
|
||||
const href = this.getHref(this.value);
|
||||
const content = this.getContent(this.value);
|
||||
return `
|
||||
<a href="${href}"
|
||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||
<span>
|
||||
<span class="text-success me-2 ${this.options?.onAdd ? "" : "d-none"}" style="color: green;" data-add>
|
||||
+
|
||||
</span>
|
||||
${content}
|
||||
</span>
|
||||
<span>
|
||||
<span class="text-danger ms-2 ${this.options?.onRemove ? "" : "d-none"}" data-remove>
|
||||
✕
|
||||
</span>
|
||||
</span>
|
||||
</a>
|
||||
`;
|
||||
}
|
||||
|
||||
onRefresh(): void {
|
||||
this.addEventListener("click", this.handleClick);
|
||||
if (this.options?.onAdd) {
|
||||
this.querySelector<HTMLElement>("[data-add]")?.addEventListener("click", this.handleAddClick);
|
||||
} else {
|
||||
this.querySelector("[data-add]")?.remove();
|
||||
}
|
||||
if (this.options?.onRemove) {
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
||||
} else {
|
||||
this.querySelector("[data-remove]")?.remove();
|
||||
}
|
||||
}
|
||||
|
||||
handleClick(event: MouseEvent) {
|
||||
if (this.value && this.options?.onClick) {
|
||||
this.options.onClick(this);
|
||||
}
|
||||
}
|
||||
|
||||
handleAddClick(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (this.value && this.options?.onAdd) {
|
||||
this.options.onAdd(this);
|
||||
}
|
||||
}
|
||||
|
||||
handleRemoveClick(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (this.value && this.options?.onRemove) {
|
||||
this.options.onRemove(this);
|
||||
}
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this.removeEventListener("click", this.handleClick);
|
||||
this.querySelector<HTMLElement>("[data-add]")?.removeEventListener("click", this.handleAddClick);
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class ListElement<T, C extends ListItemElement<T> = ListItemElement<T>> extends ValueElement<
|
||||
T[],
|
||||
ListOptions<T>
|
||||
> {
|
||||
static observedAttributes = ["value", "onadd", "onremove", "onclick"];
|
||||
|
||||
abstract childType: Type<C>;
|
||||
|
||||
className = "list-group";
|
||||
|
||||
buildHTML(): string {
|
||||
return "";
|
||||
}
|
||||
|
||||
beforeRefresh() {
|
||||
if (this.hasAttribute("onclick")) {
|
||||
const onclick = new Function("element", this.getAttribute("onclick")!) as ListOptions<T>["onClick"];
|
||||
if (!this.options) {
|
||||
this.options = {};
|
||||
}
|
||||
this.options.onClick = onclick;
|
||||
}
|
||||
if (this.hasAttribute("onadd")) {
|
||||
const onadd = new Function("element", this.getAttribute("onadd")!) as ListOptions<T>["onAdd"];
|
||||
if (!this.options) {
|
||||
this.options = {};
|
||||
}
|
||||
this.options.onAdd = onadd;
|
||||
}
|
||||
if (this.hasAttribute("onremove")) {
|
||||
const onremove = new Function("element", this.getAttribute("onremove")!) as ListOptions<T>["onRemove"];
|
||||
if (!this.options) {
|
||||
this.options = {};
|
||||
}
|
||||
this.options.onRemove = onremove;
|
||||
}
|
||||
}
|
||||
|
||||
onRefresh() {
|
||||
if (this.value) {
|
||||
for (const item of this.value) {
|
||||
this.appendChild(new this.childType(item, this.options));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { ListElement, ListItemElement } from "./component";
|
||||
import { DataStorage, StorageValue } from "./storage";
|
||||
|
||||
export class DataManager<T extends StorageValue> {
|
||||
private storage: DataStorage<T>;
|
||||
|
||||
private get dataList(): ListElement<T> {
|
||||
return document.querySelector(this.dataListSelector)!;
|
||||
}
|
||||
|
||||
constructor(
|
||||
listComponentType: typeof ListElement<T>, //prevent removing as unusing
|
||||
storageKey: string,
|
||||
private dataListSelector: string,
|
||||
) {
|
||||
this.storage = new DataStorage(storageKey);
|
||||
this.load = this.load.bind(this);
|
||||
this.store = this.store.bind(this);
|
||||
this.add = this.add.bind(this);
|
||||
this.remove = this.remove.bind(this);
|
||||
}
|
||||
|
||||
load() {
|
||||
this.dataList.value = this.storage.getAll();
|
||||
}
|
||||
|
||||
store(element: ListItemElement<T>) {
|
||||
if (element.value) {
|
||||
this.storage.add(element.value);
|
||||
}
|
||||
}
|
||||
|
||||
add(element: ListItemElement<T>) {
|
||||
const value = element.value;
|
||||
value;
|
||||
if (value && this.storage.add(value)) {
|
||||
const clone: ListItemElement<T> = element.cloneNode(true) as ListItemElement<T>;
|
||||
clone.options = { onRemove: this.remove };
|
||||
this.dataList.appendChild(clone);
|
||||
}
|
||||
}
|
||||
|
||||
remove(element: ListItemElement<T>) {
|
||||
const value = element.value;
|
||||
if (value) {
|
||||
this.storage.remove(value.id);
|
||||
element.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
export interface StorageValue {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class DataStorage<T extends StorageValue> {
|
||||
constructor(private storageKey: string) {}
|
||||
|
||||
private readValues(): T[] {
|
||||
const objects = JSON.parse(window.localStorage.getItem(this.storageKey) || "[]");
|
||||
if (!Array.isArray(objects)) {
|
||||
return Object.values(objects);
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
|
||||
private writeValues(values: T[]) {
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(values));
|
||||
}
|
||||
|
||||
getAll(): T[] {
|
||||
return this.readValues();
|
||||
}
|
||||
|
||||
add(value: T): boolean {
|
||||
const values = this.readValues();
|
||||
for (const v of values) {
|
||||
if (v.id == value.id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
values.push(value);
|
||||
this.writeValues(values);
|
||||
return true;
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const values = this.readValues();
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
if (values[i].id == id) {
|
||||
values.splice(i, 1);
|
||||
this.writeValues(values);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
export interface Channel {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
}
|
||||
|
||||
export class ScheduleChannelStorage {
|
||||
private storageKey: string = "schedule:channels";
|
||||
|
||||
getAll(): Channel[] {
|
||||
const objects = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
return Object.values(objects);
|
||||
}
|
||||
|
||||
add(object: Channel) {
|
||||
const objects = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
objects[object.id] = object;
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(objects));
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const objects = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
delete objects[id];
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(objects));
|
||||
}
|
||||
}
|
||||
|
||||
export class ScheduleChannelManager {
|
||||
loadChannels(selector: string) {
|
||||
const channels = window.scheduleChannelStorage.getAll();
|
||||
const container = document.querySelector(selector);
|
||||
if (container) {
|
||||
container.innerHTML = "";
|
||||
for (const channel of channels) {
|
||||
const element = new ScheduleChannelElement();
|
||||
element.setAttribute("channel", JSON.stringify(channel));
|
||||
element.setAttribute("removable", "");
|
||||
container.appendChild(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ScheduleChannelElement extends HTMLElement {
|
||||
static observedAttributes = ["channel", "removable"];
|
||||
|
||||
channel: Channel | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.channel = JSON.parse(this.getAttribute("channel") || "{}");
|
||||
this.innerHTML = `
|
||||
<a href="${this.channel?.provider}/${this.channel?.id}"
|
||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||
<span>
|
||||
<span class="text-primary">${this.channel?.name}</span>
|
||||
</span>
|
||||
<span>
|
||||
<span class="badge text-bg-secondary">${this.channel?.provider}</span>
|
||||
<span class="text-danger ms-2" data-remove>
|
||||
✕
|
||||
</span>
|
||||
</span>
|
||||
</a>`;
|
||||
this.classList = "list-group-item list-group-item-action";
|
||||
this.addEventListener("click", this.handleClick);
|
||||
if (this.hasAttribute("removable")) {
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
||||
} else {
|
||||
this.querySelector("[data-remove]")?.remove();
|
||||
}
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this.removeEventListener("click", this.handleClick);
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
||||
}
|
||||
|
||||
handleClick(event: MouseEvent) {
|
||||
if (this.channel) {
|
||||
window.scheduleChannelStorage.add(this.channel);
|
||||
}
|
||||
}
|
||||
|
||||
handleRemoveClick(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (this.channel) {
|
||||
window.scheduleChannelStorage.remove(this.channel.id);
|
||||
}
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("schedule-channel", ScheduleChannelElement);
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
scheduleChannelStorage: ScheduleChannelStorage;
|
||||
scheduleChannelManager: ScheduleChannelManager;
|
||||
}
|
||||
}
|
||||
|
||||
window.scheduleChannelStorage = new ScheduleChannelStorage();
|
||||
window.scheduleChannelManager = new ScheduleChannelManager();
|
||||
@@ -0,0 +1,52 @@
|
||||
import { ListElement, ListItemElement, ValueElement } from "../base/component";
|
||||
import { Channel } from "./model";
|
||||
|
||||
export class ScheduleChannelGroup extends ValueElement<string | Channel[], {}> {
|
||||
className: string = "list-group-item list-group-item-action";
|
||||
|
||||
buildHTML(): string {
|
||||
const groupName = Array.isArray(this.value) ? "Open All" : this.value;
|
||||
const groupId = Array.isArray(this.value)
|
||||
? this.value.map((item) => `${item.provider}:${item.id}`).join(",")
|
||||
: this.value || "";
|
||||
return `
|
||||
<a href="group/${encodeURIComponent(groupId)}"
|
||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||
<span>
|
||||
<span class="text-primary" style="text-transform: capitalize">${groupName}</span>
|
||||
</span>
|
||||
</a>`;
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("schedule-channel-group", ScheduleChannelGroup);
|
||||
|
||||
export class ScheduleChannelItem extends ListItemElement<Channel> {
|
||||
getHref(value: Channel): string {
|
||||
return `${value.provider}/${value.id}`;
|
||||
}
|
||||
|
||||
getContent(value: Channel): string {
|
||||
return `
|
||||
<span class="badge me-1 text-bg-secondary">${value.provider}</span>
|
||||
<span class="text-primary">${value.name}</span>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ScheduleChannelList extends ListElement<Channel, ScheduleChannelItem> {
|
||||
childType = ScheduleChannelItem;
|
||||
|
||||
public onRefresh(): void {
|
||||
super.onRefresh();
|
||||
if (this.value?.length) {
|
||||
const groupElement = new ScheduleChannelGroup(this.value);
|
||||
this.prepend(groupElement);
|
||||
} else {
|
||||
this.prepend("Empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("schedule-channel-item", ScheduleChannelItem);
|
||||
customElements.define("schedule-channel-list", ScheduleChannelList);
|
||||
@@ -0,0 +1,11 @@
|
||||
import { DataManager } from "../base/manager";
|
||||
import { ScheduleChannelList } from "./component";
|
||||
import { Channel } from "./model";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
scheduleChannelManager: DataManager<Channel>;
|
||||
}
|
||||
}
|
||||
|
||||
window.scheduleChannelManager = new DataManager(ScheduleChannelList, "schedule:channels", "#storedChannels");
|
||||
@@ -0,0 +1,5 @@
|
||||
export interface Channel {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
export interface Location {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
country_code: string;
|
||||
country?: string;
|
||||
district?: string;
|
||||
subdistrict?: string;
|
||||
}
|
||||
|
||||
export class WeatherLocationStorage {
|
||||
private storageKey: string = "weather:locations";
|
||||
|
||||
getAll(): Location[] {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
return Object.values(locations);
|
||||
}
|
||||
|
||||
add(location: Location) {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
locations[location.id] = location;
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(locations));
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
delete locations[id];
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(locations));
|
||||
}
|
||||
}
|
||||
|
||||
export class WeatherLocationManager {
|
||||
loadLocations(selector: string) {
|
||||
const locations = window.weatherLocationStorage.getAll();
|
||||
const container = document.querySelector(selector);
|
||||
if (container) {
|
||||
container.innerHTML = "";
|
||||
for (const location of locations) {
|
||||
const element = new WeatherLocationElement();
|
||||
element.setAttribute("location", JSON.stringify(location));
|
||||
element.setAttribute("removable", "");
|
||||
container.appendChild(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WeatherLocationElement extends HTMLElement {
|
||||
static observedAttributes = ["location", "removable"];
|
||||
|
||||
location: Location | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.location = JSON.parse(this.getAttribute("location") || "{}");
|
||||
this.innerHTML = `
|
||||
<a href="${this.location?.provider}/${this.location?.id}"
|
||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||
<span>
|
||||
<span class="fi fi-${this.location?.country_code} me-1"></span>
|
||||
<span class="text-primary">${this.location?.name}</span>
|
||||
<span class="small ms-1 text-secondary">
|
||||
${this.location?.country}, ${this.location?.district}, ${this.location?.subdistrict}
|
||||
</span>
|
||||
</span>
|
||||
<span>
|
||||
<span class="badge text-bg-secondary">${this.location?.provider}</span>
|
||||
<span class="text-danger ms-2" data-remove>
|
||||
✕
|
||||
</span>
|
||||
</span>
|
||||
</a>`;
|
||||
this.classList = 'list-group-item list-group-item-action';
|
||||
this.addEventListener("click", this.handleClick);
|
||||
if (this.hasAttribute("removable")) {
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
||||
} else {
|
||||
this.querySelector("[data-remove]")?.remove();
|
||||
}
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this.removeEventListener("click", this.handleClick);
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
||||
}
|
||||
|
||||
handleClick(event: MouseEvent) {
|
||||
if (this.location) {
|
||||
window.weatherLocationStorage.add(this.location);
|
||||
}
|
||||
}
|
||||
|
||||
handleRemoveClick(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (this.location) {
|
||||
window.weatherLocationStorage.remove(this.location.id);
|
||||
}
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("weather-location", WeatherLocationElement);
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
weatherLocationStorage: WeatherLocationStorage;
|
||||
weatherLocationManager: WeatherLocationManager;
|
||||
}
|
||||
}
|
||||
|
||||
window.weatherLocationStorage = new WeatherLocationStorage();
|
||||
window.weatherLocationManager = new WeatherLocationManager();
|
||||
@@ -0,0 +1,33 @@
|
||||
import { ListElement, ListItemElement } from "../base/component";
|
||||
import { Location } from "./model";
|
||||
|
||||
export class WheatherLocationItem extends ListItemElement<Location> {
|
||||
getHref(value: Location): string {
|
||||
return `${value.provider}/${value.id}`;
|
||||
}
|
||||
|
||||
getContent(value: Location): string {
|
||||
return `
|
||||
<span class="badge me-1 text-bg-secondary">${value.provider}</span>
|
||||
<span class="fi fi-${value.country_code} me-1"></span>
|
||||
<span class="text-primary">${value.name}</span>
|
||||
<span class="small ms-1 text-secondary">
|
||||
${value.country}, ${value.district}, ${value.subdistrict}
|
||||
</span>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
export class WeatherLocationList extends ListElement<Location, WheatherLocationItem> {
|
||||
childType = WheatherLocationItem;
|
||||
|
||||
public onRefresh(): void {
|
||||
super.onRefresh();
|
||||
if (!this.value?.length) {
|
||||
this.prepend("Empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("weather-location-item", WheatherLocationItem);
|
||||
customElements.define("weather-location-list", WeatherLocationList);
|
||||
@@ -0,0 +1,11 @@
|
||||
import { DataManager } from "../base/manager";
|
||||
import { WeatherLocationList } from "./component";
|
||||
import { Location } from "./model";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
weatherLocationManager: DataManager<Location>;
|
||||
}
|
||||
}
|
||||
|
||||
window.weatherLocationManager = new DataManager(WeatherLocationList, "weather:locations", "#storedLocations");
|
||||
@@ -0,0 +1,9 @@
|
||||
export interface Location {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
country_code: string;
|
||||
country?: string;
|
||||
district?: string;
|
||||
subdistrict?: string;
|
||||
}
|
||||
Vendored
+3
@@ -45,4 +45,7 @@
|
||||
&.bi-file-earmark-text {
|
||||
mask-image: url(bootstrap-icons/icons/file-earmark-text.svg);
|
||||
}
|
||||
&.bi-cast {
|
||||
mask-image: url(bootstrap-icons/icons/cast.svg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ from pathlib import Path
|
||||
from typing import TypeVar
|
||||
|
||||
from gallery.sketch.api import Api, S
|
||||
from gallery.sketch.source import DEFAULT_REQUEST_MODE, ApiSource, RequestMode
|
||||
from gallery.sketch.source import ApiSource
|
||||
|
||||
|
||||
class MockSource(ApiSource):
|
||||
@@ -12,7 +12,8 @@ class MockSource(ApiSource):
|
||||
self._path = path
|
||||
self._mapping = mapping
|
||||
|
||||
async def request(self, endpoint: str, /, mode: RequestMode = DEFAULT_REQUEST_MODE) -> str:
|
||||
# pylint:disable=arguments-differ,unused-argument
|
||||
async def request(self, endpoint: str, /, **kwargs) -> str:
|
||||
for pattern, filename in self._mapping.items():
|
||||
if pattern in endpoint:
|
||||
return (self._path / filename).read_text()
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
from os import environ
|
||||
|
||||
from aiocache import caches
|
||||
|
||||
import gallery # pylint:disable=unused-import
|
||||
|
||||
environ.setdefault("OPENWEATHER_KEY", "test")
|
||||
|
||||
caches.set_config(
|
||||
{
|
||||
"default": {
|
||||
"cache": "aiocache.SimpleMemoryCache",
|
||||
"serializer": {"class": "aiocache.serializers.PickleSerializer"},
|
||||
},
|
||||
"redis": {
|
||||
"cache": "aiocache.SimpleMemoryCache",
|
||||
"serializer": {"class": "aiocache.serializers.PickleSerializer"},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from pathlib import Path
|
||||
|
||||
from tests.common.mock import MockSource
|
||||
|
||||
RP5_MOCK_SOURCE = MockSource(
|
||||
Path(__file__).parent,
|
||||
{
|
||||
"search": "search.html",
|
||||
"test": "weather.html",
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,379 @@
|
||||
<html lang="ru" xmlns="http://www.w3.org/1999/xhtml"><head>
|
||||
<title>Результат поиска</title>
|
||||
<script type="application/ld+json">
|
||||
{
|
||||
"dateModified": "2026-09-01"
|
||||
}
|
||||
</script>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<meta name="description" content="Результат поиска. Результаты поиска.">
|
||||
<meta name="keywords" content="результат поиска, результаты поиска">
|
||||
<meta name="viewport" content="height=device-height, user-scalable=yes">
|
||||
<meta name="referrer" content="origin">
|
||||
<meta name="msapplication-config" content="/browserconfig.xml">
|
||||
<meta name="msapplication-TileColor" content="#005eb5">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_57x57.png" sizes="57x57">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_60x60.png" sizes="60x60">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_72x72.png" sizes="72x72">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_76x76.png" sizes="76x76">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_114x114.png" sizes="114x114">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_120x120.png" sizes="120x120">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_152x152.png" sizes="152x152">
|
||||
<link rel="apple-touch-icon" type="image/png" href="/images/favicons/ios/ic_ios_180x180.png" sizes="180x180">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/192x192.png" sizes="192x192">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/144x144.png" sizes="144x144">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/128x128.png" sizes="128x128">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/96x96.png" sizes="96x96">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/72x72.png" sizes="72x72">
|
||||
<link rel="shortcut icon" type="image/png" href="/images/favicons/android_2/48x48.png" sizes="48x48">
|
||||
|
||||
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
|
||||
<link rel="stylesheet" href="/css/mainStyle.css?q=1.0182" type="text/css">
|
||||
<link rel="stylesheet" href="/css/sprites.css?q=1.0182" type="text/css">
|
||||
<link rel="stylesheet" href="/css/jquery.autocomplete.css?q=1.0182" type="text/css">
|
||||
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&key=AIzaSyAvCiizuGON7hMCTMa79Zf7d_4k56cCJ_0"></script>
|
||||
<style media="print">
|
||||
</style>
|
||||
|
||||
<script type="text/javascript" src="/scripts/jquery-1.6.4.min.js"></script>
|
||||
<script type="text/javascript" src="/scripts/jquery.autocomplete.01.cr.js"></script>
|
||||
<script type="text/javascript" src="/scripts/browser.js"></script>
|
||||
<script type="text/javascript" src="/scripts/googlemap.js"></script>
|
||||
<script type="text/javascript" src="/scripts/main.022.cr.js?q=1.052"></script>
|
||||
<script type="text/javascript" src="/scripts/rp5hint.js"></script>
|
||||
<script type="text/javascript" src="/scripts/rcs.js"></script>
|
||||
<script type="text/javascript" src="/scripts/infobubble.001.js"></script>
|
||||
<script type="text/javascript" src="/scripts/maps.008.js"></script>
|
||||
<script type="text/javascript" src="/scripts/geo_vidget.js"></script>
|
||||
<!--<script type="text/javascript" src="//static.criteo.net/js/ld/publishertag.js"></script>-->
|
||||
<script>window.yaContextCb=window.yaContextCb||[]</script>
|
||||
<script src="https://yandex.ru/ads/system/context.js" [^]="" async=""></script><script type="text/javascript" language="JavaScript">cRole = ""; langId = 2; cSearch = "Название города или села";</script>
|
||||
<script type="text/javascript">
|
||||
// локальное время
|
||||
var gmtAdd = 10800000;
|
||||
var sumHour = 0;
|
||||
var cLocalTime = 'Местное время';
|
||||
// установки времени
|
||||
var Dphp = 1788293743;
|
||||
var gmtServ = 0;
|
||||
var Djs = Math.round(((D=new Date()).getTime()-D.getTimezoneOffset()*60000)/1000);
|
||||
// дельта во времени в сек.
|
||||
var tDelta = Dphp - Djs;
|
||||
</script>
|
||||
<style>
|
||||
#h8c34381aaf77ce0fb634a3b6884f16046b9fd201 {display: inline-block; width: 126px; margin: 79px 0 0 8px;}
|
||||
#ha8e4d0cfbc1ac49c913e422d81ab257c227f18a7 {clear: both;}
|
||||
@media print {
|
||||
#ha8e4d0cfbc1ac49c913e422d81ab257c227f18a7 {margin-top: 20px; background: #fff;}
|
||||
#hf420d4b9b1efdbbe6ef03d062299d020ccae86d6 {display: none !important;}
|
||||
}
|
||||
#hf420d4b9b1efdbbe6ef03d062299d020ccae86d6 {float: left; font-size: 12px; display: inline; margin: 5px 0 11px 0;}
|
||||
</style>
|
||||
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/66/2d/common.js"></script><script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/66/2d/util.js"></script></head>
|
||||
<body>
|
||||
<div id="back-wrapper" style="width: 1670px; height: 688px;"></div>
|
||||
<div id="message-outside" class="message-outside">
|
||||
<div id="message-outside-content" class="message-outside-content"></div>
|
||||
<div class="button-container">
|
||||
<input type="button" class="button-blue" value="OK" onclick="displayBlocks('#message-outside','','',true); return false;">
|
||||
</div>
|
||||
</div>
|
||||
<div id="map-screen-container">
|
||||
<div id="map-screen-close"></div>
|
||||
<div id="map-screen"></div>
|
||||
</div>
|
||||
|
||||
<div id="wrapper">
|
||||
<div id="header-wrapper" class="noprint">
|
||||
<div id="header">
|
||||
<table>
|
||||
<tbody><tr>
|
||||
<td rowspan="3" style="width: 180px;" class="td-top">
|
||||
<div id="logo">
|
||||
<a href="/Погода_в_мире" title=""><img src="/images/ru/logo.png" title="" alt="Погода_в_мире" style="margin: 4px 0 0 -17px"></a>
|
||||
</div>
|
||||
</td>
|
||||
<td class="td-top">
|
||||
<div id="topMenuContent" style="text-align: left">
|
||||
<a id="mobile_link" class="pointer" onclick="getUrl('http://m.rp5.ru/Погода_в_Змиевке,_Луганская_Народная_Республика', 'desktop'); return false;">Мобильная версия</a>|
|
||||
<a href="/Погода_в_мире">Главная</a>|
|
||||
<a href="/docs/about/ru">О сайте</a>|
|
||||
<a href="/docs/faq/ru">Частые вопросы (FAQ)</a>|
|
||||
<a href="/docs/contact/ru">Контакты</a>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="height: 31px; vertical-align: middle"><div id="countries"><a href="/Погода_в_Беларуси">Беларусь</a> <a href="/Погода_в_Литве">Литва</a> <a href="/Погода_в_России">Россия</a> <a href="/Погода_в_Украине">Украина</a> <a href="/Погода_в_мире">Все страны</a></div>
|
||||
<div class="rightNavi noprint">
|
||||
<div class="intoNavi rss">
|
||||
<span class="verticalBottom" id="RSS_top">RSS <a href="http://rp5.ru/rss/24530/ru" id="rss_link"><div class="iconrss"></div></a> </span>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding-top: 7px; vertical-align: top">
|
||||
<div id="telephone" class="buttonSummary" style="margin: 2px 2px 0 0;" onclick="getUrl('http://m.rp5.ru/Погода_в_Змиевке,_Луганская_Народная_Республика', 'desktop'); return false;">
|
||||
<table><tbody><tr><td style="height: 35px; padding: 0 34px 0 7px; vertical-align: middle; ">
|
||||
<a class="inDd2Summary inDd2"><span>Мобильная версия</span></a>
|
||||
</td></tr></tbody></table>
|
||||
</div>
|
||||
<div id="informerC" class="buttonSummary">
|
||||
<div id="appButton" class="inDdSummary" style="font-size: 12px;">
|
||||
<table><tbody><tr><td style="height: 35px; vertical-align: middle">Приложения</td></tr></tbody></table>
|
||||
</div>
|
||||
<div id="appMenu" style="display: none;">
|
||||
<ul>
|
||||
<li onclick="top.location.href='/docs/informer/ru'">Информеры</li>
|
||||
<li onclick="top.location.href='/docs/xml/ru?id=24530'">XML</li>
|
||||
<li onclick="top.location.href='/docs/sgate/ru'">API частных метеостанций</li>
|
||||
<div style="margin: 2px 0 3px 0; border-top: 1px dashed #a7a7a7"></div>
|
||||
<li onclick="top.location.href='/docs/gadget/ru'">Гаджет для WINDOWS</li>
|
||||
<div style="margin: 2px 0 3px 0; border-top: 1px dashed #a7a7a7"></div>
|
||||
<li onclick="window.open('https://play.google.com/store/apps/details?id=ru.rp5.rp5weatherhorizontal', '_blank')">Приложение для Android</li>
|
||||
<!--<li onclick="top.location.href='/wduck/source/new/app-release-v22.apk'">*.apk-файл</li>-->
|
||||
<div style="margin: 2px 0 3px 0; border-top: 1px dashed #a7a7a7;"></div>
|
||||
<li onclick="top.location.href='/Дневник_погоды_в_Сватово'">Дневник погоды для школьников</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ddListSummary">
|
||||
<span>
|
||||
<div class="ddList2">
|
||||
<div id="unitsButton" class="inDdSummary">
|
||||
<table><tbody><tr><td style="height: 35px; vertical-align: middle">Единицы измерений</td></tr></tbody></table>
|
||||
</div>
|
||||
<div id="unitsMenu" style="text-align: left; display: none;">
|
||||
<form name="fValues" action="">
|
||||
<table id="tValues" style="vertical-align: top;">
|
||||
<tbody><tr>
|
||||
<td>
|
||||
<div>Облачность:</div>
|
||||
<div>
|
||||
<input type="radio" id="r_cc1" class="radioUnits" name="r_cc" title="проценты (%)" onclick="if(this.checked) {chValue('cc',0,0);}" checked="checked">
|
||||
<label for="r_cc1">проценты (%)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_cc2" class="radioUnits" name="r_cc" title="1/10" onclick="if(this.checked) {chValue('cc',1,0);}">
|
||||
<label for="r_cc2">1/10</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_cc3" class="radioUnits" name="r_cc" title="окты" onclick="if(this.checked) {chValue('cc',2,0);}">
|
||||
<label for="r_cc3">окты</label>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div>Осадки:</div>
|
||||
<div>
|
||||
<input type="radio" id="r_pr1" class="radioUnits" name="r_pr" title="миллиметры (мм)" onclick="if(this.checked) {chValue('pr',0,0);}" checked="checked">
|
||||
<label for="r_pr1">миллиметры (мм) / сантиметры (см)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_pr2" class="radioUnits" name="r_pr" title="дюймы" onclick="if(this.checked) {chValue('pr',1,0);}">
|
||||
<label for="r_pr2">дюймы</label>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div>Температура:</div>
|
||||
<div>
|
||||
<input type="radio" id="r_t1" class="radioUnits" name="r_t" title="градусы Цельсия (°C)" onclick="if(this.checked) {chValue('t',0,0);}" checked="checked">
|
||||
<label for="r_t1">градусы Цельсия (°C)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_t2" class="radioUnits" name="r_t" title="градусы Фаренгейта (°F)" onclick="if(this.checked) {chValue('t',1,0);}">
|
||||
<label for="r_t2">градусы Фаренгейта (°F)</label>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<div>Давление:</div>
|
||||
<div>
|
||||
<input type="radio" id="r_p1" class="radioUnits" name="r_p" title="миллиметры ртутного столба (мм рт. ст.)" onclick="if(this.checked) {chValue('p',0,0);}" checked="checked">
|
||||
<label for="r_p1">миллиметры ртутного столба (мм рт. ст.)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_p2" class="radioUnits" name="r_p" title="дюймы ртутного столба (дюймы рт. ст.)" onclick="if(this.checked) {chValue('p',1,0);}">
|
||||
<label for="r_p2">дюймы ртутного столба (дюймы рт. ст.)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_p3" class="radioUnits" name="r_p" title="миллибары (мбар)" onclick="if(this.checked) {chValue('p',2,0);}">
|
||||
<label for="r_p3">миллибары (мбар)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_p4" class="radioUnits" name="r_p" title="гектопаскали (гПа)" onclick="if(this.checked) {chValue('p',3,0);}">
|
||||
<label for="r_p3">гектопаскали (гПа)</label>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div>Ветер:</div>
|
||||
<div>
|
||||
<input type="radio" id="r_wv1" class="radioUnits" name="r_wv" title="метры в секунду (м/с)" onclick="if(this.checked) {chValue('wv',0,0);}" checked="checked">
|
||||
<label for="r_wv1">метры в секунду (м/с)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_wv2" class="radioUnits" name="r_wv" title="километры в час (км/ч)" onclick="if(this.checked) {chValue('wv',1,0);}">
|
||||
<label for="r_wv2">километры в час (км/ч)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_wv3" class="radioUnits" name="r_wv" title="мили в час (мили/час)" onclick="if(this.checked) {chValue('wv',2,0);}">
|
||||
<label for="r_wv3">мили в час (мили/час)</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_wv4" class="radioUnits" name="r_wv" title="узлы" onclick="if(this.checked) {chValue('wv',3,0);}">
|
||||
<label for="r_wv4">узлы</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_wv5" class="radioUnits" name="r_wv" title="Бфрт (Бфрт)" onclick="if(this.checked) {chValue('wv',4,0);}">
|
||||
<label for="r_wv5">баллы Бофорта (Бфрт)</label>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="3">
|
||||
<div style="margin-bottom: 9px; border-top: 1px dashed #a7a7a7"></div>
|
||||
<div>Только для страниц «Архив погоды»</div>
|
||||
<div>Горизонтальная дальность видимости (VV):</div>
|
||||
<div>
|
||||
<input type="radio" id="r_vv1" class="radioUnits" name="r_vv" title="километры" onclick="if(this.checked) {chValue('vv',0,0);}" checked="checked">
|
||||
<label for="r_vv1">километры</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_vv2" class="radioUnits" name="r_vv" title="мили" onclick="if(this.checked) {chValue('vv',1,0);}">
|
||||
<label for="r_vv2">мили</label>
|
||||
</div>
|
||||
<div>Высота основания самых низких облаков (H):</div>
|
||||
<div>
|
||||
<input type="radio" id="r_h1" class="radioUnits" name="r_h" title="метры" onclick="if(this.checked) {chValue('h',0,0);}" checked="checked">
|
||||
<label for="r_h1">метры</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_h2" class="radioUnits" name="r_h" title="футы" onclick="if(this.checked) {chValue('h',1,0);}">
|
||||
<label for="r_h2">футы</label>
|
||||
</div>
|
||||
<div>Высота снежного покрова (sss):</div>
|
||||
<div>
|
||||
<input type="radio" id="r_sss1" class="radioUnits" name="r_sss" title="сантиметры" onclick="if(this.checked) {chValue('sss',0,0);}" checked="checked">
|
||||
<label for="r_sss1">сантиметры</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="radio" id="r_sss2" class="radioUnits" name="r_sss" title="дюймы" onclick="if(this.checked) {chValue('sss',1,0);}">
|
||||
<label for="r_sss2">дюймы</label>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody></table>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</span>
|
||||
</div>
|
||||
<div class="ddListSummary1">
|
||||
<span>
|
||||
<div class="ddList1">
|
||||
<div id="langButton" class="inDdSummary">
|
||||
<table><tbody><tr><td style="height: 35px; vertical-align: middle;">Language</td></tr></tbody></table>
|
||||
</div>
|
||||
<div id="langMenu" style="display: none;">
|
||||
<ul></ul>
|
||||
</div>
|
||||
</div>
|
||||
</span>
|
||||
</div>
|
||||
<div id="search">
|
||||
<form name="fsearch" autocomplete="off" method="post" action="/docs/search/ru" onsubmit="searchConfirm();">
|
||||
<input type="text" name="searchStr" id="searchStr" class="search ac_input" onfocus="clearTxt();" onblur="setTxt();" value="змиёвка" autocomplete="off">
|
||||
<input type="button" id="searchButton" class="searchButton" onclick="searchConfirm();" value="">
|
||||
<script type="text/javascript">setTxt();</script>
|
||||
</form>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody></table>
|
||||
</div>
|
||||
</div>
|
||||
<div id="content-wrapper">
|
||||
<div id="content">
|
||||
<div id="hf420d4b9b1efdbbe6ef03d062299d020ccae86d6">
|
||||
<div class="intoLeftNavi">
|
||||
<span class="verticalBottom">
|
||||
<a href="/Погода_в_мире"><nobr>Все страны</nobr></a> <img src="/images/iconArrow.png" alt="" title=""> <a href="/Погода_в_России"><nobr>Россия</nobr></a> <img src="/images/iconArrow.png" alt="" title="">
|
||||
<a href="/Погода_в_Луганской_Народной_Республике"><nobr>Луганская Народная Республика</nobr></a> <img src="/images/iconArrow.png" alt="" title="">
|
||||
<a href="/Погода_в_Сватовском_районе"><nobr>Сватовский район</nobr></a> <img src="/images/iconArrow.png" alt="" title="">
|
||||
<a href="/Погода_в_Змиевке,_Луганская_Народная_Республика"><nobr>Змиевка</nobr></a>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ha8e4d0cfbc1ac49c913e422d81ab257c227f18a7"><div id="leftNavi" class="pageTitle"> <span class="verticalBottom"><h1>Результат поиска</h1></span></div><div class="clear"></div> <p class="stinfo"><b>Найдено населенных пунктов:</b> 3</p> <table cellspacing="0" cellpadding="0" class="searchResults"> <tbody><tr class="header"> <td> <div class="ImgLeft"></div> <div class="searchPointTitle"> <span class="innerTextHead">Населенный пункт</span> <div> </div></div></td> <td> <div class="searchTitles"> <span class="innerTextHead">Страна</span> </div> </td> <td> <div class="searchTitles"> <span class="innerTextHead">Регион</span> </div> </td> <td style="border-right: none;"> <div class="ImgRight"></div> <div class="searchTitles"> <span class="innerTextHead">Район</span> </div> </td> </tr><tr class="srow0"> <td class="resBorder"><span class="innerTextResult"><a href="/Погода_в_Змиевке,_Луганская_Народная_Республика">Змиевка</a></span></td> <td class="resBorder"><span class="innerTextResult">Россия</span></td> <td class="resBorder"><span class="innerTextResult">Луганская Народная Республика</span></td> <td><span class="innerTextResult">Сватовский район</span></td> </tr><tr class="srow1"> <td class="resBorder"><span class="innerTextResult"><a href="/Погода_в_Змиевке,_Орловская_область">Змиевка</a></span></td> <td class="resBorder"><span class="innerTextResult">Россия</span></td> <td class="resBorder"><span class="innerTextResult">Орловская область</span></td> <td><span class="innerTextResult">Свердловский район</span></td> </tr><tr class="srow0"> <td class="resBorder"><span class="innerTextResult"><a href="/Погода_в_Змиевке,_Херсонская_область">Змиевка</a></span></td> <td class="resBorder"><span class="innerTextResult">Россия</span></td> <td class="resBorder"><span class="innerTextResult">Херсонская область</span></td> <td><span class="innerTextResult">Бериславский район</span></td> </tr> </tbody></table>
|
||||
</div>
|
||||
</div>
|
||||
<div class="clear"></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<div id="footer-wrapper" class="noprint">
|
||||
<div id="footer">
|
||||
|
||||
<div id="footer-info">
|
||||
<div class="footerMenu">
|
||||
<div class="footer_title"><b style="font-size: 10pt;">Показаны ранее:</b></div><span class="Ajax-PointID" id="6318">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+19</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+66</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Орле,_Орловская_область">Орел</a></div><br><span class="Ajax-PointID" id="24530">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Змиевке,_Луганская_Народная_Республика">Змиевка</a></div><br>
|
||||
</div>
|
||||
<div class="footerMenu">
|
||||
<div class="footer_title"><b style="font-size: 10pt;">Ближайшие центры:</b></div><span class="Ajax-PointID" id="531">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Сватово,_Луганская_Народная_Республика">Сватово</a></div><br><span class="Ajax-PointID" id="720">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+15</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+59</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Боровой,_Боровский_район">Боровая</a></div><br><span class="Ajax-PointID" id="735">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Купянске">Купянск</a></div><br><span class="Ajax-PointID" id="518">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Кременной,_Россия">Кременная</a></div><br>
|
||||
</div>
|
||||
<div class="footerMenu">
|
||||
<div class="footer_title"><b style="font-size: 10pt;">Популярные:</b></div><span class="Ajax-PointID" id="488">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+13</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+55</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Беловодске">Беловодск</a></div><br><span class="Ajax-PointID" id="520">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Луганске,_Луганская_Народная_Республика">Луганск</a></div><br><span class="Ajax-PointID" id="529">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+21</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+70</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Ровеньках,_Луганская_Народная_Республика">Ровеньки</a></div><br><span class="Ajax-PointID" id="536">
|
||||
<div class="MainTemp">
|
||||
<span class="t_0" style="display: inline;"><span class="red">+17</span></span>
|
||||
<span class="t_1" style="display: none;"><span class="red">+63</span></span>
|
||||
</div></span> <div class="footer_links"><a href="/Погода_в_Старобельске">Старобельск</a></div><br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="footer-content">
|
||||
<div id="footer-copyright">Copyright © ООО «Расписание Погоды», 2004-2026</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- old code -->
|
||||
|
||||
|
||||
|
||||
|
||||
</body></html>
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,11 @@
|
||||
from pathlib import Path
|
||||
|
||||
from tests.common.mock import MockSource
|
||||
|
||||
YANDEXWEATHER_MOCK_SOURCE = MockSource(
|
||||
Path(__file__).parent,
|
||||
{
|
||||
"details": "details.html",
|
||||
"suggest": "suggest.json",
|
||||
},
|
||||
)
|
||||
File diff suppressed because one or more lines are too long
@@ -2,22 +2,22 @@ import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from gallery.painting.matchtv.api import MatchTvApi
|
||||
from gallery.painting.matchtv.api import MatchTVApi
|
||||
from tests.common.mock import create_mock_api
|
||||
from tests.data.matchtv import MATCHTV_MOCK_SOURCE
|
||||
|
||||
|
||||
@pytest.fixture(name="matchtv_api", scope="module")
|
||||
def matchtv_api_fixture() -> MatchTvApi:
|
||||
return create_mock_api(MatchTvApi, MATCHTV_MOCK_SOURCE)
|
||||
def matchtv_api_fixture() -> MatchTVApi:
|
||||
return create_mock_api(MatchTVApi, MATCHTV_MOCK_SOURCE)
|
||||
|
||||
|
||||
async def test_search(matchtv_api: MatchTvApi):
|
||||
async def test_search(matchtv_api: MatchTVApi):
|
||||
result = await matchtv_api.find_channels("матч")
|
||||
assert len(result) == 6
|
||||
|
||||
|
||||
async def test_channel(matchtv_api: MatchTvApi):
|
||||
async def test_channel(matchtv_api: MatchTVApi):
|
||||
result = await matchtv_api.get_schedule("test", datetime.date.today())
|
||||
assert result is not None
|
||||
assert len(result.values) > 0
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from gallery.painting.rp5.api import RP5WeatherApi
|
||||
from tests.common.mock import create_mock_api
|
||||
from tests.data.rp5 import RP5_MOCK_SOURCE
|
||||
|
||||
|
||||
@pytest.fixture(name="rp5_api", scope="module")
|
||||
def rp5_api_fixture() -> RP5WeatherApi:
|
||||
return create_mock_api(RP5WeatherApi, RP5_MOCK_SOURCE)
|
||||
|
||||
|
||||
async def test_search(rp5_api: RP5WeatherApi):
|
||||
result = await rp5_api.find_locations("test")
|
||||
assert len(result) == 3
|
||||
|
||||
|
||||
async def test_day(rp5_api: RP5WeatherApi):
|
||||
result = await rp5_api.get_day("test", datetime.date(2026, 9, 11))
|
||||
assert len(result.values) == 4
|
||||
|
||||
|
||||
async def test_days(rp5_api: RP5WeatherApi):
|
||||
result = await rp5_api.get_days("test", 10)
|
||||
assert len(result.values) == 8
|
||||
@@ -2,22 +2,22 @@ import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from gallery.painting.yandextv.api import YandexTvApi
|
||||
from gallery.painting.yandextv.api import YandexTVApi
|
||||
from tests.common.mock import create_mock_api
|
||||
from tests.data.yandextv import YANDEXTV_MOCK_SOURCE
|
||||
|
||||
|
||||
@pytest.fixture(name="yandextv_api", scope="module")
|
||||
def yandextv_api_fixture() -> YandexTvApi:
|
||||
return create_mock_api(YandexTvApi, YANDEXTV_MOCK_SOURCE)
|
||||
def yandextv_api_fixture() -> YandexTVApi:
|
||||
return create_mock_api(YandexTVApi, YANDEXTV_MOCK_SOURCE)
|
||||
|
||||
|
||||
async def test_search(yandextv_api: YandexTvApi):
|
||||
async def test_search(yandextv_api: YandexTVApi):
|
||||
result = await yandextv_api.find_channels("матч")
|
||||
assert len(result) == 4
|
||||
|
||||
|
||||
async def test_channel(yandextv_api: YandexTvApi):
|
||||
async def test_channel(yandextv_api: YandexTVApi):
|
||||
result = await yandextv_api.get_schedule("test", datetime.date.today())
|
||||
assert result is not None
|
||||
assert len(result.values) > 0
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from gallery.painting.yandexweather.api import YandexWeatherApi
|
||||
from tests.common.mock import create_mock_api
|
||||
from tests.data.yandexweather import YANDEXWEATHER_MOCK_SOURCE
|
||||
|
||||
|
||||
@pytest.fixture(name="yandexweather_api", scope="module")
|
||||
def yandexweather_api_fixture() -> YandexWeatherApi:
|
||||
return create_mock_api(YandexWeatherApi, YANDEXWEATHER_MOCK_SOURCE)
|
||||
|
||||
|
||||
async def test_search(yandexweather_api: YandexWeatherApi):
|
||||
result = await yandexweather_api.find_locations("test")
|
||||
assert len(result) == 5
|
||||
|
||||
|
||||
async def test_day(yandexweather_api: YandexWeatherApi):
|
||||
result = await yandexweather_api.get_day("test", datetime.date(2026, 9, 1))
|
||||
assert len(result.values) == 4
|
||||
|
||||
|
||||
async def test_days(yandexweather_api: YandexWeatherApi):
|
||||
result = await yandexweather_api.get_days("test", 10)
|
||||
assert len(result.values) == 10
|
||||
Reference in New Issue
Block a user