first commit

This commit is contained in:
Dryusdan 2017-12-23 15:42:07 +01:00
commit a2ddb80445
3 changed files with 96 additions and 0 deletions

30
README.md Normal file
View File

@ -0,0 +1,30 @@
# Hour bot with unsplash image
This bot use unsplash image to display it with special hour (morning, lunch, snack...)
## How to set up
Install Pillow Mastodon.py and requests
`pip install Pillow Mastodon.py requests`
clone it
`git clone https://github.com/Dryusdan/random-unsplash-hour.git`
create `secrets/secrets.txt` and paste this :
```
client_id: looooong
client_secret: veryyyy loooong
access_token: toooo loooooong
mastodon_hostname: miaou.drycat.fr
```
## How to use it
`./random-image.py -h --help` to display help.
```
usage: random-image.py [-h] [-p PERIOD] [--no-upload NO_UPLOAD]
optional arguments:
-h, --help show this help message and exit
-p PERIOD, --period PERIOD La période du script [morning | mid-morning | lunch | snack | apero | diner | night]
--no-upload NO_UPLOAD Don't upload image
```

66
random-image.py Executable file
View File

@ -0,0 +1,66 @@
#!/usr/bin/python3
# coding: utf-8
# -*- coding: utf-8 -*-
from PIL import Image
from io import BytesIO
from mastodon import Mastodon
import requests
import os
import sys
import time
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sex", help="Sexe of image [man | woman ]")
parser.add_argument("--no-upload", help="Don't upload image")
args = parser.parse_args()
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
# Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt"
uc_client_id = get_parameter("client_id", secrets_filepath)
uc_client_secret = get_parameter("client_secret", secrets_filepath)
uc_access_token = get_parameter("access_token", secrets_filepath)
mastodon_hostname = get_parameter("mastodon_hostname", secrets_filepath)
mastodon = Mastodon(
client_id = uc_client_id,
client_secret = uc_client_secret,
access_token = uc_access_token,
api_base_url = 'https://' + mastodon_hostname,
)
#https://source.unsplash.com/collection/1053828/1920x1080
if args.sex == "man":
collection = "1523783"
elif args.sex == "women":
collection = "1523809"
response = requests.get("https://source.unsplash.com/collection/"+collection+"/")
pattern = Image.open(BytesIO(response.content), "r").convert('RGB')
pattern.save('output.jpg')
if args.no_upload:
print("don't upload this image")
else:
media_dict = mastodon.media_post("output.jpg")
#text.encode('utf-8').strip()
mastodon.status_post("Image de unsplash", in_reply_to_id=None, media_ids=[media_dict])

BIN
segoeui.ttf Normal file

Binary file not shown.