Functions for counting followers on a domain

master
Bob Mottram 2019-07-05 15:25:15 +01:00
parent 8b285809c5
commit 410010ec2b
2 changed files with 57 additions and 0 deletions

View File

@ -36,6 +36,22 @@ def getFollowersOfPerson(baseDir: str,nickname: str,domain: str,followFile='foll
break
return followers
def noOfFollowersOnDomain(baseDir: str,handle: str, domain: str, followFile='followers.txt') -> int:
"""Returns the number of followers of the given handle from the given domain
"""
filename=baseDir+'/accounts/'+handle+'/'+followFile
if not os.path.isfile(filename):
return 0
ctr=0
with open(filename, "r") as followersFilename:
for followerHandle in followersFilename:
if '@' in followerHandle:
followerDomain=followerHandle.split('@')[1].replace('\n','')
if domain==followerDomain:
ctr+=1
return ctr
def followPerson(baseDir: str,nickname: str, domain: str, \
followNickname: str, followDomain: str, \
federationList: [], followFile='following.txt') -> bool:

View File

@ -30,6 +30,7 @@ from follow import followerOfPerson
from follow import unfollowPerson
from follow import unfollowerOfPerson
from follow import getFollowersOfPerson
from follow import noOfFollowersOnDomain
from person import createPerson
from person import setPreferredNickname
from person import setBio
@ -271,7 +272,46 @@ def testFollowersOfPerson():
assert 'sausagedog@'+domain in followList
os.chdir(currDir)
shutil.rmtree(baseDir)
def testNoOfFollowersOnDomain():
print('testNoOfFollowersOnDomain')
currDir=os.getcwd()
nickname='mxpop'
domain='diva.domain'
otherdomain='soup.dragon'
password='birb'
port=80
httpPrefix='https'
federationList=[]
baseDir=currDir+'/.tests_nooffollowersOndomain'
if os.path.isdir(baseDir):
shutil.rmtree(baseDir)
os.mkdir(baseDir)
os.chdir(baseDir)
createPerson(baseDir,nickname,domain,port,httpPrefix,True,password)
createPerson(baseDir,'maxboardroom',otherdomain,port,httpPrefix,True,password)
createPerson(baseDir,'ultrapancake',otherdomain,port,httpPrefix,True,password)
createPerson(baseDir,'drokk',otherdomain,port,httpPrefix,True,password)
createPerson(baseDir,'sausagedog',otherdomain,port,httpPrefix,True,password)
followPerson(baseDir,'drokk',otherdomain,nickname,domain,federationList)
followPerson(baseDir,'sausagedog',otherdomain,nickname,domain,federationList)
followPerson(baseDir,'maxboardroom',otherdomain,nickname,domain,federationList)
followerOfPerson(baseDir,nickname,domain,'drokk',otherdomain,federationList)
followerOfPerson(baseDir,nickname,domain,'sausagedog',otherdomain,federationList)
followerOfPerson(baseDir,nickname,domain,'maxboardroom',otherdomain,federationList)
followersOnOtherDomain=noOfFollowersOnDomain(baseDir,nickname+'@'+domain, otherdomain)
assert followersOnOtherDomain==3
unfollowerOfPerson(baseDir,nickname,domain,'sausagedog',otherdomain)
followersOnOtherDomain=noOfFollowersOnDomain(baseDir,nickname+'@'+domain, otherdomain)
assert followersOnOtherDomain==2
os.chdir(currDir)
shutil.rmtree(baseDir)
def testFollows():
print('testFollows')
currDir=os.getcwd()
@ -402,5 +442,6 @@ def runAllTests():
testCreatePerson()
testAuthentication()
testFollowersOfPerson()
testNoOfFollowersOnDomain()
testFollows()
print('Tests succeeded\n')