infotbm/libs.py

30 lines
656 B
Python
Raw Normal View History

2017-11-18 16:40:49 +01:00
'''
Common libraries
'''
from json import loads as read_json
from urllib import request
from urllib.error import HTTPError
2017-11-18 16:40:49 +01:00
def get_data_from_json (url):
'''
gets data from json at url
'''
opener = request.build_opener ()
try:
return (read_json (opener.open (url).read ().decode ('utf8')))
except HTTPError:
2017-11-21 21:07:08 +01:00
return (None)
2017-11-21 21:07:08 +01:00
def hms2seconds (hhmmss):
'''
Convert H:M:S string to time in seconds
'''
try:
cut_string = hhmmss.split (':')
cut_time = (int (cut_string [0]), int (cut_string [1]), int (cut_string [2]))
return (3600 * cut_time [0] + 60 * cut_time [1] + cut_time [2])
2018-02-22 21:53:02 +01:00
except (IndexError, ValueError, TypeError):
2017-11-21 21:07:08 +01:00
return (0)