5 Commits

Author SHA1 Message Date
  Dryusdan fa1550e26a Merge branch 'improve-randomness' of framasky/masto-image-bot into master 1 year ago
  Luc Didry e5e4938479
Improve local images randomness by using random.SystemRandom() 1 year ago
  Dryusdan 67849b7f9f Remove quote for unsplash_client_id 1 year ago
  Tristan Le Chanony 38c1afa81a Improve README 2 years ago
  Tristan Le Chanony d7287622b9 Improve unsplash mode 2 years ago
3 changed files with 17 additions and 2 deletions
Split View
  1. +1
    -0
      README.md
  2. +15
    -2
      bot.py
  3. +1
    -0
      config.sample.txt

+ 1
- 0
README.md View File

@ -32,6 +32,7 @@ If you don't want any "spoiler text", just leave the line empty.
| limit | Limit send per minute per person | int |
| limit_hour | Limit send par hour per person | int |
| collection_url | URL of website you deserve image. `<collection>` is a variable who depend on collection.json (you can remove this variable) | string |
| unsplash_client_id | Access key of your Unsplash App (you can create it on api.unsplash.com ) | string |
Copy `blacklist.sample.json` to `blacklist.json` and replace or add accounts that should not receive any image


+ 15
- 2
bot.py View File

@ -32,7 +32,8 @@ def post_img_local(mastodon, text, log, config):
continu = True;
while continu:
file = random.choice(os.listdir(img_path+"/"))
secure_random = random.SystemRandom()
file = secure_random.choice(os.listdir(img_path+"/"))
if os.path.isdir(img_path+file):
img_path = img_path+file+"/"
else:
@ -69,7 +70,19 @@ def post_img_local(mastodon, text, log, config):
return media_dict;
def post_unsplash_random_image(mastodon, log, config):
response = requests.get('https://api.unsplash.com/photos/random?client_id=03ad5bfbaa0acd6c96a728d425e533683ec25e5fb7fcf99f6461720b3d0d75a1')
collection_url = get_parameter("collection_url", config)
unsplash_client_id = get_parameter("unsplash_client_id", config)
collecion_file = open(collection_filepath,'r')
collections = json.loads(collecion_file.read())
collecion_file.close()
count_collection = len(collections)-1
if count_collection > -1:
id_collection = randint(0,count_collection)
collection_url="&collections="+str(collections[id_collection])
else:
collection_url=''
response = requests.get("https://api.unsplash.com/photos/random?client_id="+unsplash_client_id+collection_url)
randim_json = json.loads(response.text)
randim_url = "{}&q=85&crop=entropy&cs=tinysrgb&w=2048&fit=max".format(randim_json['urls']['raw'])


+ 1
- 0
config.sample.txt View File

@ -10,3 +10,4 @@ spoiler_text: some text here
limit: 2
limit_hour: 10
collection_url: https://source.unsplash.com/collection/<collection>/
unsplash_client_id: 03ad5bfbaa0acd6c96a728d425e533683ec25e5fb7fcf99f6461720b3d0d75a1

Loading…
Cancel
Save