Skip to content

Commit

Permalink
v.1.0.3
Browse files Browse the repository at this point in the history
+Fixed issues ( #1 ) and ( #2 )
+Remove EZ-Tool (start-env.py), the tool has been split into an independent repository(https://github.com/soranoo/Start-Python-Virtual-Environment).
+Added `log_full_color` into the config file(config.example.toml)
+Removed virtual environment
+Convert a valid JSON string into JSON
+ Updated `.gitignore`
+Updated package dependency
  • Loading branch information
soranoo committed Feb 9, 2022
1 parent 0d5338a commit 0337a26
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
config.toml
start-env.py
**/env
**/logs
**/data
**/src/__pycache__
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To install **TradingView-Free-Webhook-Alerts**, check out the [Getting Started g


## Notice
* The program will read the coming email and mark it as read.
* The program will read the incoming email and mark it as read.
* It is suggested to create a new email account for the best performance.
* If you are using Gmail as your email service provider, you should read through the following documents in order to protect your account from getting suspended; therefore, it is suggested to create a new Google account instead of using your main account.
* [Gmail receiving limits](https://support.google.com/a/answer/1366776)
Expand Down
2 changes: 2 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ log_color = true
# include the current time zone into the log
log_time_zone = false

# highlight the log with the same color with log label
log_full_color = false
8 changes: 1 addition & 7 deletions docs/gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
## Usage
- [Setting up TradingView alert](#setting-up-tradingview-alert)
- [Program Deployment](#program-deployment)
- [EZ Tool](#ez-tool)

<a name="installing-python-package"></a>
## 1. Installing the Python package dependencies
Expand Down Expand Up @@ -52,9 +51,4 @@ You must finish the following steps before using the program.
<a name="program-deployment"></a>
## 5. Program Deployment

1. Run `py main.py` in the command prompt.

<a name="ez-tool"></a>
## 6. EZ Tool

Run 'start-env.py' and the command prompt with the activated virtual environment will pop up.
1. Run `py main.py` in the command prompt.
2 changes: 1 addition & 1 deletion env/Lib/site-packages/Pygments-2.11.2.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
../../Scripts/pygmentize.exe,sha256=doxfUiZoZJIwzM8pj79f4z9ekAEfYb3x6zewa3WDZWY,103298
../../Scripts/pygmentize.exe,sha256=JxwwIJz-tqVG4_RMNFKnpI_3IqsX1fSlAH0X44PBz0Y,103305
Pygments-2.11.2.dist-info/AUTHORS,sha256=DuYDYxd0Q_DjzfRU1-j2I2dlcUQRpo9Uti6a3nv-zJA,9487
Pygments-2.11.2.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
Pygments-2.11.2.dist-info/LICENSE,sha256=wBLPF6K6eRQpd8jMW7FJemdUAb95wsm5WnYE4t396Lg,1331
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
../../Scripts/normalizer.exe,sha256=Y7Hb1qb1T4hGzOhr2RksxxwIZZSesCQBoGpxaZf2wxY,103327
../../Scripts/normalizer.exe,sha256=K8V0mEOXBMvIvKyMiHwLxizaAE72PCmwuHbNHGx4g5s,103334
charset_normalizer-2.0.11.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
charset_normalizer-2.0.11.dist-info/LICENSE,sha256=6zGgxaT7Cbik4yBV0lweX5w1iidS_vPNcgIT0cz-4kE,1070
charset_normalizer-2.0.11.dist-info/METADATA,sha256=E6L1l_DgbZV6HaN4cwASdSKTEL7aDI2tDd-BV6WOuJ8,11713
Expand Down
2 changes: 1 addition & 1 deletion env/Lib/site-packages/commonmark-0.9.1.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
../../Scripts/cmark.exe,sha256=8imILTssggQsefUzp7avZJr-RkUhZrR5YNXEGWMdvp8,103298
../../Scripts/cmark.exe,sha256=Rvcp-lj6VU6wjiN1Lfz5MboJ3cd4YD2eAw_w_Bqsmus,103305
commonmark-0.9.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
commonmark-0.9.1.dist-info/LICENSE,sha256=2gzYPRg8HXgZy_BFQcKl0fX2g47YKBCpFDqJ3cvCt94,1645
commonmark-0.9.1.dist-info/METADATA,sha256=Fleg3xhzbgFyI3hktxT4IKS1zEf4H2yuqagr45tWVGg,5710
Expand Down
6 changes: 3 additions & 3 deletions env/Lib/site-packages/pip-19.2.3.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
../../Scripts/pip.exe,sha256=OgwtmdkDHoUI3MTphuXdJWWzZ6L9dUBjY8xtkRO8F8k,103295
../../Scripts/pip3.8.exe,sha256=OgwtmdkDHoUI3MTphuXdJWWzZ6L9dUBjY8xtkRO8F8k,103295
../../Scripts/pip3.exe,sha256=OgwtmdkDHoUI3MTphuXdJWWzZ6L9dUBjY8xtkRO8F8k,103295
../../Scripts/pip.exe,sha256=sLjQrjSXtbf8vZHhAy3Al9JdUUOYbuMyZrlWpc_vy1U,103302
../../Scripts/pip3.8.exe,sha256=sLjQrjSXtbf8vZHhAy3Al9JdUUOYbuMyZrlWpc_vy1U,103302
../../Scripts/pip3.exe,sha256=sLjQrjSXtbf8vZHhAy3Al9JdUUOYbuMyZrlWpc_vy1U,103302
pip-19.2.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
pip-19.2.3.dist-info/LICENSE.txt,sha256=W6Ifuwlk-TatfRU2LR7W1JMcyMj5_y1NkRkOEJvnRDE,1090
pip-19.2.3.dist-info/METADATA,sha256=uA6603UkWcOVSlssH5-xiouxIBqVvzVkNDCHNkzsJs4,3195
Expand Down
2 changes: 1 addition & 1 deletion env/Lib/site-packages/pyfiglet-0.8.post1.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
../../Scripts/pyfiglet.exe,sha256=6Hl_layOrJPoD4ymdW_ymIDA8cAgnkax4_m4BQkVY2s,103290
../../Scripts/pyfiglet.exe,sha256=mGezNxL1BO23o1ZlEui4iOZ4NlzHWjOMQy23fFaY8cc,103297
pyfiglet-0.8.post1.dist-info/DESCRIPTION.rst,sha256=OCTuuN6LcWulhHS3d5rfjdsQtW22n7HENFRh6jC6ego,10
pyfiglet-0.8.post1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
pyfiglet-0.8.post1.dist-info/METADATA,sha256=Emjv1QbWqtpZ86IB7bmpbpzczAhbFCZsIyUThHu4uFw,1319
Expand Down
4 changes: 2 additions & 2 deletions env/Lib/site-packages/setuptools-41.2.0.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
../../Scripts/easy_install-3.8.exe,sha256=efnctx-fcdpSco8zTKkQjtPXLNsmEgCy7fSq0JbEgFg,103313
../../Scripts/easy_install.exe,sha256=efnctx-fcdpSco8zTKkQjtPXLNsmEgCy7fSq0JbEgFg,103313
../../Scripts/easy_install-3.8.exe,sha256=owwge2R_lMubaeD4IR1KT3XgTPJPM5D94L0M_I90iTE,103320
../../Scripts/easy_install.exe,sha256=owwge2R_lMubaeD4IR1KT3XgTPJPM5D94L0M_I90iTE,103320
__pycache__/easy_install.cpython-38.pyc,,
easy_install.py,sha256=MDC9vt5AxDsXX5qcKlBz2TnW6Tpuv_AobnfhCJ9X3PM,126
pkg_resources/__init__.py,sha256=6Kq6B-JSGEFSg_2FAnl6Lmoa5by2RyjDxPBiWuAh-dw,108309
Expand Down
2 changes: 1 addition & 1 deletion env/Scripts/activate
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deactivate () {
# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="D:\Code\TradingView-Free-Webhook-Alerts\env"
VIRTUAL_ENV="D:\GitProjects\TradingView-Free-Webhook-Alerts\env"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
Expand Down
2 changes: 1 addition & 1 deletion env/Scripts/activate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" 65001 > nul
)

set VIRTUAL_ENV=D:\Code\TradingView-Free-Webhook-Alerts\env
set VIRTUAL_ENV=D:\GitProjects\TradingView-Free-Webhook-Alerts\env

if not defined PROMPT set PROMPT=$P$G

Expand Down
Binary file modified env/Scripts/cmark.exe
Binary file not shown.
Binary file modified env/Scripts/easy_install-3.8.exe
Binary file not shown.
Binary file modified env/Scripts/easy_install.exe
Binary file not shown.
Binary file modified env/Scripts/normalizer.exe
Binary file not shown.
Binary file modified env/Scripts/pip.exe
Binary file not shown.
Binary file modified env/Scripts/pip3.8.exe
Binary file not shown.
Binary file modified env/Scripts/pip3.exe
Binary file not shown.
Binary file modified env/Scripts/pyfiglet.exe
Binary file not shown.
Binary file modified env/Scripts/pygmentize.exe
Binary file not shown.
55 changes: 44 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

from rich import print as cprint
from rich import traceback
from imap_tools import MailBox
from datetime import datetime, timezone
from bs4 import BeautifulSoup

from src.logger import log, Colorcode

from imap_tools import MailBox
from datetime import datetime, timezone

traceback.install()

Expand All @@ -33,7 +34,7 @@
displaying_loop_duration = False

# welcome message
print(pyfiglet.figlet_format("TradingView\nFree Webhook"))
cprint(pyfiglet.figlet_format("TradingView\nFree Webhook"))


def send_webhook(payload):
Expand Down Expand Up @@ -79,8 +80,11 @@ def fillZero(num):


def get_latest_email(mailbox):
for email in mailbox.fetch(limit=1, reverse=True):
return email
try:
for email in mailbox.fetch(limit=1, reverse=True):
return email
except:
return False

def connect_imap_server():
try:
Expand All @@ -100,24 +104,47 @@ def main():
global last_email_uid
log.info("Initializing...")
mailbox = connect_imap_server()
last_email_uid = get_latest_email(mailbox).uid
latestEmail = get_latest_email(mailbox)
last_email_uid = int(latestEmail.uid) if latestEmail else False
close_imap_connection(mailbox)
log.info(f"Listening to IMP server({imap_server_address})...")
while True:
# ref: https://github.com/ikvk/imap_tools#actions-with-emails
startTime = datetime.now()
mailbox = connect_imap_server()
latestEmailUid = get_latest_email(mailbox).uid
uidDifferent = int(latestEmailUid)-int(last_email_uid)
latestEmail = get_latest_email(mailbox)
latestEmailUid = int(latestEmail.uid) if latestEmail else False
# check if inbox turn from empty to not empty
if not last_email_uid and latestEmailUid >= 0:
# giving a previous email uid
last_email_uid = latestEmailUid-1
uidDifferent = (latestEmailUid-last_email_uid) if latestEmailUid else -1
if uidDifferent > 0:
latestEmails = mailbox.fetch(limit=uidDifferent, reverse=True)
for email in latestEmails:
if email.from_ in tradingview_alert_email_address:
# get email content
if email.text == "":
try:
ctx = BeautifulSoup(email.html, "html.parser")
ctx = list(ctx.find_all("p"))[1].text
except:
log.warning("No content found in email.")
ctx = ""
else:
ctx = email.text
# check if json
try:
ctx = json.loads(ctx)
except:
ctx = email.text
# stop display loop duration
if displaying_loop_duration:
display_loop_duration(-1)
log.info(f"Sending webhook alert<{email.subject}>..., content: {email.text}")
# send webhook
log.info(f"Sending webhook alert<{email.subject}>, content: {ctx}")
try:
send_webhook(email.text)
send_webhook(ctx)
log.ok("Sent webhook alert successfully!")
log.info(f"The whole process taken {round(abs(datetime.now(timezone.utc)-email.date).total_seconds(),3)}s.")
except Exception as err:
Expand All @@ -134,4 +161,10 @@ def main():


if __name__ == "__main__":
main()
try:
main()
except KeyboardInterrupt:
log.warning("The program has been stopped by user.")
log.warning("The program will shut down after 10s...")
time.sleep(10)
exit()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
beautifulsoup4==4.10.0
certifi==2021.10.8
charset-normalizer==2.0.11
colorama==0.4.4
Expand All @@ -9,5 +10,6 @@ pyfiglet==0.8.post1
Pygments==2.11.2
requests==2.27.1
rich==11.1.0
soupsieve==2.3.1
toml==0.10.2
urllib3==1.26.8
3 changes: 2 additions & 1 deletion src/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
log_color = config.get("log_color")
log_time_zone = config.get("log_time_zone")
log_save = config.get("log_save")
log_full_color = config.get("log_full_color")

dir_path = os.getcwd() # logs path
filename = "{:%d-%m-%Y}".format(datetime.now()) + ".log"
Expand Down Expand Up @@ -112,7 +113,7 @@ def create_logger(logFolder = ""):
dateFormat = "%d-%m-%Y %H:%M:%S %z" if log_time_zone else "%d-%m-%Y %H:%M:%S"
logging.captureWarnings(True) # catch py waring message
formatter_file = logging.Formatter("%(asctime)s | %(levelname)-8s | %(message)s", datefmt=dateFormat)
formatter_console_color = ColoredFormatter(f"{Colorcode.gray}%(asctime)s{Colorcode.reset} | %(log_color)s%(levelname)-8s%(reset)s | {Colorcode.white}%(message)s{Colorcode.reset}",
formatter_console_color = ColoredFormatter(f"{Colorcode.gray}%(asctime)s{Colorcode.reset} | %(log_color)s%(levelname)-8s%(reset)s | {'%(log_color)s' if log_full_color else Colorcode.white}%(message)s{Colorcode.reset}",
datefmt=dateFormat, log_colors=log_colors)
formatter_console = logging.Formatter("%(asctime)s | %(levelname)-8s | %(message)s", datefmt=dateFormat)
logger = logging.getLogger("py.warnings") # catch py waring message
Expand Down
31 changes: 19 additions & 12 deletions start-env.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# -----------------* reference *-----------------
# https://stackoverflow.com/a/60781848
# -----------------------------------------------

import os
import time

path_env = r".\env\Scripts\activate"
def load_env(env_name):
open_env_cmd = f"cmd /k {os.path.join(os.getcwd(), env_name, 'Scripts', 'activate')}"
create_env_cmd = f"py -m venv env"
if os.path.isdir(os.path.join(os.getcwd(), env_name, "Scripts")):
os.system(open_env_cmd)
else:
print("No virtual environment found.")
# ask the user create a new venv or not
o = input("Do you want to create a new virtual environment? [y/n] ")
if o.lower() == "y":
print("Creating virtual environment...")
os.system(create_env_cmd)
os.system(open_env_cmd)
else:
print("The program will shut down after 10s...")
time.sleep(10)
exit()

'''
Create venv 'py -m venv env'
Freeze 'pip freeze -l > requirements.txt'
With /k cmd executes and then remain open
With /c executes and close
'''
if __name__ == "__main__":
os.system(f"cmd /k {path_env}")
load_env("env")

0 comments on commit 0337a26

Please sign in to comment.