mau.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python
  2. #
  3. import requests
  4. import sys
  5. import magic
  6. import time
  7. import string
  8. import random
  9. import mimetypes
  10. import logging
  11. import config
  12. from glob import glob
  13. # logging.basicConfig(filename='mau.log',level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') # Set up log file and level
  14. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  15. message = sys.argv[1]
  16. description = sys.argv[2]
  17. visibility = sys.argv[3]
  18. pics_path = sys.argv[4]
  19. def get_pics():
  20. imgfiles = []
  21. for file in glob(f'{pics_path}/*.jpg'):
  22. imgfiles.append(file)
  23. for file in glob(f'{pics_path}/*.png'):
  24. imgfiles.append(file)
  25. logging.debug(f'{imgfiles}')
  26. return imgfiles
  27. def do_upload(msg, media_ids):
  28. logging.info(f'Posting to Mastodon...')
  29. auth_data = f'\"Bearer {config.access_token}'
  30. response = requests.post(f'{config.instance_url}/api/v1/statuses',
  31. params={'status': msg, 'media_ids[]': media_ids},
  32. headers={'Authorization': auth_data,
  33. 'visibility': 'public'})
  34. json_response = response.json()
  35. def img_upload(description, media_info): # Upload the image, return the media id
  36. logging.info(f'Uploading image {media_info}')
  37. auth_data = f'\"Bearer {config.access_token}'
  38. response = requests.post(f'{config.instance_url}/api/v2/media',
  39. files={'file': media_info},
  40. params={'description': description},
  41. headers={'Authorization': auth_data})
  42. json_response = response.json()
  43. id = json_response['id']
  44. logging.debug(f'{id}')
  45. return id
  46. def guess_type(media_file): # Get the mime type of the file
  47. mime_type = None
  48. try:
  49. mime_type = magic.from_file(media_file, mime=True)
  50. except AttributeError:
  51. mime_type = mimetypes.guess_type(media_file)[0]
  52. logging.debug(f'It looks like this is a {mime_type}.')
  53. return mime_type
  54. media_ids = []
  55. for image in get_pics():
  56. mime_type = guess_type(image) # Get the mime type
  57. random_suffix = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) # Create a random suffix for our file name so we don't upload the same file name more than once
  58. file_name = "mau_upload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type)
  59. info = (file_name, open(image, 'rb'), mime_type) # Get the image, and create multipart form data
  60. logging.info(f'{image}...\n')
  61. media_id = img_upload(description,info) # Upload the image, get the id
  62. media_ids.append(media_id) # Append to the media_ids list
  63. logging.info(f'Current list: {media_ids}')
  64. logging.info(f'Pausing, so the server has time to process the image\n')
  65. time.sleep(10)
  66. if len(sys.argv) < 5:
  67. print('Usage: python mau.py \"<message>\" \"<alt text>\" \"<public, unlisted, private>\" \"</path/to/photo/folder/\"')
  68. else:
  69. do_upload(message, media_ids) # Post the image
  70. logging.info(f'Done.')
  71. sys.exit()