import requests
import sys
import pandas as pd

# API_KEY = 'Q0nytxzfMVj7ePvRp111UY9u0mvBpbZlIIx8z63k'
API_KEY = 'PspCfR6NgTMW9ZlxijBY5de3yUkkcamch3OcGxEp'
# DATE = '2021-01-01'
# facilityId = 2103

def QueryEmission(DATE, facilityId, hour):
    parameters = {
        'api_key': API_KEY,
        'beginDate': DATE,
        'endDate': DATE,
        'facilityId': facilityId,
        'page': 1,
        'perPage': 100
    }
    emissionsUrl = "https://api.epa.gov/easey/emissions-mgmt/emissions/apportioned/hourly/by-facility"
    emissionsResponse = requests.get(emissionsUrl, params=parameters)

    # 错误提示
    print("Status code: "+str(emissionsResponse.status_code))
    if (int(emissionsResponse.status_code) > 399):
        sys.exit("Error message: "+emissionsResponse.json()['error']['message'])


    # collecting data as a data frame
    emissionsResponse_df = pd.DataFrame(emissionsResponse.json())

        
    t = emissionsResponse_df.transpose()
    Q_1 = t.values[[8], hour]
    Q_2 = t.values[[8], hour + 1]
    # print(Q_1)
    # print(Q_2)
    return Q_1, Q_2

# QueryEmission('2021-10-10', 6165, 11)