new version

Moved twitter code to own class
Added pillow
Added configs for image properties
Added license headers and license
master
Domeniko Gentner 4 years ago
parent 85b9629912
commit 785ad84f48
  1. 2
      .gitignore
  2. 21
      LICENSE.md
  3. 1
      Pipfile
  4. 36
      Pipfile.lock
  5. 20
      css.json
  6. BIN
      font/open-sans-v18-latin-regular.ttf
  7. 8
      mail/__init__.py
  8. 8
      mail/mail.py
  9. 9
      painter/__init__.py
  10. 40
      painter/painter.py
  11. 48
      tw2png.py
  12. 9
      twitter/__init__.py
  13. 56
      twitter/twitter.py

2
.gitignore vendored

@ -3,3 +3,5 @@ venv
mail/__pycache__ mail/__pycache__
bearer.json bearer.json
mail_credentials.json mail_credentials.json
painter/__pycache__
test.png

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Domeniko Gentner
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.

@ -7,6 +7,7 @@ verify_ssl = true
[packages] [packages]
requests = "*" requests = "*"
pillow = "*"
[requires] [requires]
python_version = "3.8" python_version = "3.8"

36
Pipfile.lock generated

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "acbc8c4e7f2f98f1059b2a93d581ef43f4aa0c9741e64e6253adff8e35fbd99e" "sha256": "d1925e0b4d1174e1ffb7d76338272b6b9aa0bf4d958c89448f76a1e35bc4007f"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -37,6 +37,40 @@
], ],
"version": "==2.10" "version": "==2.10"
}, },
"pillow": {
"hashes": [
"sha256:006de60d7580d81f4a1a7e9f0173dc90a932e3905cc4d47ea909bc946302311a",
"sha256:0a2e8d03787ec7ad71dc18aec9367c946ef8ef50e1e78c71f743bc3a770f9fae",
"sha256:0eeeae397e5a79dc088d8297a4c2c6f901f8fb30db47795113a4a605d0f1e5ce",
"sha256:11c5c6e9b02c9dac08af04f093eb5a2f84857df70a7d4a6a6ad461aca803fb9e",
"sha256:2fb113757a369a6cdb189f8df3226e995acfed0a8919a72416626af1a0a71140",
"sha256:4b0ef2470c4979e345e4e0cc1bbac65fda11d0d7b789dbac035e4c6ce3f98adb",
"sha256:59e903ca800c8cfd1ebe482349ec7c35687b95e98cefae213e271c8c7fffa021",
"sha256:5abd653a23c35d980b332bc0431d39663b1709d64142e3652890df4c9b6970f6",
"sha256:5f9403af9c790cc18411ea398a6950ee2def2a830ad0cfe6dc9122e6d528b302",
"sha256:6b4a8fd632b4ebee28282a9fef4c341835a1aa8671e2770b6f89adc8e8c2703c",
"sha256:6c1aca8231625115104a06e4389fcd9ec88f0c9befbabd80dc206c35561be271",
"sha256:795e91a60f291e75de2e20e6bdd67770f793c8605b553cb6e4387ce0cb302e09",
"sha256:7ba0ba61252ab23052e642abdb17fd08fdcfdbbf3b74c969a30c58ac1ade7cd3",
"sha256:7c9401e68730d6c4245b8e361d3d13e1035cbc94db86b49dc7da8bec235d0015",
"sha256:81f812d8f5e8a09b246515fac141e9d10113229bc33ea073fec11403b016bcf3",
"sha256:895d54c0ddc78a478c80f9c438579ac15f3e27bf442c2a9aa74d41d0e4d12544",
"sha256:8de332053707c80963b589b22f8e0229f1be1f3ca862a932c1bcd48dafb18dd8",
"sha256:92c882b70a40c79de9f5294dc99390671e07fc0b0113d472cbea3fde15db1792",
"sha256:95edb1ed513e68bddc2aee3de66ceaf743590bf16c023fb9977adc4be15bd3f0",
"sha256:b63d4ff734263ae4ce6593798bcfee6dbfb00523c82753a3a03cbc05555a9cc3",
"sha256:bd7bf289e05470b1bc74889d1466d9ad4a56d201f24397557b6f65c24a6844b8",
"sha256:cc3ea6b23954da84dbee8025c616040d9aa5eaf34ea6895a0a762ee9d3e12e11",
"sha256:cc9ec588c6ef3a1325fa032ec14d97b7309db493782ea8c304666fb10c3bd9a7",
"sha256:d3d07c86d4efa1facdf32aa878bd508c0dc4f87c48125cc16b937baa4e5b5e11",
"sha256:d8a96747df78cda35980905bf26e72960cba6d355ace4780d4bdde3b217cdf1e",
"sha256:e38d58d9138ef972fceb7aeec4be02e3f01d383723965bfcef14d174c8ccd039",
"sha256:eb472586374dc66b31e36e14720747595c2b265ae962987261f044e5cce644b5",
"sha256:fbd922f702582cb0d71ef94442bfca57624352622d75e3be7a1e7e9360b07e72"
],
"index": "pypi",
"version": "==8.0.1"
},
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b",

@ -0,0 +1,20 @@
{
"dimensions": {
"x": 600,
"y": 300
},
"background": {
"r": 57,
"g": 63,
"b": 77
},
"text": {
"font": "font/open-sans-v18-latin-regular.ttf",
"size": 18,
"color": {
"r": 255,
"g": 255,
"b": 255
}
}
}

@ -1 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from .mail import mail from .mail import mail

@ -1,3 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from email.mime.text import MIMEText from email.mime.text import MIMEText
from json import load as j_load from json import load as j_load
from pathlib import Path from pathlib import Path

@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from .painter import painter

@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
from json import load as j_load
class painter:
def __init__(self, twitter: str, handle: str):
# Load json
css = Path("css.json")
with css.open('r') as fp:
css = j_load(fp)
# Size
img_size = (css['dimensions']['x'], css['dimensions']['y'])
# Background color
background_color = (css["background"]['r'], css["background"]['g'], css["background"]['b'])
# text options
text = css['text']
color = (text['color']['r'], text['color']['g'], text['color']['b'])
font = text['font']
size = text['size']
image = Image.new('RGB', img_size, background_color)
font = ImageFont.truetype(font)
context = ImageDraw.Draw(image)
context.text((10, 10), handle, fill=color, font=font)
image.save('test.png')

@ -1,36 +1,16 @@
import requests #!/usr/bin/env python3
from pathlib import Path # -*- coding: utf-8 -*-
from platform import system # /**********************************************************************************
from json import load as j_load # * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from mail import mail from mail import mail
from painter import painter
from twitter import twitter
from sys import exit
get_last_id_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=GothSeidank&count=1&trim_user=1" twitter = twitter()
# get_tweet_url = "https://api.twitter.com/2/tweets/1324994969590001666" painter(twitter.get_latest_tweet(), twitter.handle())
exit(0)
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)

@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
from .twitter import twitter

@ -0,0 +1,56 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /**********************************************************************************
# * _author : Domeniko Gentner
# * _mail : code@tuxstash.de
# * _repo : https://git.tuxstash.de/gothseidank/tweet2png
# * _license : This project is under MIT License
# *********************************************************************************/
import requests
from pathlib import Path
from platform import system
from json import load as j_load
from mail import mail
from typing import Union
from sys import exit
class twitter:
def __init__(self):
self.path = str()
self.credentials = str()
if system().lower() == "windows":
self.path = Path("bearer.json")
else:
self.path = Path("/etc/twpng/bearer.json")
try:
with self.path.open('r') as fp:
self.credentials = j_load(fp)
except FileNotFoundError:
mail("Could not find bearer token file! Script was discontinued.")
exit(1)
def handle(self):
return self.credentials['twitter-handle']
def get_latest_tweet(self) -> Union[str, None]:
try:
get_url = f"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" \
f"{self.credentials['twitter-handle']}&count=1&trim_user=1"
header = {
"Authorization": f"Bearer {self.credentials['bearer']}"
}
r = requests.get(url=get_url, headers=header)
r.raise_for_status()
return r.json()[0]
except Exception as e:
message = f"The api call to twitter failed, the server responded:\n'{e}'.\nScript was discontinued."
mail(message)
exit(1)
Loading…
Cancel
Save