Compare commits

...

5 Commits

Author SHA1 Message Date
Dryusdan fa1550e26a Merge branch 'improve-randomness' of framasky/masto-image-bot into master 2019-09-12 12:05:48 +02:00
Luc Didry e5e4938479
Improve local images randomness by using random.SystemRandom()
My bot @gracyimp@botsin.space gave me the same image twice this morning,
within a few seconds, so I asked myself how to improve the randomness.
Indeed, this allowed my bot to publish some images that never were
published!
2019-09-04 09:24:26 +02:00
Dryusdan 67849b7f9f Remove quote for unsplash_client_id 2019-06-22 10:49:11 +02:00
Tristan Le Chanony 38c1afa81a Improve README 2019-01-07 13:52:34 +01:00
Tristan Le Chanony d7287622b9 Improve unsplash mode 2019-01-07 13:50:35 +01:00
3 changed files with 17 additions and 2 deletions

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

17
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'])

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