Ejemplo n.º 1
0
def _fetch_flight_data(
    from_date: datetime, to_date: datetime, iata_code: str, departing: bool
) -> FlightList:
    """
    Fetch data on flights to/from an Icelandic airport (given with its IATA code)
    between from_date and to_date from Isavia's JSON API.
    """
    date_format = "%Y-%m-%d %H:%M"
    from_date_str = from_date.strftime(date_format)
    to_date_str = to_date.strftime(date_format)

    # Insert GET parameters
    url: str = _ISAVIA_FLIGHTS_URL.format(
        iata_code.upper(), from_date_str, to_date_str, str(departing).lower()
    )

    res = query_json_api(url)

    # Verify result was successful
    if not res or "Success" not in res or not res["Success"] or "Items" not in res:
        return []

    # Add result to cache
    _FLIGHT_CACHE[departing] = res["Items"]
    return res["Items"]
Ejemplo n.º 2
0
def _fetch_exchange_rates() -> Optional[Dict]:
    """ Fetch exchange rate data from apis.is and cache it. """
    res = query_json_api(_CURR_API_URL)
    if not res or "results" not in res:
        logging.warning("Unable to fetch exchange rate data from {0}".format(
            _CURR_API_URL))
        return None
    return {c["shortName"]: c["value"] for c in res["results"]}
Ejemplo n.º 3
0
def _get_news_data(max_items=8):
    """ Fetch news headline data from RÚV, preprocess it. """
    res = query_json_api(_NEWS_API)
    if not res or "nodes" not in res or not len(res["nodes"]):
        return None

    items = [{
        "title": i["node"]["title"],
        "intro": i["node"]["intro"]
    } for i in res["nodes"]]

    return items[:max_items]
Ejemplo n.º 4
0
def _get_petrol_station_data() -> Optional[List]:
    """ Fetch list of petrol stations w. prices from apis.is (Gasvaktin) """
    pd = query_json_api(_PETROL_API)
    if not pd or "results" not in pd:
        return None

    # Fix company names
    for s in pd["results"]:
        name = s.get("company", "")
        s["company"] = _COMPANY_NAME_FIXES.get(name, name)

    return pd["results"]
Ejemplo n.º 5
0
def _query_tv_schedule_api():
    """ Fetch current television schedule from API, or return cached copy. """
    global _CACHED_SCHEDULE
    global _LAST_FETCHED
    if (not _CACHED_SCHEDULE or not _LAST_FETCHED
            or _LAST_FETCHED.date() != datetime.today().date()):
        _CACHED_SCHEDULE = None
        sched = query_json_api(_RUV_SCHEDULE_API_ENDPOINT)
        if sched and "results" in sched and len(sched["results"]):
            _LAST_FETCHED = datetime.utcnow()
            _CACHED_SCHEDULE = sched["results"]
    return _CACHED_SCHEDULE
Ejemplo n.º 6
0
def _fetch_flight_info(from_date, to_date, ftype="arrivals"):
    """ Fetch flight data from Isavia's JSON API. """
    assert ftype in ("arrivals", "departures")

    fmt = "%Y-%m-%d %H:%M"
    from_str = from_date.strftime(fmt)
    to_str = to_date.isoformat(fmt)
    arr = "true" if ftype == "arrivals" else "false"
    airport = "KEF"

    url = _ISAVIA_URL.format(airport, from_str, to_str, arr)

    res = query_json_api(url)

    # Verify sanity of result
    if not res or "Success" not in res or not res[
            "Success"] or "Items" not in res:
        return None

    print(res)

    return res["Items"]
Ejemplo n.º 7
0
def _fetch_exchange_rates():
    """ Fetch exchange rate data from apis.is and cache it. """
    res = query_json_api(_CURR_API_URL)
    if not res or "results" not in res:
        return None
    return {c["shortName"]: c["value"] for c in res["results"]}
Ejemplo n.º 8
0
def _query_wiki_api(subject: str):
    """ Fetch JSON from Wikipedia API """
    url = _WIKI_API_URL.format(subject)
    return query_json_api(url)
Ejemplo n.º 9
0
def _query_owm_by_coords(lat: float, lon: float):
    d = query_json_api(_OWM_API_URL_BYLOC.format(lat, lon, _get_OWM_API_key()))
    return _postprocess_owm_data(d)
Ejemplo n.º 10
0
def _query_owm_by_name(city: str, country_code: Optional[str] = None):
    d = query_json_api(
        _OWM_API_URL_BYNAME.format(city, country_code or "",
                                   _get_OWM_API_key()))
    return _postprocess_owm_data(d)