Skip to content

Commit

Permalink
feature: add first log try
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Jul 6, 2019
1 parent 05af973 commit f40a42d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ settings.ini
htmlcov/
\~/
settings.env
log
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: python
cache: pip
python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"
install:
- pip install -r requirements-dev.txt
- pip install coveralls
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ requests==2.20.0
lxml==4.2.1
luma.led_matrix==1.0.5
pyyaml==3.13
loguru==0.3.0
7 changes: 7 additions & 0 deletions rerwatcher/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env python3
# coding: utf-8
from loguru import logger

from . import build_rer_watcher

logger.add("log/file_{time}.log", rotation="12:00")

# RERWatcher entrypoint.
try:
logger.info("Building RERWatcher app...")
rer_watcher = build_rer_watcher()
logger.info("RERWatcher app built.")
logger.info("Starting RERWatcher app...")
rer_watcher.start()
except KeyboardInterrupt:
logger.info("RERWatcher app stopped.")
pass
59 changes: 37 additions & 22 deletions rerwatcher/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from string import Template

import requests
from loguru import logger
from lxml import etree
from requests import RequestException
from requests.auth import HTTPBasicAuth


Expand Down Expand Up @@ -54,6 +56,26 @@ def format_timedelta(timedelta):
return time


class TimeTable:
"""Timetable
Encapsulate timetable information.
Attributes:
miss (str): the train code.
time: the departure time.
"""

def __init__(self, miss, time):
self.miss = miss
self.time = time

def text(self):
"""Return the timetable to nice format."""
return f"{self.miss}: {self.time}"


class TransilienApi:
"""Translien api
Expand Down Expand Up @@ -96,8 +118,21 @@ def fetch_data(self):
Returns:
timetables (list<TimeTable>): the next departure timetable.
"""
response = requests.get(url=self._url, auth=self._auth)
timetables = self._get_timetables(response)
response = None
timetables = [TimeTable(miss='Error', time='')]

try:
logger.info(f"Fetching data from {self._url}.")
response = requests.get(url=self._url, auth=self._auth)
except RequestException:
logger.error(f"Fetching {self._url} failed.")

try:
if response:
logger.info(f"Formatting response.")
timetables = self._get_timetables(response)
except Exception:
logger.error(f"Formatting data failed.")

return timetables

Expand Down Expand Up @@ -125,23 +160,3 @@ def _get_timetables(self, response, limit=2):
timetables.append(timetable)

return timetables


class TimeTable:
"""Timetable
Encapsulate timetable information.
Attributes:
miss (str): the train code.
time: the departure time.
"""

def __init__(self, miss, time):
self.miss = miss
self.time = time

def text(self):
"""Return the timetable to nice format."""
return ("{miss}: {time}".format(miss=self.miss, time=self.time))

0 comments on commit f40a42d

Please sign in to comment.