wardyns-feditools/import_following.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-07-30 19:18:11 -07:00
#!/usr/bin/env python
2023-01-26 19:40:26 -08:00
import requests
import json
import os
from argparse import ArgumentParser, FileType
from generate_config import generate_config
parent = os.path.dirname(os.path.realpath(__file__))
# Initialize arguments
parser = ArgumentParser(description='Import follows from a csv file')
parser.add_argument('file',
type=open,
help='csv file containing users to follow',
)
args = parser.parse_args()
csv = args.file.read()
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']})
acclist = csv.split('\n')
for account in acclist:
searchlist = session.get(config['instance'] + '/api/v2/search?', params={'q':account, 'type':'accounts'}).json()
searchlist = searchlist['accounts']
found = False
for user in searchlist:
if user['fqn'] == account:
account = user
found = True
break
if found == False:
print("Could not find user: " + account)
continue
relationship = account['pleroma']['relationship']
if relationship['following'] == True:
print('Already following: ' + account['fqn'])
continue
print("Following: " + account['fqn'])
session.post(config['instance'] + '/api/v1/accounts/' + account['id'] + '/follow')