From 36a9ec862dfe061851a532ae54754eab2c536717 Mon Sep 17 00:00:00 2001 From: Dryusdan Date: Thu, 8 Mar 2018 23:17:54 +0100 Subject: [PATCH] first commit --- .gitignore | 4 ++++ README.md | 27 ++++++++++++++++++++++-- dlimage.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 dlimage.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3839c48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +secrets +secrets/* +saved_media +saved_media/* diff --git a/README.md b/README.md index 4438f2a..6b9a618 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,26 @@ -# Mastodon_DL_Favorites_Image +# Mastodon DL Favorites Image -This tool permit DL image of your last 40 favs \ No newline at end of file +This tool permit DL image of your last 40 favs + +## 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 +``` + +create save_media folder is the same path + +## How to run it + +``` +python dlimage.py +``` + +:) diff --git a/dlimage.py b/dlimage.py new file mode 100644 index 0000000..a70ca64 --- /dev/null +++ b/dlimage.py @@ -0,0 +1,60 @@ +#!/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 +import math + +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) +def get_media(): + media_attachments = toot.media_attachments + for media in media_attachments: + print(media.url[-20:]) + response = requests.get(media.remote_url) + pattern = Image.open(BytesIO(response.content), "r").convert('RGB') + pattern.save('save_media/'+media.url[-20:]) + +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, +) + +masto_allfav = mastodon.favourites(None, None, 40) +for toot in masto_allfav: + if toot.account.acct == "bonjourtoutlemonde@hostux.social": + get_media() + else : + if toot.account.acct == "BonjourMadame@hostux.social": + get_media() +