<commit_before>from irc3 import asyncio
from irc3.plugins.command import command

import re
import json
import aiohttp

from datetime import datetime

_RE_FIX = "(,\\s*|\\s*-\\s*){component}|{component}((,\\s*)|\\s*-\\s*)"


def remove_component(targetlocation, component="Erfurt"):
    """
    Fix API locations like "Erfurt, Daberstedt" to just "Daberstedt"
    """
    return re.sub(_RE_FIX.format(component=component), "", targetlocation)


@command(permission="view")
@asyncio.coroutine
def station(bot, mask, target, args):
    """
    Show departures for station: 151213 (Leipziger Platz, Erfurt). The service
    URL is consumed by Erfurt Mobil (Android/iOS).

    A list of all known stations in the VMT area can be viewed here:
    https://evag-live.wla-backend.de/stations/latest.json

    %%station
    """
    url = "https://evag-live.wla-backend.de/node/v1/departures/151213"
    headers = {"Content-Type": "application/json"}
    with aiohttp.Timeout(10):
        with aiohttp.ClientSession(loop=bot.loop) as session:
            resp = yield from session.get(url, headers=headers)
            if resp.status != 200:
                bot.privmsg(target, "Error while retrieving traffic data.")
                raise Exception()
            body = yield from resp.read()

    try:
        body = json.loads(body.decode('utf-8'))
    except json.JSONDecodeError:
        bot.privmsg(target, "Error while retrieving traffic data.")

    try:
        data = []
        for departure in body.get("departures", []):
            delay = 0  # in seconds
            try:
                timestamp = departure.get("timestamp")
                timestamp_rt = departure.get("timestamp_rt", 0)
                delay = max(0, timestamp_rt - timestamp) // 60
            except ValueError:
                pass

            data.append({
                "type": "Tram" if departure["type"] == "Strab" else "Bus",
                "line": departure["line"],
                "target": remove_component(departure["targetLocation"]),
                "time": datetime.utcfromtimestamp(departure["timestamp"]),
                "delay": delay
            })
<commit_msg>use local timezone to format datetime<commit_after>from irc3 import asyncio
from irc3.plugins.command import command

import re
import json
import aiohttp

from datetime import datetime
from dateutil import tz

_RE_FIX = "(,\\s*|\\s*-\\s*){component}|{component}((,\\s*)|\\s*-\\s*)"


def remove_component(targetlocation, component="Erfurt"):
    """
    Fix API locations like "Erfurt, Daberstedt" to just "Daberstedt"
    """
    return re.sub(_RE_FIX.format(component=component), "", targetlocation)


@command(permission="view")
@asyncio.coroutine
def station(bot, mask, target, args):
    """
    Show departures for station: 151213 (Leipziger Platz, Erfurt). The service
    URL is consumed by Erfurt Mobil (Android/iOS).

    A list of all known stations in the VMT area can be viewed here:
    https://evag-live.wla-backend.de/stations/latest.json

    %%station
    """
    url = "https://evag-live.wla-backend.de/node/v1/departures/151213"
    headers = {"Content-Type": "application/json"}
    with aiohttp.Timeout(10):
        with aiohttp.ClientSession(loop=bot.loop) as session:
            resp = yield from session.get(url, headers=headers)
            if resp.status != 200:
                bot.privmsg(target, "Error while retrieving traffic data.")
                raise Exception()
            body = yield from resp.read()

    try:
        body = json.loads(body.decode('utf-8'))
    except json.JSONDecodeError:
        bot.privmsg(target, "Error while retrieving traffic data.")

    try:
        # use local timezone
        tzinfo = tz.tzlocal()

        data = []
        for departure in body.get("departures", []):
            delay = 0  # in seconds
            try:
                timestamp = departure.get("timestamp")
                timestamp_rt = departure.get("timestamp_rt", 0)
                delay = max(0, timestamp_rt - timestamp) // 60
            except ValueError:
                pass

            data.append({
                "type": "Tram" if departure["type"] == "Strab" else "Bus",
                "line": departure["line"],
                "target": remove_component(departure["targetLocation"]),
                "time": datetime.fromtimestamp(departure["timestamp"], tzinfo),
                "delay": delay
            })

{'fraction_non_alphanumeric': 0.1280144241604688, 'fraction_numerical': 0.011719630380887988, 'mean_word_length': 2.3774733637747336, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '188149', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1265, 'n_words': 329}