from flask import Flask
from flask_restful import Resource, Api
from bs4 import BeautifulSoup
from IPython import embed
import urllib2
import re
import json

app = Flask(__name__)
api = Api(app)

data = {}

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

class Main(Resource):
  def get(self):
    return {"yo": "ayy"}

class Weather(Resource):
  global data
  webpage = urllib2.urlopen('https://www.atmos.illinois.edu/weather/')
  soup = BeautifulSoup(webpage, "html.parser")
  location = str(soup.find_all('font')[3])
  match = re.compile(r'<b>(.*?)<br>(.*?)<\/br>')
  data['weather_station_location'] = match.search(location).group(1) + " " + match.search(location).group(2)

  time = str(soup.find_all('font')[5]) #convert to unix
  match = re.compile(r'(^([0-1]\d:[0-5]\d)(AM|PM)?$)')
  data['last_recorded_time'] = match.search(time)
  weather_data = str(soup.find_all('font')[6])

  match = re.compile(r'.*?>\\n(.*?)\\n')
  #data['weather_condition'] = match.search(weather_data)
  match = re.compile(r'Temperature:.*?([0-9]+)')
  data['temperature'] = match.search(weather_data).group(1)
  match = re.compile(r'Dew Point:.*?([0-9]+)')
  data['dew_point'] = match.search(weather_data).group(1)
  match = re.compile(r'Rel. Humidity:.*?([0-9]+%)')
  data['relative_humidity'] = match.search(weather_data).group(1)
  match = re.compile(r'Winds:(.*?)\\n')
  #data['winds'] = match.search(weather_data)
  match = re.compile(r'Visibility:.*?([0-9]+.*?)\\n')
  #data['visibility'] = match.search(weather_data)
  match = re.compile(r'Pressure:.*?(([0-9]+(\.[0-9][0-9]?)?).*?)\\n(.*?)\\n')
  #data['pressure'] = match.search(weather_data)

  data['latestRadarImage'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/nicerad/nicerad_N.gif"
  data['stormTotalPrecipImage'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/niceradilxpretx/niceradilxpretx_N.gif"
  data['surfaceTempImage'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/sfctmp/sfctmp_N.gif"
  data['surfaceDewPointTempImage'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/sfctdp/sfctdp_N.gif"
  data['seaLevelPressure'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/sfcslp/sfcslp_N.gif"
  data['mdwSufaceObservations'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/sfcslp/sfcslp_N.gif"
  data['irImage'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/satconusenhir/satconusenhir_N.gif"
  data['irImage2'] = "https://www.atmos.illinois.edu/weather/tree/prods/current/satnoamir/satnoamir_N.gif"

  def get(self):
    return data

class Laundry(Resource):
  def get(self):
    request_url = "http://23.23.147.128/homes/mydata/urba7723"
    response = urllib2.urlopen(request_url)
    data = json.load(response)
    return data

api.add_resource(Main, '/')
#api.add_resource(Dining, '/dining')
api.add_resource(Weather, '/weather')
api.add_resource(Laundry, '/laundry')
#api.add_resource(WiFi, '/wifi')
#api.add_resource(DailyIllini, '/dailyillini')
#api.add_resource(Directory, '/directory/search/:id') #? parameters at end
#api.add_resource(AcademicDepartment, '/department/:name')
#api.add_resource(Wiki, '/wiki/')


if __name__ == '__main__':
    app.run(debug=True)

@@ -2579,16 +2579,27 @@
 N.gif%22%0A%0A
+  embed()%0A%0A
   def ge
@@ -2821,16 +2821,59 @@
 n data%0A%0A
+class Dining(Resource):%0A  def get(self):%0A%0A%0A
 api.add_
@@ -2892,17 +2892,16 @@
 n, '/')%0A
-#
 api.add_
@@ -2926,16 +2926,42 @@
 /dining'
+, '/dining/search/%3Cquery%3E'
 )%0Aapi.ad

add route