Skip to content

Commit

Permalink
Merge pull request #3 from TobiasS1402/dev-env
Browse files Browse the repository at this point in the history
Dev env
  • Loading branch information
TobiasS1402 authored Sep 14, 2022
2 parents e4c974e + 4ffe15b commit 18bbe31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ name: Docker
# documentation.

on:
push:
branches: [ "master" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "master" ]
tags: [ 'v*.*.*' ]

env:
# Use docker.io for Docker Hub if empty
Expand Down
18 changes: 11 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import requests
import json
import logging
from dotenv import dotenv_values
import os
from dotenv import load_dotenv
from apscheduler.schedulers.blocking import BlockingScheduler


Expand All @@ -16,8 +17,11 @@ def readConfig():
'''
reading environment variables
'''
logging.info("Environment file has been read")
return dotenv_values(".env")
if os.path.exists('.env') == True:
load_dotenv()
return logging.info("Environment file has been read")
else:
return logging.info(".env file not present, falling back to normal Environment")

def init(latitude, longitude):
'''
Expand All @@ -27,7 +31,7 @@ def init(latitude, longitude):
url = "https://www.ah.nl/gql" #open gql endpoint for anonymous requests
headers = {'Client-Name': 'ah-stores','Client-Version':'0.230.0'} #Missing client identification. Requests should include \"client-name\" and \"client-version\" headers
gql_body = """query stores($filter: StoreFilterInput, $size: PageSize!, $start: Int) {stores(filter: $filter, size: $size, start: $start) { result { ...storeList __typename} page { total hasNextPage __typename} __typename }}fragment storeList on Store { id name storeType phone distance address { ...storeAddress __typename } geoLocation { latitude longitude __typename} openingDays { ...openingDaysInfo __typename } __typename}fragment storeAddress on StoreAddress { city street houseNumber houseNumberExtra postalCode countryCode __typename}fragment openingDaysInfo on StoreOpeningDay { dayName type date openingHour { ...storeOpeningHour __typename } }fragment storeOpeningHour on StoreOpeningHour { date openFrom openUntil __typename}"""
json_data = {"operationName":"stores","variables":{"filter":{"location":{"latitude":latitude,"longitude":longitude}},"start":0,"size":readConfig().get('number_of_stores')},"query": gql_body} #tweak size: number of albert heijns this variable is for filtering
json_data = {"operationName":"stores","variables":{"filter":{"location":{"latitude":latitude,"longitude":longitude}},"start":0,"size":os.environ['number_of_stores']},"query": gql_body} #tweak size: number of albert heijns this variable is for filtering
response = requests.post(url=url, headers=headers, json=json_data, ) #simple post request putting it all together

if response.status_code == 200:
Expand Down Expand Up @@ -106,8 +110,8 @@ def telegramConnection(appieNotification):
'''
setting up api connection for sending Telegram messages
'''
bot_token = readConfig().get('telegram_bot_token')
bot_chatID = readConfig().get('telegram_chat_id')
bot_token = os.environ['telegram_bot_token']
bot_chatID = os.environ['telegram_chat_id']
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + appieNotification
response = requests.get(send_text)
logging.info(response.json)
Expand All @@ -117,7 +121,7 @@ def boxRequests():
'''
function where the magic happens: it connects to the local sqlite db, connects authenticated to the surprise-boxes api and executes queries on the database
'''
results = init(float(readConfig().get('latitude')),float(readConfig().get('longitude'))) #Don't forget to make this a variable / cli parameter
results = init(float(os.environ['latitude']),float(os.environ['longitude'])) #Don't forget to make this a variable / cli parameter
sqliteConnection = sqlite3.connect('appie.db')
cursor = sqliteConnection.cursor()
for key,value in results.items():
Expand Down
34 changes: 33 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,39 @@ A simple Python program for polling the Albert Heijn REST and Graph API's to get

The program works by grabbing the nearest x (default 5) number of Albert Heijn stores based on your latitude and longitude. After this it keeps a record of stores with boxes available and it will send you a Telegram notification when something's changed. e.g. boxes are available, a box is gone, everything is gone.

## :checkered_flag: Starting ##
## ☸ Running inside a container ##

### 🚢 pulling from ghcr.io ###
```bash
# Clone this project
$ docker pull ghcr.io/tobiass1402/appiesniper:v0.1.2

# Run the project with env file
$ docker run --env-file ./.env -d ghcr.io/tobiass1402/appiesniper:v0.1.2r

# Run the project with docker env variables
$ docker run -d ghcr.io/tobiass1402/appiesniper:v0.1.2 -e longitude=5.1331746 -e latitude=51.5868726 -e telegram_bot_token='xxxxxxxx:xxxxxxxxxxxxxxxxxxxx' -e telegram_chat_id='xxxxxxxx' -e number_of_stores=5
```

### 🔨 Building it yourself ###
```bash
# Clone this project
$ git clone https://github.com/TobiasS1402/appiesniper

# Access
$ cd appiesniper

# Build the container environment
$ docker build . -t tobiass1402/appiesniper

# Run the project with env file
$ docker run --env-file ./.env -d tobiass1402/appiesniper

# Run the project with docker env variables
$ docker run -d tobiass1402/appiesniper -e longitude=5.1331746 -e latitude=51.5868726 -e telegram_bot_token='xxxxxxxx:xxxxxxxxxxxxxxxxxxxx' -e telegram_chat_id='xxxxxxxx' -e number_of_stores=5
```

## ☸ Running standalone ##

```bash
# Clone this project
Expand Down

0 comments on commit 18bbe31

Please sign in to comment.