Also remove moved accounts from followers

main
Bob Mottram 2021-01-09 20:27:17 +00:00
parent e7d0668cd6
commit 6d0678db54
1 changed files with 58 additions and 39 deletions

View File

@ -19,12 +19,12 @@ from follow import unfollowAccount
def _moveFollowingHandlesForAccount(baseDir: str, nickname: str, domain: str, def _moveFollowingHandlesForAccount(baseDir: str, nickname: str, domain: str,
session, session,
httpPrefix: str, cachedWebfingers: {}, httpPrefix: str, cachedWebfingers: {},
followFile: str, debug: bool) -> int: debug: bool) -> int:
"""Goes through all follows for an account and updates any that have moved """Goes through all follows for an account and updates any that have moved
""" """
ctr = 0 ctr = 0
followingFilename = \ followingFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/' + followFile baseDir + '/accounts/' + nickname + '@' + domain + '/following.txt'
if not os.path.isfile(followingFilename): if not os.path.isfile(followingFilename):
return ctr return ctr
with open(followingFilename, "r") as f: with open(followingFilename, "r") as f:
@ -35,14 +35,14 @@ def _moveFollowingHandlesForAccount(baseDir: str, nickname: str, domain: str,
_updateMovedHandle(baseDir, nickname, domain, _updateMovedHandle(baseDir, nickname, domain,
followHandle, session, followHandle, session,
httpPrefix, cachedWebfingers, httpPrefix, cachedWebfingers,
followFile, debug) debug)
return ctr return ctr
def _updateMovedHandle(baseDir: str, nickname: str, domain: str, def _updateMovedHandle(baseDir: str, nickname: str, domain: str,
handle: str, session, handle: str, session,
httpPrefix: str, cachedWebfingers: {}, httpPrefix: str, cachedWebfingers: {},
followFile: str, debug: bool) -> int: debug: bool) -> int:
"""Check if an account has moved, and if so then alter following.txt """Check if an account has moved, and if so then alter following.txt
for each account. for each account.
Returns 1 if moved, 0 otherwise Returns 1 if moved, 0 otherwise
@ -112,45 +112,65 @@ def _updateMovedHandle(baseDir: str, nickname: str, domain: str,
# so just unfollow them # so just unfollow them
unfollowAccount(baseDir, nickname, domain, unfollowAccount(baseDir, nickname, domain,
movedToNickname, movedToDomainFull, movedToNickname, movedToDomainFull,
followFile, debug) 'following.txt', debug)
return ctr return ctr
followingFilename = \ followingFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/' + followFile baseDir + '/accounts/' + nickname + '@' + domain + '/following.txt'
if not os.path.isfile(followingFilename): if os.path.isfile(followingFilename):
return ctr with open(followingFilename, "r") as f:
with open(followingFilename, "r") as f: followingHandles = f.readlines()
followingHandles = f.readlines()
movedToHandle = movedToNickname + '@' + movedToDomainFull movedToHandle = movedToNickname + '@' + movedToDomainFull
handleLower = handle.lower() handleLower = handle.lower()
refollowFilename = \ refollowFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/refollow.txt' baseDir + '/accounts/' + \
nickname + '@' + domain + '/refollow.txt'
# unfollow the old handle # unfollow the old handle
with open(followingFilename, 'w+') as f: with open(followingFilename, 'w+') as f:
for followHandle in followingHandles: for followHandle in followingHandles:
if followHandle.strip("\n").strip("\r").lower() != \ if followHandle.strip("\n").strip("\r").lower() != \
handleLower: handleLower:
f.write(followHandle) f.write(followHandle)
else:
handleNickname = handle.split('@')[0]
handleDomain = handle.split('@')[1]
unfollowAccount(baseDir, nickname, domain,
handleNickname,
handleDomain,
followFile, debug)
ctr += 1
print('Unfollowed ' + handle + ' who has moved to ' +
movedToHandle)
# save the new handles to the refollow list
if os.path.isfile(refollowFilename):
with open(refollowFilename, 'a+') as f:
f.write(movedToHandle + '\n')
else: else:
with open(refollowFilename, 'w+') as f: handleNickname = handle.split('@')[0]
f.write(movedToHandle + '\n') handleDomain = handle.split('@')[1]
unfollowAccount(baseDir, nickname, domain,
handleNickname,
handleDomain,
'following.txt', debug)
ctr += 1
print('Unfollowed ' + handle + ' who has moved to ' +
movedToHandle)
# save the new handles to the refollow list
if os.path.isfile(refollowFilename):
with open(refollowFilename, 'a+') as f:
f.write(movedToHandle + '\n')
else:
with open(refollowFilename, 'w+') as f:
f.write(movedToHandle + '\n')
followersFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/followers.txt'
if os.path.isfile(followersFilename):
with open(followersFilename, "r") as f:
followerHandles = f.readlines()
handleLower = handle.lower()
# remove followers who have moved
with open(followersFilename, 'w+') as f:
for followerHandle in followerHandles:
if followerHandle.strip("\n").strip("\r").lower() != \
handleLower:
f.write(followerHandle)
else:
ctr += 1
print('Removed follower who has moved ' + handle)
return ctr return ctr
@ -176,7 +196,6 @@ def migrateAccounts(baseDir: str, session,
ctr += \ ctr += \
_moveFollowingHandlesForAccount(baseDir, nickname, domain, _moveFollowingHandlesForAccount(baseDir, nickname, domain,
session, httpPrefix, session, httpPrefix,
cachedWebfingers, cachedWebfingers, debug)
'following.txt', debug)
break break
return ctr return ctr