@ -7,14 +7,18 @@ from mastodon import StreamListener
from lxml import html
from logging.handlers import RotatingFileHandler
from pprint import pprint
from random import randint
from utils.config import get_parameter , init_log , init_mastodon
from PIL import Image
from io import BytesIO
import requests , os , random , sys , time , json , logging , argparse , re
config_file = " config.txt "
secrets_filepath = " secrets/secrets.txt "
log_filepath = " activity.log "
blacklist_filepath = " blacklist.json "
config_file = " config.txt "
secrets_filepath = get_parameter ( " secrets_filepath " , config_file )
log_filepath = get_parameter ( " log_filepath " , config_file )
blacklist_filepath = get_parameter ( " blacklist_filepath " , config_file )
collection_filepath = get_parameter ( " collection_filepath " , config_file )
log = init_log ( log_filepath )
mastodon = init_mastodon ( config_file , secrets_filepath )
@ -22,23 +26,43 @@ blacklist_file = open(blacklist_filepath,'r')
BLACKLIST = json . loads ( blacklist_file . read ( ) )
blacklist_file . close ( )
mime_dict = { ' .jpg ' : ' image/jpeg ' , ' .jpe ' : ' image/jpeg ' , ' .jpeg ' : ' image/jpeg ' , ' .png ' : ' image/png ' , ' .gif ' : ' image/gif ' }
def post_img ( mastodon , text , log , config ) :
img_path = get_parameter ( " img_path " , config )
file = random . choice ( os . listdir ( img_path + " / " ) )
image_byte = open ( img_path + " / " + file , " rb " ) . read ( )
def post_img_local ( mastodon , text , log , config ) :
img_path = get_parameter ( " img_path " , config )
file = random . choice ( os . listdir ( img_path + " / " ) )
image_byte = open ( img_path + " / " + file , " rb " ) . read ( )
file , ext = os . path . splitext ( file )
mime_dict = { ' .jpg ' : ' image/jpeg ' , ' .jpe ' : ' image/jpeg ' , ' .jpeg ' : ' image/jpeg ' , ' .png ' : ' image/png ' , ' .gif ' : ' image/gif ' }
file , ext = os . path . splitext ( file )
#mime = mime_dict[str.lower(ext)]
try :
mime = mime_dict [ str . lower ( ext ) ]
except KeyError :
mime = None ;
log . error ( ext + " is not present on mime_dict, please add this " )
pass
media_dict = mastodon . media_post ( image_byte , mime )
return media_dict ;
def post_img_distant ( mastodon , text , log , config ) :
collection_url = get_parameter ( " collection_url " , config )
collecion_file = open ( collection_filepath , ' r ' )
collections = json . loads ( collecion_file . read ( ) )
collecion_file . close ( )
count_collection = len ( collections ) - 1
id_collection = randint ( 0 , count_collection )
mime = mime_dict [ str . lower ( ext ) ]
#try:
#except KeyError:
# mime = None;
# pass
collection_url = collection_url . replace ( " <collection> " , str ( collections [ id_collection ] ) )
response = requests . get ( collection_url )
pattern = Image . open ( BytesIO ( response . content ) , " r " ) . convert ( ' RGB ' )
pattern . save ( ' output.jpg ' )
media_dict = mastodon . media_post ( image_byte , mime )
media_dict = mastodon . media_post ( " output.jpg " )
return media_dict ;
def cleanhtml ( raw_html ) :
@ -48,6 +72,9 @@ def cleanhtml(raw_html):
class BotListener ( StreamListener ) :
def __init__ ( self , args ) :
self . args = args
# use only notification
def on_notification ( self , notification ) :
@ -147,7 +174,12 @@ class BotListener(StreamListener):
sensitive = True
else :
sensitive = False
media_dict = post_img ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
if self . args . source == " local " :
media_dict = post_img_local ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
elif self . args . source == " distant " :
media_dict = post_img_distant ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
mastodon . status_post ( text , None , media_ids = [ media_dict ] , sensitive = sensitive , visibility = visibility , spoiler_text = get_parameter ( " spoiler_text " , config_file ) )
else :
log . debug ( " no picture send :( " )
@ -158,29 +190,35 @@ class BotListener(StreamListener):
def main ( ) :
parser = argparse . ArgumentParser ( description = ' Choose between image or streaming ' )
parser . add_argument ( " -i " , " --img " , action = ' store_true ' , help = " post image " )
parser . add_argument ( " -s " , " --stream " , action = " store_true " , help = " stream user profile " )
args = parser . parse_args ( )
if args . img :
media_dict = post_img ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
if get_parameter ( " sensitive " , config_file ) == " yes " :
sensitive = True
else :
sensitive = False
mastodon . status_post ( get_parameter ( " default_text " , config_file ) , None , media_ids = [ media_dict ] , sensitive = sensitive , visibility = ' public ' , spoiler_text = get_parameter ( " spoiler_text " , config_file ) )
sys . exit ( )
elif args . stream :
stream = BotListener ( ) ;
while True :
try :
log . info ( " Start listening... " )
mastodon . stream_user ( stream )
except Exception as error :
log . warning ( ' General exception caught: ' + str ( error ) )
time . sleep ( 0.5 )
else :
print ( " Require an argument. Use --help to display help " )
parser = argparse . ArgumentParser ( description = ' Choose between image or streaming ' )
parser . add_argument ( " -i " , " --img " , action = ' store_true ' , help = " post image " )
parser . add_argument ( " -s " , " --source " , help = " Source of image [ local | distant ] " )
parser . add_argument ( " --stream " , action = " store_true " , help = " stream user profile " )
args = parser . parse_args ( )
if args . img :
if args . source == " local " :
media_dict = post_img_local ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
elif args . source == " distant " :
media_dict = post_img_distant ( mastodon , get_parameter ( " default_text " , config_file ) , log , config_file )
if get_parameter ( " sensitive " , config_file ) == " yes " :
sensitive = True
else :
sensitive = False
mastodon . status_post ( get_parameter ( " default_text " , config_file ) , None , media_ids = [ media_dict ] , sensitive = sensitive , visibility = ' public ' , spoiler_text = get_parameter ( " spoiler_text " , config_file ) )
sys . exit ( )
elif args . stream :
stream = BotListener ( args ) ;
while True :
try :
log . info ( " Start listening... " )
mastodon . stream_user ( stream )
except Exception as error :
log . warning ( ' General exception caught: ' + str ( error ) )
time . sleep ( 0.5 )
else :
print ( " Require an argument. Use --help to display help " )
main ( )