# Path: yams_api/plugins/dev/api_utils/http_methods.py
# def http_requests_get_to_dict(url, headers={}):
#     """Fetch content at a URL, returning a namedtuple of python-request module attributes
# 
#     :param url: url to scrape
#     :param headers: dict of headers to use for the request
#     :return: a namedtuple containing ('headers', 'text', 'status') for Pythonic scripting
#     """
# 
#     # Keep these outside of the try/except so we can potentially get metadata even on exceptions.
#     # Defines some placeholders to avoid a potential NameError
#     _headers = dict()
#     _status = 200
#     _status_major_code = 0
# 
#     try:
#         r = requests.get(url, headers=headers)
#         # r.content is a bytes obj and probably won't be json serializable.  could return a tuple
#         _status = r.status_code
#         _str_status = str(_status)
#         _status_major_code = int(_str_status[0]) # '200' -> '2' ->2
# 
#         rh = r.headers
#         rt = r.text
# 
#         # common headers
#         _headers["status_code"] = _status
#         _headers["etag"] = rh.get("etag", "")
#         _headers["date"] = rh.get("date", "")
#         _headers["last-modified"] = rh.get("last-modified", "")
#         _headers["content-type"] = rh.get("content-type", "")
#         _headers["raw_headers"] = dict(rh)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             rt,
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             False
#         )
# 
#     except ConnectionError as e:
#         _headers["response"] = str(e)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             str(e),
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             True)
# 
#     return _http_namedtuple


from lxml.html import fromstring as html_string
from yams_api.plugins.dev.api_utils.http_methods import http_requests_get_to_dict

    # not really into this as a list, but will adjust as needed
    _resp["response"]["outages"] = []

    if not r.error and r.http_2xx:

        _rtxml = html_string(r.text)
        _body = _rtxml.xpath("//html/body")[0]

        _content_row = _body.xpath("section[@id='status-content']/article/section[@class='entry-content']/section[@class='row']/main/table[contains(@class, 'table')]")

        _tbody_rows = _content_row[0].xpath("tbody/tr")

        _serv_status = {}
        for _service_row in _tbody_rows:

            _service_row_td_collection = _service_row.xpath("td")

            _service_name = _service_row_td_collection[0].text.strip()
            _service_status = _service_row_td_collection[2].text.strip()

            if _service_status == "All Systems are Available and Operating Normally":
                _service_status = "ok"
            else:
                _resp["response"]["outages"].append(_service_name)

            _serv_status[_service_name] = _service_status

        _resp["response"]["services"] = _serv_status


# Path: yams_api/plugins/dev/api_utils/http_methods.py
# def http_requests_get_to_dict(url, headers={}):
#     """Fetch content at a URL, returning a namedtuple of python-request module attributes
# 
#     :param url: url to scrape
#     :param headers: dict of headers to use for the request
#     :return: a namedtuple containing ('headers', 'text', 'status') for Pythonic scripting
#     """
# 
#     # Keep these outside of the try/except so we can potentially get metadata even on exceptions.
#     # Defines some placeholders to avoid a potential NameError
#     _headers = dict()
#     _status = 200
#     _status_major_code = 0
# 
#     try:
#         r = requests.get(url, headers=headers)
#         # r.content is a bytes obj and probably won't be json serializable.  could return a tuple
#         _status = r.status_code
#         _str_status = str(_status)
#         _status_major_code = int(_str_status[0]) # '200' -> '2' ->2
# 
#         rh = r.headers
#         rt = r.text
# 
#         # common headers
#         _headers["status_code"] = _status
#         _headers["etag"] = rh.get("etag", "")
#         _headers["date"] = rh.get("date", "")
#         _headers["last-modified"] = rh.get("last-modified", "")
#         _headers["content-type"] = rh.get("content-type", "")
#         _headers["raw_headers"] = dict(rh)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             rt,
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             False
#         )
# 
#     except ConnectionError as e:
#         _headers["response"] = str(e)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             str(e),
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             True)
# 
#     return _http_namedtuple


# Path: yams_api/plugins/dev/constantcontact/methods.py
from lxml.html import fromstring as html_string
from yams_api.plugins.dev.api_utils.http_methods import http_requests_get_to_dict


    # not really into this as a list, but will adjust as needed
    _resp["response"]["outages"] = []

    if not r.error and r.http_2xx:

        _rtxml = html_string(r.text)
        _body = _rtxml.xpath("//html/body")[0]

        _content_row = _body.xpath("section[@id='status-content']/article/section[@class='entry-content']/section[@class='row']/main/table[contains(@class, 'table')]")

        _tbody_rows = _content_row[0].xpath("tbody/tr")

        _serv_status = {}
        for _service_row in _tbody_rows:

            _service_row_td_collection = _service_row.xpath("td")

            _service_name = _service_row_td_collection[0].text.strip()
            _service_status = _service_row_td_collection[2].text.strip()

            if _service_status == "All Systems are Available and Operating Normally":
                _service_status = "ok"
            else:
                _resp["response"]["outages"].append(_service_name)

            _serv_status[_service_name] = _service_status

        _resp["response"]["services"] = _serv_status


    else: