Skip to content

Commit

Permalink
🎉 first project structure/functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ch148 committed Apr 29, 2021
0 parents commit 0920f15
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TIME_INTERVAL=180
WATCHED_ASSETS=['BTC', 'ETH' '...']

SMTP_SERVER="smtp.gmail.com"
SENDER_EMAIL=
SENDER_PW=
RECEIVER_EMAIL=
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea/
.vscode/
.DS_Store
*__pycache__
.vscode
.env
venv/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Chris Heinz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h1 align="center">
Crypto Price Alert 🔔
</h1>
<p>
The script uses the Coingecko API to get real-time prices and send you an email,
if you the price of an asset gets above/below your goal.
</p>

## Getting started

1. **Prerequisites**

Before you start, you should have Python 3 with Pip installed.

Then set up a virtual environment and install the required packages inside the project.

```shell
pip install -r requirements.txt
```

2. **Configuration**

Now you can configure the mail server and assets you want to keep an eye on. _.env_ file.
There already is a .env.example file, which you can rename and use.
You need to set the time interval in seconds in which the script is getting executed.
It is also possible to define a list of assets you are interested in.
The script will check if any of the assets is currently trending at the coingecko search chart.

```shell
TIME_INTERVAL=600
WATCHED_ASSETS=['BTC', 'ETH', '...']
```

3. **Start the script.**

You can start the script by running the main file and follow the instructions in the console.
Enjoy experimenting with the script.

```shell
python main.py
```
77 changes: 77 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from decouple import config as getenv
from email.message import EmailMessage
from pycoingecko import CoinGeckoAPI
import smtplib
import os


def send_email(subject, content):
port = 465
smtp_server = getenv("SMTP_SERVER")
sender_email = getenv("SENDER_EMAIL")
receiver_email = getenv("RECEIVER_EMAIL")
password = getenv("SENDER_PW")

server = smtplib.SMTP_SSL(smtp_server, port)

server.ehlo()
server.login(sender_email, password)

msg = EmailMessage()
msg.set_content(content)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = receiver_email

server.send_message(msg)
print("Server sent mail")


# send mail if price is below your goal
def check_price_below(price, goal, asset_shortform):
if price <= goal:
difference = round(goal - price, 4)
send_email("New lower price goal for {}! 📈 🥳".format(asset_shortform),
"{} is currently {}€ under your goal of {}€.\n\n The current price is at {}€."
.format(asset_shortform, difference, goal, round(price, 4)))


# send mail if price is above your goal
def check_price_above(price, goal, asset_shortform):
if price >= goal:
difference = round(price - goal, 4)
send_email("New upper price goal for {}! 📉 🥳".format(asset_shortform),
"{} is currently {}€ above your goal of {}€.\n\n The current price is at {}€."
.format(asset_shortform.upper(), difference, goal, round(price, 4)))


# check if trending asset is in asset list
def check_is_trending(asset_list):
cg = CoinGeckoAPI()
top_7_trending = cg.get_search_trending()["coins"]
trending = []
for coin in top_7_trending:
trending.append(coin['item']['symbol'])
if coin['item']['symbol'] in asset_list:
price = cg.get_price(ids=coin['item']['id'], vs_currencies='eur')[coin['item']['id']]['eur']
print("-" * 50 + "TRENDING" + "-" * 50)
print("{} is trending now at {}€️".format(coin['item']['symbol'], round(price, 4)))
print("-" * 108)


# returns the correct coingecko id of an asset by it's shortform e.g. BTC
def get_coingecko_id(asset_shortform):
cg = CoinGeckoAPI()
all_coins = cg.get_coins_list()
for coin in all_coins:
if coin['symbol'] == asset_shortform.lower():
return coin['id']

print("Sorry we found no match for your asset.")


# triggers a desktop notification
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
46 changes: 46 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from decouple import config as getenv
from functions import check_price_below, check_price_above, check_is_trending, get_coingecko_id
from pycoingecko import CoinGeckoAPI

import time

cg = CoinGeckoAPI()

watched_assets = getenv('WATCHED_ASSETS')
price_goal_above = 0
price_goal_below = 0


asset_short = input("Enter shortform of the asset you want to watch: ")
asset_id = get_coingecko_id(asset_short)

in_above = input("Do you want to get notified when your asset is above your goal ? (y/n): ")
in_below = input("Do you want to get notified when your asset is below your goal ? (y/n): ")


if in_above == "y":
price_goal_above = float(input("Enter your upper price goal: "))

if in_below == "y":
price_goal_below = float(input("Enter your lower price goal: "))


while True:
check_is_trending(watched_assets)

# get the data from the api
asset = cg.get_price(ids=asset_id, vs_currencies='eur', include_24hr_change="true", include_market_cap="true")

asset_price = round(asset[asset_id]['eur'], 4)
asset_24h_change = round(asset[asset_id]['eur_24h_change'], 2)
asset_mcap = round(asset[asset_id]['eur_market_cap'])
print("Current price of {} is at {}€ with a {}% change in 24h and a market cap of {:,}€."
.format(asset_id, asset_price, asset_24h_change, asset_mcap))

if in_above == "y":
check_price_above(asset_price, price_goal_above, asset_short.upper())

if in_below == "y":
check_price_below(asset_price, price_goal_below, asset_short.upper())

time.sleep(int(getenv('TIME_INTERVAL')))
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-decouple>=3.3
pycoingecko>=1.4.1

0 comments on commit 0920f15

Please sign in to comment.