<commit_before># -*- coding: utf-8 -*-

import requests
from datetime import datetime
from jobs import AbstractJob


class Nsb(AbstractJob):
    def __init__(self, conf):
        self.from_stop_id, self.from_location = conf["from"]
        self.to_stop_id, self.to_location = conf["to"]
        self.interval = conf["interval"]
        self.timeout = conf.get("timeout")

    def _parse(self, json):
        departures = []
        for tp in json["data"]["trip"]["tripPatterns"]:
            legs = tp["legs"]
            if len(legs) == 0:
                continue
            elif len(legs) > 1:
                raise ValueError("found {} legs, but expected 1".format(len(legs)))
            departure = datetime.fromisoformat(legs[0]["expectedStartTime"])
            arrival = datetime.fromisoformat(legs[0]["expectedEndTime"])
            duration = tp["duration"]
            departures.append(
                {
                    "departure": departure.isoformat(),
                    "arrival": arrival.isoformat(),
                    "duration": duration,
                }
            )

        return {
            "from": self.from_location,
            "to": self.to_location,
            "departures": departures,
        }

    def get(self):
        query = """
{
  trip(
    from: {
      place: "NSR:StopPlace:%d"
    },
    to: {
      place: "NSR:StopPlace:%d"
    },
    modes: {
      transportModes: {
        transportMode: rail
      }
    },
    searchWindow: 720,
    maximumTransfers: 1
  ) {
    tripPatterns {
      duration
      legs {
        expectedStartTime
        expectedEndTime
        line {
          id
          publicCode
        }
      }
    }
  }
}
"""
        data = {"query": query % (self.from_stop_id, self.to_stop_id)}
        url = "https://api.entur.io/journey-planner/v3/graphql"
        headers = {"ET-Client-Name": "github_mpolden-jarvis2"}
        r = requests.post(url, timeout=self.timeout, json=data, headers=headers)
        r.raise_for_status()
        return self._parse(r.json())<commit_msg>nsb: Do not limit number of transfers<commit_after># -*- coding: utf-8 -*-

import requests
from datetime import datetime
from jobs import AbstractJob


class Nsb(AbstractJob):
    def __init__(self, conf):
        self.from_stop_id, self.from_location = conf["from"]
        self.to_stop_id, self.to_location = conf["to"]
        self.interval = conf["interval"]
        self.timeout = conf.get("timeout")

    def _parse(self, json):
        departures = []
        for tp in json["data"]["trip"]["tripPatterns"]:
            legs = tp["legs"]
            if len(legs) == 0:
                continue
            departure = datetime.fromisoformat(legs[0]["expectedStartTime"])
            arrival = datetime.fromisoformat(legs[-1]["expectedEndTime"])
            duration = tp["duration"]
            departures.append(
                {
                    "departure": departure.isoformat(),
                    "arrival": arrival.isoformat(),
                    "duration": duration,
                }
            )

        return {
            "from": self.from_location,
            "to": self.to_location,
            "departures": departures,
        }

    def get(self):
        # Test query at https://api.entur.io/graphql-explorer/journey-planner-v3
        query = """
{
  trip(
    from: {
      place: "NSR:StopPlace:%d"
    },
    to: {
      place: "NSR:StopPlace:%d"
    },
    modes: {
      transportModes: {
        transportMode: rail
      }
    },
    searchWindow: 720
  ) {
    tripPatterns {
      duration
      legs {
        expectedStartTime
        expectedEndTime
        line {
          id
          publicCode
        }
      }
    }
  }
}
"""
        data = {"query": query % (self.from_stop_id, self.to_stop_id)}
        url = "https://api.entur.io/journey-planner/v3/graphql"
        headers = {"ET-Client-Name": "github_mpolden-jarvis2"}
        r = requests.post(url, timeout=self.timeout, json=data, headers=headers)
        r.raise_for_status()
        return self._parse(r.json())
{'fraction_non_alphanumeric': 0.1288823673269748, 'fraction_numerical': 0.005380288579114698, 'mean_word_length': 1.7899045020463846, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 3, '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': '5479417', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1231, 'n_words': 228}