2023-01-23 12:34:02 -08:00
#!/usr/bin/env python
2023-01-16 00:23:32 -08:00
import random
2023-01-23 12:53:41 -08:00
import requests
import json
2023-01-23 12:34:02 -08:00
from getpass import getpass
2023-01-16 00:23:32 -08:00
import os
2023-01-23 12:53:41 -08:00
from urllib . parse import urlencode
2023-01-16 00:23:32 -08:00
2023-01-23 12:53:41 -08:00
#Ensure Credentials
parent = os . path . dirname ( os . path . realpath ( __file__ ) )
try :
os . mkdir ( os . path . join ( parent , ' .creds ' ) )
except FileExistsError :
pass
2023-01-16 00:23:32 -08:00
2023-01-23 12:53:41 -08:00
#Create app
try :
with open ( os . path . join ( parent , ' .creds ' , ' client.secret ' ) , " x " ) as clientcreds :
instance = input ( ' Please enter your instance: ' )
if not instance [ : 4 ] == ' http ' :
instance = ' https:// ' + instance
response = requests . post ( instance + ' /api/v1/apps ' , data = { ' client_name ' : ' Hourly lgf ' , ' scopes ' : ' write:statuses ' , ' redirect_uris ' : ' urn:ietf:wg:oauth:2.0:oob ' } )
response = response . json ( )
print ( response )
clientcreds . write ( response [ ' client_id ' ] + " \n " )
clientcreds . write ( response [ ' client_secret ' ] + " \n " )
clientcreds . write ( instance + " \n " )
except FileExistsError :
pass
2023-01-16 00:23:32 -08:00
2023-01-23 12:53:41 -08:00
#Fetch information from app credential file
with open ( os . path . join ( parent , ' .creds ' , ' client.secret ' ) , " r " ) as clientcreds :
lines = clientcreds . read ( ) . split ( ' \n ' )
client_id = lines [ 0 ]
client_secret = lines [ 1 ]
instance = lines [ 2 ]
2023-01-16 00:23:32 -08:00
2023-01-23 12:53:41 -08:00
#Log in to bot account
try :
with open ( os . path . join ( parent , ' .creds ' , ' user.secret ' ) , " x " ) as usercreds :
print ( instance + ' /oauth/authorize? ' , urlencode ( { ' response_type ' : ' code ' , ' client_id ' : client_id , ' redirect_uri ' : ' urn:ietf:wg:oauth:2.0:oob ' , ' scope ' : ' write:statuses ' } ) )
code = input ( " To generate a token to access your account, lgfbot needs an authorization code. Please authorize using the link above and enter the code it provides you \n Code: " )
response = requests . post ( instance + ' /oauth/token ' , data = { ' grant_type ' : ' authorization_code ' , ' code ' : code , ' client_id ' : client_id , ' client_secret ' : client_secret , ' redirect_uri ' : ' urn:ietf:wg:oauth:2.0:oob ' , ' scope ' : ' write:statuses ' } )
usercreds . write ( response . json ( ) [ ' access_token ' ] )
except FileExistsError :
pass
2023-01-16 00:23:32 -08:00
2023-01-23 12:53:41 -08:00
#Fetch information from user credential file
with open ( os . path . join ( parent , ' .creds ' , ' user.secret ' ) , " r " ) as usercreds :
lines = usercreds . read ( ) . split ( ' \n ' )
token = lines [ 0 ]
session = requests . Session ( )
session . headers . update ( { " Authorization " : " Bearer " + token } )
2023-01-16 00:23:32 -08:00
2023-01-23 12:34:02 -08:00
with open ( ' adjectives.txt ' ) as f :
adjectives = list ( map ( str . rstrip , f ) )
with open ( ' verbs.txt ' ) as f :
verbs = list ( map ( str . rstrip , f ) )
postform = random . choice ( (
2023-01-23 16:26:13 -08:00
' {adjective} {gender} who {verb} ' ,
2023-01-23 12:34:02 -08:00
' {adjective} {gender} ' ,
' {gender} who {verb} ' ,
) )
2023-01-23 16:26:13 -08:00
2023-01-23 12:34:02 -08:00
gender = random . choice ( ( ' lgf ' , ' lbf ' , ' elf ' ) )
2023-01-16 00:23:32 -08:00
verb = random . choice ( verbs )
adjective = random . choice ( adjectives )
2023-01-23 12:34:02 -08:00
content = postform . format ( adjective = adjective , gender = gender , verb = verb )
2023-01-16 00:23:32 -08:00
print ( content )
2023-01-23 16:26:13 -08:00
session . post ( instance + ' /api/v1/statuses ' , data = { ' status ' : content , ' visibility ' : ' unlisted ' } )