156 lines
5.0 KiB
Python
156 lines
5.0 KiB
Python
|
#!/usr/bin/python3
|
||
|
# coding: utf-8
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
|
||
|
from mastodon import Mastodon
|
||
|
import os
|
||
|
import time
|
||
|
import sys
|
||
|
import argparse
|
||
|
from configparser import ConfigParser
|
||
|
import ytscraper
|
||
|
|
||
|
__prog_name__ = 'tootytvideo'
|
||
|
__version__ = '0.1'
|
||
|
__description__ = 'Toot a youtube video on Mastodon'
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
# Read arguments
|
||
|
parser = argparse.ArgumentParser(prog=__prog_name__,
|
||
|
description=__description__)
|
||
|
parser.add_argument('--version', action='version', version=__version__)
|
||
|
parser.add_argument('-c', '--config', default='config.ini',
|
||
|
help="Config file (default: %(default)s)")
|
||
|
args = parser.parse_args()
|
||
|
config_filepath = args.config
|
||
|
|
||
|
# Check if config file exists
|
||
|
if not os.path.isfile(config_filepath):
|
||
|
print("Config file %s not found, exiting." % config_filepath)
|
||
|
sys.exit(0)
|
||
|
else:
|
||
|
# Read config file
|
||
|
conf = ConfigParser()
|
||
|
conf.read(config_filepath)
|
||
|
|
||
|
# Overwrite default settings
|
||
|
if conf.has_option('toot', 'status'):
|
||
|
toot_status = conf['toot']['status'] + '\n'
|
||
|
else:
|
||
|
toot_status = ''
|
||
|
|
||
|
if conf.has_option('toot', 'visibility'):
|
||
|
toot_visibility = conf['toot']['visibility']
|
||
|
else:
|
||
|
toot_visibility = ''
|
||
|
|
||
|
if conf.has_option('toot', 'sensitive'):
|
||
|
toot_sensitive = conf['toot']['sensitive']
|
||
|
else:
|
||
|
toot_sensitive = False
|
||
|
|
||
|
if conf.has_option('toot', 'spoiler'):
|
||
|
toot_spoiler = conf['toot']['spoiler']
|
||
|
else:
|
||
|
toot_spoiler = None
|
||
|
|
||
|
# Log into Mastodon if enabled in settings
|
||
|
print('[ OK ] {} Login'.format(time.strftime('%H:%M')))
|
||
|
mastodon_hostname = conf['mastodon']['mastodon_hostname']
|
||
|
try:
|
||
|
mastodonAPI = Mastodon(
|
||
|
client_id=conf['mastodon']['client_id'],
|
||
|
client_secret=conf['mastodon']['client_secret'],
|
||
|
access_token=conf['mastodon']['access_token'],
|
||
|
api_base_url='https://' + mastodon_hostname
|
||
|
)
|
||
|
masto_username = mastodonAPI.account_verify_credentials()['username']
|
||
|
print('[ OK ] {} Sucessfully authenticated on {} as @{}'.format(
|
||
|
time.strftime('%H:%M'), mastodon_hostname, masto_username))
|
||
|
except BaseException as e:
|
||
|
print('[ERROR] Error while logging into Mastodon:', str(e))
|
||
|
sys.exit(0)
|
||
|
# Run
|
||
|
resp = True
|
||
|
|
||
|
while resp:
|
||
|
print('+- New toot -+')
|
||
|
yturl = input('Youtube URL : ')
|
||
|
comment = input('Add a comment : ')
|
||
|
hashtag = input('Add hastag : ')
|
||
|
|
||
|
# Prepare the toot
|
||
|
if conf.has_option('toot', 'status'):
|
||
|
toot_status = conf['toot']['status'] + '\n'
|
||
|
else:
|
||
|
toot_status = ''
|
||
|
# video commentary
|
||
|
toot_status += comment
|
||
|
toot_status += '\n'
|
||
|
# video title
|
||
|
video = ytscraper.scrape_url(yturl)
|
||
|
toot_status += video.title
|
||
|
toot_status += '\n'
|
||
|
toot_status += video.yturl
|
||
|
# add hashtag
|
||
|
toot_status += '\n'
|
||
|
toot_status += conf['toot']['hashtag']
|
||
|
toot_status += ' '
|
||
|
toot_status += hashtag
|
||
|
|
||
|
# Prepare media IDs
|
||
|
try:
|
||
|
mfile = ytscraper.download_image(
|
||
|
video.thumbnail,
|
||
|
video.videoid,
|
||
|
'.'
|
||
|
)
|
||
|
image_byte = open(mfile, "rb").read()
|
||
|
|
||
|
if mfile[-3:] == "jpe":
|
||
|
mime = "image/jpeg"
|
||
|
elif mfile[-3:] == "jpg":
|
||
|
mime = "image/jpeg"
|
||
|
elif mfile[-3:] == "png":
|
||
|
mime = "image/png"
|
||
|
elif mfile[-3:] == "gif":
|
||
|
mime = "image/gif"
|
||
|
elif mfile[-3:] == "gifv":
|
||
|
mime = "video/mp4"
|
||
|
elif mfile[-3:] == "mp4":
|
||
|
mime = "video/mp4"
|
||
|
else:
|
||
|
print("Incorrect media file format")
|
||
|
|
||
|
media_dict = mastodonAPI.media_post(image_byte, mime)
|
||
|
except BaseException as e:
|
||
|
print ('[ERROR] Error while reading media file : ' + str(e))
|
||
|
sys.exit(0)
|
||
|
|
||
|
# Post the toot
|
||
|
try:
|
||
|
status = mastodonAPI.status_post(
|
||
|
status=toot_status,
|
||
|
in_reply_to_id=None,
|
||
|
media_ids=[media_dict],
|
||
|
sensitive=toot_sensitive,
|
||
|
visibility=toot_visibility,
|
||
|
spoiler_text=toot_spoiler
|
||
|
)
|
||
|
print('[ OK ] {} Posting this on Mastodon account : {}'.format(
|
||
|
time.strftime('%H:%M'), status['url']))
|
||
|
except BaseException as e:
|
||
|
print('[ERROR] Error while posting toot:' + str(e))
|
||
|
sys.exit(0)
|
||
|
|
||
|
# make a pause
|
||
|
r = input('Continue (y/n)? ')
|
||
|
if r.lower() == 'y':
|
||
|
resp = True
|
||
|
else:
|
||
|
resp = False
|
||
|
|
||
|
# The End
|
||
|
print('[ OK ] {} The show is over !'.format(time.strftime('%H:%M')))
|