diff --git a/.github/workflows/dockerhub-autopublishing.yml b/.github/workflows/dockerhub-autopublishing.yml new file mode 100644 index 0000000..1444956 --- /dev/null +++ b/.github/workflows/dockerhub-autopublishing.yml @@ -0,0 +1,18 @@ +name: Docker-Hub-Autopublishing + +on: + push: + tags: + - '*' + +jobs: + build: + runs-on: debian-latest + + steps: + - uses: actions/checkout@v1 + - name: Build docker image and push to docker hub + run: | + echo "${{ secrets.DOCKER_TOKEN }}" | docker login -u michivonah --password-stdin + docker image build -t michivonah/ep-alerts:latest . + docker push michivonah/ep-alerts:latest diff --git a/.github/workflows/image-build.yml b/.github/workflows/image-build.yml deleted file mode 100644 index d735988..0000000 --- a/.github/workflows/image-build.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Build Docker Image -on: - push: - branches: - - main -jobs: - build: - name: push docker image to docker hub - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: login to docker hub - id: docker-hub - env: - username: ${{secrets.DOCKER_USERNAME}} - password: ${{secrets.DOCKER_TOKEN}} - run: | - docker login -u $username -p $password - - name: build the docker image - id: build-docker-image - run: | - docker build -t michivonah/ep-alerts:latest . - - name: push the docker image - id: push-docker-image - run: docker push michivonah/ep-alerts:latest diff --git a/main.py b/main.py index f31846e..e404edd 100644 --- a/main.py +++ b/main.py @@ -26,63 +26,41 @@ def sendMessage(message, notificationType): header = { "Authorization":f"Bearer {accessToken}" } - try: - response = requests.post(url=endpoint, headers=header, data=message) - except Exception as error: - raise Exception(f"Got an error while sending the notification: {error}") - else: - try: - response = requests.post(url=endpoint, data=message) - except Exception as error: - raise Exception(f"Got an error while sending the notification: {error}") + response = requests.post(url=endpoint, headers=header, data=message) + else: response = requests.post(url=endpoint, data=message) return response case "discord" | _: endpoint = os.getenv('DISCORD_WEBHOOK') data = { "content": message, } - try: - response = requests.post(url=endpoint, json=data) - return response - except Exception as error: - raise Exception(f"Got an error while sending the notification: {error}") + response = requests.post(url=endpoint, json=data) + return response # Check for the current waiting times def checkTimes(subscribedAttractions, themepark): - try: - endpoint = "https://api.wartezeiten.app/v1/waitingtimes" + endpoint = "https://api.wartezeiten.app/v1/waitingtimes" - header = { - "language":"de", - "park":themepark - } + header = { + "language":"de", + "park":themepark + } - req = requests.get(url=endpoint, headers=header) - except: - raise Exception(f"API Request to endpoint {endpoint} failed.") - try: - result = req.json() - except: - raise Exception("Format of API response is invalid. (JSON expected)") - try: - attractions = result - for attraction in attractions: - if isinstance(attraction, dict) and "code" in attraction: - if attraction["code"] in subscribedAttractions: - if attraction["status"] == "opened": - refreshTime = 30 - if not attraction["code"] in currentTimes: currentTimes[attraction["code"]] = attraction["waitingtime"]; - if currentTimes[attraction["code"]] > attraction["waitingtime"]: - sendMessage(f"Waiting time of {attraction['name']} sank to {attraction['waitingtime']} Minutes!", notificationType) - elif currentTimes[attraction["code"]] < attraction["waitingtime"]: - sendMessage(f"Waiting time for {attraction['name']} increased to {attraction['waitingtime']} Minutes!", notificationType) - currentTimes[attraction["code"]] = attraction["waitingtime"] - else: - refreshTime = 180 + req = requests.get(url=endpoint, headers=header) + result = req.json() + attractions = result + for attraction in attractions: + if attraction["code"] in subscribedAttractions: + if attraction["status"] == "opened": + refreshTime = 30 + if not attraction["code"] in currentTimes: currentTimes[attraction["code"]] = attraction["waitingtime"]; + if currentTimes[attraction["code"]] > attraction["waitingtime"]: + sendMessage(f"Waiting time of {attraction['name']} sank to {attraction['waitingtime']} Minutes!", notificationType) + elif currentTimes[attraction["code"]] < attraction["waitingtime"]: + sendMessage(f"Waiting time for {attraction['name']} increased to {attraction['waitingtime']} Minutes!", notificationType) + currentTimes[attraction["code"]] = attraction["waitingtime"] else: - print(f"Info: Attraction was skipped because it has an invalid data structure. Affected attraction: {attraction}") - except Exception as error: - raise Exception(f"Got an error while checking for differences since the last API call. Error: {error}") + refreshTime = 180 # Main Loop # Checks every 30 seconds for changes in the waiting times of the subscribed attractions @@ -92,10 +70,7 @@ if __name__ == '__main__': print("By Michi von Ah") print("Big thanks to the wartezeiten.app API!") while True: - try: - checkTimes(subscribedAttractions, themepark) - print(f"Checked for updates at {time.strftime('%H:%M:%S', time.localtime())}") - except Exception as error: - raise Exception(error) + checkTimes(subscribedAttractions, themepark) + print(f"Checked for updates at {time.strftime('%H:%M:%S', time.localtime())}") time.sleep(refreshTime) diff --git a/requirements.txt b/requirements.txt index a8ecb02..fdb4576 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ # requirements.txt -python-dotenv==1.1.1 -pipenv==2025.0.4 -requests==2.32.5 \ No newline at end of file +python-dotenv==1.0.1 +pipenv==2024.1.0 \ No newline at end of file