Moved twitter code to own class Added pillow Added configs for image properties Added license headers and licensemaster
parent
85b9629912
commit
785ad84f48
13 changed files with 223 additions and 35 deletions
@ -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. |
@ -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 |
||||
} |
||||
} |
||||
} |
Binary file not shown.
@ -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 |
||||
|
@ -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 |
||||
from pathlib import Path |
||||
from platform import system |
||||
from json import load as j_load |
||||
#!/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 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" |
||||
# 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) |
||||
|
||||
|
||||
twitter = twitter() |
||||
painter(twitter.get_latest_tweet(), twitter.handle()) |
||||
exit(0) |
||||
|
@ -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…
Reference in new issue