From 410010ec2b0e39fc03cfe3738f662068dba40e18 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 5 Jul 2019 15:25:15 +0100 Subject: [PATCH] Functions for counting followers on a domain --- follow.py | 16 ++++++++++++++++ tests.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/follow.py b/follow.py index 7b2488b8..3e809ea7 100644 --- a/follow.py +++ b/follow.py @@ -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: diff --git a/tests.py b/tests.py index 50838933..0b27f40a 100644 --- a/tests.py +++ b/tests.py @@ -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')