Browse Source

Merge branch 'add-unsplash-random-source' of framasky/masto-image-bot into master

tags/v1.6rc1
Dryusdan 8 months ago
parent
commit
1a8f9a381a
2 changed files with 27 additions and 3 deletions
  1. 1
    1
      README.md
  2. 26
    2
      bot.py

+ 1
- 1
README.md View File

@@ -48,7 +48,7 @@ optional arguments:
-h, --help show this help message and exit
-i, --img post image
-s SOURCE, --source SOURCE
Source of image [ local | distant ]
Source of image [ local | distant | unsplash-random ]
--stream stream user profile

```

+ 26
- 2
bot.py View File

@@ -68,6 +68,21 @@ def post_img_local(mastodon, text, log, config):
media_dict = mastodon.media_post(image_byte, mime)
return media_dict;

def post_unsplash_random_image(mastodon, log, config):
response = requests.get('https://api.unsplash.com/photos/random?client_id=03ad5bfbaa0acd6c96a728d425e533683ec25e5fb7fcf99f6461720b3d0d75a1')
randim_json = json.loads(response.text)
randim_url = "{}&q=85&crop=entropy&cs=tinysrgb&w=2048&fit=max".format(randim_json['urls']['raw'])

img_response = requests.get(randim_url)
pattern = Image.open(BytesIO(img_response.content), "r").convert('RGB')
pattern.save('output.jpg')

media_dict = mastodon.media_post("output.jpg")

toot = "Shot by {} ({})\n{}".format(randim_json['user']['name'], randim_json['user']['links']['html'], randim_json['links']['html'])

return { 'media_dict': media_dict, 'toot': toot };

def post_img_distant(mastodon, text, log, config):

collection_url = get_parameter("collection_url", config)
@@ -201,6 +216,10 @@ class BotListener(StreamListener):
media_dict = post_img_local(mastodon, get_parameter("default_text", config_file), log, config_file)
elif self.args.source == "distant":
media_dict = post_img_distant(mastodon, get_parameter("default_text", config_file), log, config_file)
elif self.args.source == "unsplash-random":
resp = post_unsplash_random_image(mastodon, log, config_file)
text = text + "\n" + resp['toot']
media_dict = resp['media_dict']

mastodon.status_post(text, id, media_ids=[media_dict], sensitive=sensitive, visibility=visibility, spoiler_text=get_parameter("spoiler_text", config_file))
else:
@@ -214,20 +233,25 @@ def main():

parser = argparse.ArgumentParser(description='Choose between image or streaming')
parser.add_argument("-i", "--img", action='store_true', help="post image")
parser.add_argument("-s", "--source", help="Source of image [ local | distant ]")
parser.add_argument("-s", "--source", help="Source of image [ local | distant | unsplash-random ]")
parser.add_argument("--stream", action="store_true", help="stream user profile")
args = parser.parse_args()
if args.img:
text = get_parameter("default_text", config_file)
if args.source == "local":
media_dict = post_img_local(mastodon, get_parameter("default_text", config_file), log, config_file)
elif args.source == "distant":
media_dict = post_img_distant(mastodon, get_parameter("default_text", config_file), log, config_file)
elif args.source == "unsplash-random":
resp = post_unsplash_random_image(mastodon, log, config_file)
text = resp['toot']
media_dict = resp['media_dict']

if get_parameter("sensitive", config_file) == "yes":
sensitive = True
else:
sensitive = False
mastodon.status_post(get_parameter("default_text", config_file), None, media_ids=[media_dict], sensitive=sensitive, visibility='public', spoiler_text=get_parameter("spoiler_text", config_file))
mastodon.status_post(text, None, media_ids=[media_dict], sensitive=sensitive, visibility='public', spoiler_text=get_parameter("spoiler_text", config_file))
sys.exit()

elif args.stream:

Loading…
Cancel
Save