110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
# Import modules
|
|
from mastodon import Mastodon
|
|
import os
|
|
import html2text
|
|
from argparse import ArgumentParser
|
|
import os
|
|
|
|
# Initialize arguments
|
|
parser = ArgumentParser(description='Search a fedi users posts for a specific word or phrase')
|
|
parser.add_argument('-c', '--case',
|
|
action='store_const',
|
|
const=True,
|
|
help='Match string as case sensitive',
|
|
default=False,
|
|
dest='case'
|
|
)
|
|
parser.add_argument('-d', '--dms',
|
|
action='store_const',
|
|
const=True,
|
|
help='Include DMs',
|
|
default=False,
|
|
dest='dms'
|
|
)
|
|
parser.add_argument('account',
|
|
type=str,
|
|
help='Account to search through',
|
|
)
|
|
parser.add_argument('pattern',
|
|
type=str,
|
|
help='Pattern to search for',
|
|
|
|
)
|
|
args = parser.parse_args()
|
|
case = args.case
|
|
dms = args.dms
|
|
account = args.account
|
|
pattern = args.pattern
|
|
|
|
# Ensure Fediverse credentials
|
|
parent = os.path.dirname(os.path.realpath(__file__))
|
|
if os.path.exists(os.path.join(parent, '.creds')) == False:
|
|
os.mkdir(os.path.join(parent, '.creds'))
|
|
|
|
if os.path.exists(os.path.join(parent, '.creds', 'client.secret')) == False:
|
|
instance = input('Please enter your instance: ')
|
|
if not instance[:4] == 'http':
|
|
instance = 'https://' + instance
|
|
Mastodon.create_app('Wardyns fedi tools', api_base_url = instance, to_file = os.path.join(parent, '.creds', 'client.secret'))
|
|
|
|
mastodon = Mastodon(
|
|
client_id = os.path.join(parent, '.creds', 'client.secret'),
|
|
)
|
|
|
|
if os.path.exists(os.path.join(parent, '.creds', 'user.secret')) == False:
|
|
username = input('Enter your username: ')
|
|
password = input('Enter your password: ')
|
|
mastodon.log_in(username=username, password=password, scopes=['read', 'write'], to_file=os.path.join(parent, '.creds', 'user.secret'))
|
|
|
|
mastodon = Mastodon(
|
|
access_token = os.path.join(parent, '.creds', 'user.secret')
|
|
)
|
|
|
|
# Main block
|
|
if case == False:
|
|
pattern = pattern.lower()
|
|
oldest_status_id = None
|
|
htmlconvert = html2text.HTML2Text()
|
|
htmlconvert.ignore_links = True
|
|
htmlconvert.body_width = 0
|
|
if account[0] == '@':
|
|
account = account[1:]
|
|
if len(account.split('@')) == 1:
|
|
account = account + '@' + mastodon.api_base_url.split('/')[2]
|
|
accountlist = mastodon.account_search(account)
|
|
|
|
for curaccount in accountlist:
|
|
print(curaccount['fqn'])
|
|
if curaccount['fqn'].lower() == account.lower():
|
|
account = curaccount
|
|
break
|
|
if type(account) is str:
|
|
print('Could not find an account with the search term: ' + account)
|
|
exit()
|
|
accid = account['id']
|
|
print('Searching for posts including "' + pattern + '" from user ' + account['fqn'])
|
|
print('\n---\n')
|
|
while True:
|
|
while True:
|
|
try:
|
|
statuses = mastodon.account_statuses(accid, max_id=oldest_status_id, limit=1000)
|
|
break
|
|
except IndexError:
|
|
break
|
|
except:
|
|
pass
|
|
if len(statuses) == 0:
|
|
break
|
|
oldest_status_id = statuses[-1]['id']
|
|
for status in statuses:
|
|
if status['reblog'] == None:
|
|
if status['visibility'] == 'direct' and dms == False:
|
|
continue
|
|
content = str(htmlconvert.handle(status['content']))
|
|
if case == False:
|
|
content = content.lower()
|
|
if pattern in content:
|
|
print(content)
|
|
print('\nlink: ' + status['url'])
|
|
print('\n---\n')
|
|
print('Finished searching') |