diff --git a/.gitignore b/.gitignore index 122e27b..47df344 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ ### OWN ### -credentials.py -.credentials.py -./credentials.py -*/credentials.py +#barry_energy_api/credentials.py +#.credentials.py +#barry_energy_api/credentials.py +#*/credentials.py ### Linux ### diff --git a/README.md b/README.md index acb9973..f292c22 100644 --- a/README.md +++ b/README.md @@ -1 +1,65 @@ -On its way \ No newline at end of file +

Welcome to Barry Energy API Client 👋

+

+ Version + + License: MIT + +

+ +> Simple python package to work with the API of Barry Energy. +> The API documentation can be found [here](https://developer.barry.energy). + +> Please be aware that this project is NOT offical. + +## Install + +```sh +pip3 install barry-energy-api +``` + +## Usage + +#### Get your API Token +```sh +Get from mobile app under "Add-ons" -> "Barry API" +``` +
+ +1. Import the package +```python +from barry_energy_api import PriceZone, BarryEnergyAPI +``` +2. Initialize the API +```python +api = BarryEnergyAPI('api-token', 5, PriceZone.DK_EAST) +``` +3. Get aggregated consumption for the last 5 days as defined when API was initialized. +```python +api.consumption() +``` +4. Get prices from the last two days. +##### Notice that methode has overloading +```python +api.spotprices(2) +``` + + +## Author + +👤 **Emil Elkjær Nielsen** + +* Website: https://evsn.dk +* Github: [@eelkjaer](https://github.com/eelkjaer) +* LinkedIn: [@emil-elkjær](https://linkedin.com/in/emil-elkjær) + +## Show your support + +Give a ⭐️ if this project helped you! + +## 📝 License + +Copyright © 2021 [Emil Elkjær Nielsen](https://github.com/eelkjaer)
+This project is [MIT](LICENSE) licensed. + +*** +_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ \ No newline at end of file diff --git a/barry_energy_api/__init__.py b/barry_energy_api/__init__.py new file mode 100644 index 0000000..16491a5 --- /dev/null +++ b/barry_energy_api/__init__.py @@ -0,0 +1,88 @@ +import enum + +from barry_energy_api.entities import meteringpoints, spotprice, consumption +from barry_energy_api.entities.consumption import getConsumption +from barry_energy_api.entities.meteringpoints import getMeteringPoints +from barry_energy_api.entities.spotprice import getSpotprice +from barry_energy_api.utils import dateFormat, createHeader +from datetime import datetime, timedelta + + +class BarryException(Exception): + pass + +class PriceZone(enum.Enum): + """ + DK_WEST = West Denmark (zip-code below 4999) + DK_EAST = East Denmark (zip-code above 5000) + FRANCE = Self explanatory + """ + DK_WEST = "DK_NORDPOOL_SPOT_DK1" + DK_EAST = "DK_NORDPOOL_SPOT_DK2" + FRANCE = "FR_EPEX_SPOT_FR" + + +class BarryEnergyAPI: + def __init__(self, api_token: str, pastdays: int, pricezone: PriceZone): + # Sets the API key + self.api_token = api_token + self.pricezone = pricezone.value + + # Set the date range + self.todate = datetime.utcnow() + self.fromdate = dateFormat(self.todate - timedelta(days=pastdays)) + self.todate = dateFormat(self.todate) + + # Creates HTTP header + self.header = createHeader(self.api_token, self.fromdate, self.todate, self.pricezone) + + def set_startdate(self, pastdays: int): + """ + Changes the date which measuring is happening from. + :param pastdays: + Days before today + """ + self.fromdate = dateFormat(datetime.utcnow() - timedelta(days=pastdays)) + + def spotprices(self, pastdays=None): + """ + Gives the spot prices in the specified from/to range. + Spot prices are delivered ahead of our current time, up to 24 hours ahead. + The price is excluding VAT and tarrifs. + """ + try: + headers = self.header + if pastdays is not None: + headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays)) + return getSpotprice(headers) + except Exception as ex: + raise BarryException(str(ex)) + + def consumption(self, pastdays=None): + """ + Gives the consumption on an hourly resolution. + It accepts a past from and to date and MPID/PDL as input. + + Note that it will not return any data unless one is a Barry customer. + The data usually has two to three days of delay, which is a limitation of the regulatory authorities. + """ + try: + headers = self.header + if pastdays is not None: + headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays)) + return getConsumption(headers) + except Exception as ex: + raise BarryException(str(ex)) + + def meteringpoints(self, pastdays=None): + """ + Gives information on all of your metering points subscribed to Barry. + This method contains metadata on your metering points, including the price area and the MPID/PDL (Metering Point Identification). + """ + try: + headers = self.header + if pastdays is not None: + headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays)) + return getMeteringPoints(headers) + except Exception as ex: + raise BarryException(str(ex)) diff --git a/consumption.py b/barry_energy_api/entities/consumption.py similarity index 100% rename from consumption.py rename to barry_energy_api/entities/consumption.py diff --git a/meteringpoints.py b/barry_energy_api/entities/meteringpoints.py similarity index 100% rename from meteringpoints.py rename to barry_energy_api/entities/meteringpoints.py diff --git a/spotprice.py b/barry_energy_api/entities/spotprice.py similarity index 92% rename from spotprice.py rename to barry_energy_api/entities/spotprice.py index 246c05c..0bafc81 100644 --- a/spotprice.py +++ b/barry_energy_api/entities/spotprice.py @@ -9,7 +9,7 @@ def getSpotprice(headers): "id": 0, "jsonrpc": "2.0", "params": [ - "DK_NORDPOOL_SPOT_DK1", + headers[3], headers[1], headers[2] ] diff --git a/barry_energy_api/utils.py b/barry_energy_api/utils.py new file mode 100644 index 0000000..4013675 --- /dev/null +++ b/barry_energy_api/utils.py @@ -0,0 +1,11 @@ +def createHeader(apitoken, fromdate, todate, pricezone): + apiHeader = { + "Content-Type": "application/json", + "Authorization": "Bearer " + apitoken + } + + return [apiHeader, fromdate, todate, pricezone] + + +def dateFormat(date): + return date.strftime("%Y-%m-%dT%H:%M:%SZ") \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index 829bc31..0000000 --- a/main.py +++ /dev/null @@ -1,31 +0,0 @@ -from datetime import datetime, timedelta -from utils import createHeader, dateFormat -from consumption import getConsumption -from meteringpoints import getMeteringPoints -from spotprice import getSpotprice - - -## Set dates -fromdate = datetime.utcnow() -todate = dateFormat(fromdate - timedelta(days=1)) -fromdate = dateFormat(fromdate) -## - -## Create list with API header and required date range. -header = createHeader(fromdate, todate) - -## API calls -consumption = getConsumption(header) -meteringpoints = getMeteringPoints(header) -spotprice = getSpotprice(header) - - -## Show results -print("Your metering point(s):") -print(meteringpoints) - -print("Your spot price:") -print(spotprice) - -print("Current consumption:") -print(consumption) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1e4bbcb --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +import os +from setuptools import setup + +# Thanks Frankkkkk for inspiring. +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + +setup( + name = "barry-energy-api", + version = "1.0.0", + author = "Emil Elkjær Nielsen", + author_email = "emil@evsn.dk", + description = ("Connecting to and getting data from Barry Energy's API (http://barry.energy)"), + license = "LICENS", + keywords = "python barry energy api electricity prices barry.energy", + url = "https://github.com/eelkjaer/BarryAPIClient", + packages=['barry_energy_api'], + long_description=open("README.md", "r").read(), + long_description_content_type="text/markdown", + install_requires=[], + classifiers=[ + "Development Status :: 4 - Beta", + "Programming Language :: Python :: 3.9", + "Topic :: Utilities", + "License :: OSI Approved :: MIT License", + ], +) \ No newline at end of file diff --git a/utils.py b/utils.py deleted file mode 100644 index cae6ec6..0000000 --- a/utils.py +++ /dev/null @@ -1,15 +0,0 @@ -def createHeader(fromdate, todate): - - from credentials import getApiKey - apiKey = getApiKey() #Simply returns a String with the API key from Barry - - apiHeader = { - "Content-Type": "application/json", - "Authorization": "Bearer " + apiKey - } - - return [apiHeader, fromdate, todate] - - -def dateFormat(date): - return date.strftime("%Y-%m-%dT%H:%M:%SZ") \ No newline at end of file