epicyon/migrate.py

55 lines
1.8 KiB
Python
Raw Normal View History

2020-04-03 17:01:25 +00:00
__filename__ = "migrate.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-10-31 10:36:35 +00:00
import os
2020-04-03 17:01:25 +00:00
def _migrateFollows(followFilename: str, oldHandle: str,
newHandle: str) -> None:
2019-10-31 10:36:35 +00:00
"""Changes a handle within following or followers list
"""
if not os.path.isfile(followFilename):
return
if oldHandle not in open(followFilename).read():
return
2020-04-03 17:01:25 +00:00
followData = None
2019-10-31 10:36:35 +00:00
with open(followFilename, 'r') as followFile:
2020-04-03 17:01:25 +00:00
followData = followFile.read()
2019-10-31 10:36:35 +00:00
if not followData:
return
2020-04-03 17:01:25 +00:00
newFollowData = followData.replace(oldHandle, newHandle)
if followData == newFollowData:
2019-10-31 10:36:35 +00:00
return
2020-07-12 20:04:58 +00:00
with open(followFilename, 'w+') as followFile:
2019-10-31 10:36:35 +00:00
followFile.write(newFollowData)
2020-04-03 17:01:25 +00:00
def migrateAccount(baseDir: str, oldHandle: str, newHandle: str) -> None:
2019-10-31 10:36:35 +00:00
"""If a followed account changes then this modifies the
following and followers lists for each account accordingly
"""
if oldHandle.startswith('@'):
2020-04-03 17:01:25 +00:00
oldHandle = oldHandle[1:]
2019-10-31 10:36:35 +00:00
if '@' not in oldHandle:
return
if newHandle.startswith('@'):
2020-04-03 17:01:25 +00:00
newHandle = newHandle[1:]
2019-10-31 10:36:35 +00:00
if '@' not in newHandle:
return
# update followers and following lists for each account
2020-04-03 17:01:25 +00:00
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
2019-10-31 10:36:35 +00:00
for handle in dirs:
if '@' in handle:
2020-04-03 17:01:25 +00:00
accountDir = baseDir + '/accounts/' + handle
followFilename = accountDir + '/following.txt'
_migrateFollows(followFilename, oldHandle, newHandle)
2020-04-03 17:01:25 +00:00
followFilename = accountDir + '/followers.txt'
_migrateFollows(followFilename, oldHandle, newHandle)
2020-12-13 22:13:45 +00:00
break