import tcxparser
from configparser import ConfigParser
import urllib.request
import dateutil.parser
import json

# Darksky weather API
# Create config file manually
parser = ConfigParser()
parser.read('slowburn.config', encoding='utf-8')
darksky_key = parser.get('darksky', 'key')

tcx = tcxparser.TCXParser('gps_logs/2017-06-15_Running.tcx')
run_time = tcx.completed_at


def convert_time_to_unix(time):
    parsed_time = dateutil.parser.parse(time)
    time_in_unix = parsed_time.strftime('%s')
    return time_in_unix


def darksky_api_request(run_time):
    unix_run_time = convert_time_to_unix(run_time)
    darksky_request = urllib.request.urlopen(
        "https://api.darksky.net/forecast/" + darksky_key + "/" + str(tcx.latitude) + "," + str(
            tcx.longitude) + "," + unix_run_time + "?exclude=currently,flags").read()
    return (darksky_request)


def get_temperatures(darksky_request):
    darksky_json = json.loads(darksky_request.decode('utf-8'))
    temperatures = {}
    for weather in darksky_json['hourly']['data']:
        temperatures[weather['time']] = weather['temperature']
    return temperatures


def temperature_at_run_time(run_time, temperatures):
    """Get temperature at the hour of run completion."""

    hours = []
    run_time = convert_time_to_unix(run_time)
    for time, temperature in temperatures.items():
        hours.append(((abs(time - int(run_time))), temperature))

    temperature = (min(hours, key=lambda time_delta: time_delta[0]))[1]
    return temperature


darksky_request = darksky_api_request(run_time)
temperatures = get_temperatures(darksky_request)
print(temperature_at_run_time(run_time, temperatures))

@@ -516,16 +516,106 @@
 _unix%0A%0A%0A
+class GetWeather:%0A    def __init__(self, run_time):%0A        self.run_time = run_time%0A%0A    
 def dark
@@ -630,31 +630,31 @@
 request(
-run_time
+self
 ):%0A    
+    
 unix_run
@@ -674,32 +674,37 @@
 rt_time_to_unix(
+self.
 run_time)%0A    da
@@ -693,24 +693,28 @@
 f.run_time)%0A
+    
     darksky_
@@ -755,16 +755,20 @@
         
+    
 %22https:/
@@ -848,16 +848,20 @@
  + str(%0A
+    
         
@@ -938,16 +938,20 @@
 .read()%0A
+    
     retu
@@ -976,16 +976,19 @@
 t)%0A%0A
-%0A
+    
 def 
-get
+all
 _tem
@@ -1001,30 +1001,23 @@
 res(
-darksky_request
+self
 ):%0A    
+    
 dark
@@ -1038,24 +1038,29 @@
 n.loads(
+self.
 darksky_
 request.
@@ -1051,23 +1051,29 @@
 darksky_
+api_
 request
+()
 .decode(
@@ -1082,24 +1082,28 @@
 tf-8'))%0A    
+    
 temperatures
@@ -1108,24 +1108,28 @@
 es = %7B%7D%0A    
+    
 for weather 
@@ -1163,32 +1163,36 @@
 data'%5D:%0A        
+    
 temperatures%5Bwea
@@ -1230,16 +1230,20 @@
 ature'%5D%0A
+    
     retu
@@ -1259,17 +1259,20 @@
 atures%0A%0A
-%0A
+    
 def temp
@@ -1282,46 +1282,20 @@
 ture
-_at_run_time(run_time, temperatures):%0A
+(self):%0A    
     
@@ -1348,24 +1348,28 @@
 on.%22%22%22%0A%0A    
+    
 hours = %5B%5D%0A 
@@ -1371,16 +1371,25 @@
  %5B%5D%0A    
+    unix_
 run_time
@@ -1412,16 +1412,21 @@
 to_unix(
+self.
 run_time
@@ -1423,24 +1423,28 @@
 f.run_time)%0A
+    
     for time
@@ -1460,16 +1460,25 @@
 ture in 
+self.all_
 temperat
@@ -1481,16 +1481,18 @@
 eratures
+()
 .items()
@@ -1493,16 +1493,20 @@
 tems():%0A
+    
         
@@ -1535,16 +1535,21 @@
 e - int(
+unix_
 run_time
@@ -1568,16 +1568,20 @@
 ture))%0A%0A
+    
     temp
@@ -1644,16 +1644,20 @@
 0%5D))%5B1%5D%0A
+    
     retu
@@ -1677,145 +1677,47 @@
 e%0A%0A%0A
-darksky_request = darksky_api_request(run_time)%0Atemperatures = get_temperatures(darksky_request)%0Aprint(temperature_at_run_time(run_time, 
+Weather = GetWeather(run_time)%0AWeather.
 temp
@@ -1727,8 +1727,7 @@
 ture
-s)
+(
 )%0A

Put Darksky API request inside a class