-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
63 lines (52 loc) · 1.61 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from itertools import count
import requests
import socket
import xmltodict
from loguru import logger
def get_xml_stream_as_json(xml_url, session=None):
if not session:
session = requests
try:
req = session.get(xml_url)
except requests.ConnectionError as e:
logger.error(f"URL: {xml_url}\nerror:\n{e}")
return None
except requests.Timeout as e:
logger.error(f"URL: {xml_url}\nerror:\n{e}")
return None
if req and not req.ok:
logger.debug(f"Failed to download: {xml_url}")
return None
return xmltodict.parse(req.text)
def parsed_time_to_seconds(human_time):
"""
# 12:44 => number
"""
if not human_time:
return None
time_parts = human_time.split(":")
def part_to_seconds(part, order): return int(part) * pow(60, order)
return sum(map(part_to_seconds, reversed(time_parts), count()))
def convert_str_to_number(x):
total_stars = 0
num_map = {'K': 1000, 'M': 1000000, 'B': 1000000000}
x = x.replace(",", ".")
if x.isdigit():
total_stars = int(x)
else:
if len(x) > 1:
total_stars = float(x[:-1]) * num_map.get(x[-1].upper(), 1)
return int(total_stars)
def is_connected():
REMOTE_SERVER = "one.one.one.one"
try:
# see if we can resolve the host name -- tells us if there is
# a DNS listening
host = socket.gethostbyname(REMOTE_SERVER)
# connect to the host -- tells us if the host is actually
# reachable
s = socket.create_connection((host, 80), 2)
s.close()
return True
except:
pass