2022-11-23 01:07:54 -08:00
#!/usr/bin/python3.8
import sys
import os
import re
import argparse
import time
2023-01-26 19:40:26 -08:00
import json
import requests
parent = os . path . dirname ( os . path . realpath ( __file__ ) )
if not os . path . exists ( os . path . join ( parent , ' config.json ' ) ) :
generate_config ( " Wardyn ' s feditools " , " read write follow push " )
with open ( os . path . join ( parent , ' config.json ' ) , ' r ' ) as config_file :
config = json . load ( config_file )
session = requests . Session ( )
session . headers . update ( { " Authorization " : " Bearer " + config [ ' user_token ' ] } )
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
parser = argparse . ArgumentParser ( description = ' Remove follow requests that match the following filters: \n 1: Has less than n (defaults to 1) posts \n 2: Has no profile picture \n 3: Has no bio \n ' , formatter_class = argparse . RawTextHelpFormatter )
2022-11-23 01:07:54 -08:00
parser . add_argument ( ' -t ' , ' --threshold ' ,
action = ' store ' ,
type = int ,
help = ' The amount of filters the request has to pass to not be automatically declined, defaults to 1 (setting this to a number higher than the amount of availible filters will deny every request in the list) ' ,
default = 1 ,
dest = ' threshold '
)
parser . add_argument ( ' -i ' , ' --instances ' ,
action = ' store ' ,
type = str ,
help = ' block follow requests from these instances (comma seperated list) ' ,
default = None ,
dest = ' instances '
)
2023-01-26 19:40:26 -08:00
parser . add_argument ( ' -b ' , ' --blank ' ,
action = ' store ' ,
type = str ,
default = None ,
help = ' If your instance has a custom image for blank pfps set, please specify the url to the image with this flag ' ,
dest = ' custompfp '
)
2022-11-23 01:07:54 -08:00
parser . add_argument ( ' -p ' , ' --posts ' ,
action = ' store ' ,
type = int ,
help = ' The minimum number of posts a user must have to pass the posts filter (does not include boosts), defaults to 1 ' ,
default = 1 ,
dest = ' minposts '
)
parser . add_argument ( ' -y ' , ' --yes ' ,
action = ' store_const ' ,
const = True ,
help = ' Skips asking for confirmation before denying requests, use this if you want to automate this script. ' ,
default = False ,
dest = ' auto '
)
parser . add_argument ( ' -s ' , ' --simulate ' ,
action = ' store_const ' ,
const = True ,
help = ' Simulates the process without modifying anything ' ,
default = False ,
dest = ' simulate '
)
parser . add_argument ( ' -a ' , ' --accept ' ,
action = ' store_const ' ,
const = True ,
help = ' Automatically accept requests that arent rejected ' ,
default = False ,
dest = ' accept '
)
parser . add_argument ( ' -l ' , ' --loop ' ,
action = ' store ' ,
type = int ,
help = ' Loop command every x hours ' ,
default = None ,
dest = ' loop '
)
args = parser . parse_args ( )
threshold = args . threshold
minposts = args . minposts
auto = args . auto
simulate = args . simulate
accept = args . accept
loop = args . loop
instances = args . instances
2023-01-26 19:40:26 -08:00
custompfp = args . custompfp
2022-11-23 01:07:54 -08:00
if instances is not None :
instances = instances . split ( " , " )
if type ( loop ) == int :
loop = loop * 60 * 60
2023-01-26 19:40:26 -08:00
blanks = [ config [ ' instance ' ] + ' /avatars/original/missing.png ' , config [ ' instance ' ] + ' /images/avi.png ' ]
if custompfp :
blanks . append ( custompfp )
2022-11-23 01:07:54 -08:00
2023-01-26 19:40:26 -08:00
def filterposts ( min , follow_request ) :
posts = follow_request [ ' statuses_count ' ]
2022-11-23 01:07:54 -08:00
if posts > = min :
return ( True )
else :
return ( False )
2023-01-26 19:40:26 -08:00
def filterbio ( follow_request ) :
if follow_request [ ' note ' ] == ' <p></p> ' :
2022-11-23 01:07:54 -08:00
return ( False )
else :
return ( True )
2023-01-26 19:40:26 -08:00
def filterpfp ( follow_request ) :
if follow_request [ ' avatar ' ] in blanks :
2022-11-23 01:07:54 -08:00
return ( False )
else :
return ( True )
while True :
denied = [ ]
accepted = [ ]
2023-01-26 19:40:26 -08:00
follow_requests = session . get ( config [ ' instance ' ] + ' /api/v1/follow_requests ' , params = { ' limit ' : 80 } ) . json ( )
if len ( follow_requests ) > 0 :
for follow_request in follow_requests :
postcheck = filterposts ( minposts , follow_request )
pfpcheck = filterpfp ( follow_request )
biocheck = filterbio ( follow_request )
following = session . get ( config [ ' instance ' ] + ' /api/v1/accounts/relationships ' , params = { ' id ' : follow_request [ ' id ' ] } ) . json ( ) [ 0 ] [ ' following ' ]
2022-11-23 01:07:54 -08:00
blockedinstance = False
if instances is not None :
2023-01-26 19:40:26 -08:00
blockedinstance = follow_request [ ' fqn ' ] . split ( ' @ ' ) [ - 1 ] in instances
2022-11-23 01:07:54 -08:00
if postcheck + biocheck + pfpcheck < threshold and following == False or blockedinstance == True :
2023-01-26 19:40:26 -08:00
denied . append ( follow_request )
2022-11-23 01:07:54 -08:00
elif accept == True :
2023-01-26 19:40:26 -08:00
accepted . append ( follow_request )
2022-11-23 01:07:54 -08:00
print ( ' DENIED: ' )
2023-01-26 19:40:26 -08:00
for follow_request in denied :
print ( follow_request [ ' fqn ' ] )
2022-11-23 01:07:54 -08:00
confirm = None
if auto == False :
while True :
confirm = input ( ' Deny these requests? (y/n): ' )
if confirm == ' n ' :
pass
elif not confirm == ' y ' :
print ( ' Not recognized, try again ' )
continue
break
if auto == True or confirm == ' y ' :
2023-01-26 19:40:26 -08:00
for follow_request in denied :
2022-11-23 01:07:54 -08:00
if simulate :
2023-01-26 19:40:26 -08:00
print ( ' This is where ' + follow_request [ ' fqn ' ] + ' would be rejected ' )
2022-11-23 01:07:54 -08:00
else :
2023-01-26 19:40:26 -08:00
session . post ( config [ ' instance ' ] + ' /api/v1/follow_requests/ ' + follow_request [ ' id ' ] + ' /reject ' )
for follow_request in accepted :
2022-11-23 01:07:54 -08:00
if simulate :
2023-01-26 19:40:26 -08:00
print ( ' This is where ' + follow_request [ ' fqn ' ] + ' would be accepted ' )
2022-11-23 01:07:54 -08:00
else :
2023-01-26 19:40:26 -08:00
session . post ( config [ ' instance ' ] + ' /api/v1/follow_requests/ ' + follow_request [ ' id ' ] + ' /authorize ' )
2022-11-23 01:07:54 -08:00
if loop == None :
break
else :
time . sleep ( loop )