From da6b8ad0bfdec72c92bea60efe71a423358b97da Mon Sep 17 00:00:00 2001 From: Domeniko Gentner Date: Sat, 7 Nov 2020 13:24:49 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 +++ Pipfile | 12 +++++++++ Pipfile.lock | 57 +++++++++++++++++++++++++++++++++++++++++++ bearer.json | 4 +++ mail/__init__.py | 1 + mail/mail.py | 41 +++++++++++++++++++++++++++++++ mail_credentials.json | 8 ++++++ tw2png.py | 36 +++++++++++++++++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 bearer.json create mode 100644 mail/__init__.py create mode 100644 mail/mail.py create mode 100644 mail_credentials.json create mode 100644 tw2png.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cc266e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +venv +mail/__pycache__ diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..b799f0f --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +requests = "*" + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..174ca63 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,57 @@ +{ + "_meta": { + "hash": { + "sha256": "acbc8c4e7f2f98f1059b2a93d581ef43f4aa0c9741e64e6253adff8e35fbd99e" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "certifi": { + "hashes": [ + "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3", + "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41" + ], + "version": "==2020.6.20" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", + "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0" + ], + "version": "==2.10" + }, + "requests": { + "hashes": [ + "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", + "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898" + ], + "index": "pypi", + "version": "==2.24.0" + }, + "urllib3": { + "hashes": [ + "sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2", + "sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e" + ], + "version": "==1.25.11" + } + }, + "develop": {} +} diff --git a/bearer.json b/bearer.json new file mode 100644 index 0000000..50f2a3d --- /dev/null +++ b/bearer.json @@ -0,0 +1,4 @@ +{ + "bearer": "insert bearer token from twitter" +} + diff --git a/mail/__init__.py b/mail/__init__.py new file mode 100644 index 0000000..21fa571 --- /dev/null +++ b/mail/__init__.py @@ -0,0 +1 @@ +from .mail import mail diff --git a/mail/mail.py b/mail/mail.py new file mode 100644 index 0000000..7a5ec3b --- /dev/null +++ b/mail/mail.py @@ -0,0 +1,41 @@ +from email.mime.text import MIMEText +from json import load as j_load +from pathlib import Path +from platform import system +from smtplib import SMTP_SSL, SMTPHeloError, SMTPAuthenticationError, SMTPException +from ssl import create_default_context + + +class mail: + + def __init__(self, what: str): + + if system().lower() == "windows": + path = Path("mail_credentials.json") + else: + path = Path("/etc/twpng/mail_credentials.json") + + with path.open("r") as fp: + credentials = j_load(fp) + + message = MIMEText(what, _charset='utf8') + message['Subject'] = "twpng error report" + message['From'] = credentials['email-sendfrom'] + message['To'] = credentials['email-sendto'] + + try: + with SMTP_SSL(host=credentials['smtp-server'], + port=credentials['smtp-port'], + context=create_default_context()) as server: + server.login(user=credentials['email-user'], password=credentials['email-password']) + server.sendmail(to_addrs=credentials['email-sendto'], + msg=message.as_string(), + from_addr=credentials['email-sendfrom']) + + except SMTPHeloError as helo: + print(f"SMTPHeloError: {helo}") + except SMTPAuthenticationError as auth_error: + print(f"Authentication Error: {auth_error}") + except SMTPException as e: + print(f"SMTPException: {e}") + diff --git a/mail_credentials.json b/mail_credentials.json new file mode 100644 index 0000000..43c11d0 --- /dev/null +++ b/mail_credentials.json @@ -0,0 +1,8 @@ +{ + "smtp-server": "mail.example.com", + "smtp-port": 465, + "email-user": "username", + "email-sendfrom": "sendingmail@example.com", + "email-password": "passw0rd", + "email-sendto": "receiver@example.com" +} diff --git a/tw2png.py b/tw2png.py new file mode 100644 index 0000000..e298cd8 --- /dev/null +++ b/tw2png.py @@ -0,0 +1,36 @@ +import requests +from pathlib import Path +from platform import system +from json import load as j_load +from mail import mail + +get_last_id_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=GothSeidank&count=1&trim_user=1" +# get_tweet_url = "https://api.twitter.com/2/tweets/1324994969590001666" + + +if system().lower() == "windows": + path = Path("bearer.json") +else: + path = Path("/etc/twpng/bearer.json") + + try: + with path.open('r') as fp: + credentials = j_load(fp) + except FileNotFoundError: + mail("Could not find bearer token file! Script was discontinued.") + exit(1) + +try: + header = { + "Authorization": f"Bearer {credentials['bearer']}" + } + + r = requests.get(url=get_last_id_url, headers=header) + r.raise_for_status() + +except Exception as e: + message = f"The api call to twitter failed and requests said:\n'{e}'. Script was discontinued." + mail(message) + exit(1) + +