This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
64 lines (53 loc) · 1.62 KB
/
utils.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
64
#!/usr/bin/env python
# Utility functions for infinity tracker.
import json
import urllib2
import urlparse
from six import binary_type, integer_types, text_type
# From python-influxdb
def _get_unicode(data, force=False):
"""Try to return a text aka unicode object from the given data."""
if isinstance(data, binary_type):
return data.decode("utf-8")
elif data is None:
return ""
elif force:
return str(data)
else:
return data
# From python-influxdb
def _escape_tag(tag):
tag = _get_unicode(tag, force=True)
return (
tag.replace("\\", "\\\\")
.replace(" ", "\\ ")
.replace(",", "\\,")
.replace("=", "\\=")
)
# From python-influxdb
def _escape_value(value):
value = _get_unicode(value)
if isinstance(value, text_type) and value != "":
return '"{0}"'.format(value.replace('"', '\\"').replace("\n", "\\n"))
elif isinstance(value, integer_types) and not isinstance(value, bool):
return str(value) + "i"
else:
return str(value)
def extract_req_body(s):
qsl = urlparse.parse_qsl(s, keep_blank_values=True)
for k, v in qsl:
if k == "data":
return v
return ""
def get_current_temp(api_key, location_query):
if not (api_key and location_query):
return False
url = "http://api.wunderground.com/api/{}/geolookup/conditions/q/{}.json".format(
api_key, location_query
)
f = urllib2.urlopen(url) # nosec
json_string = f.read()
parsed_json = json.loads(json_string)
temp_f = parsed_json["current_observation"]["temp_f"]
f.close()
return temp_f