From 8a1fd82584d455ecc8312e10373945bf83f56089 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 8 Jul 2019 09:51:33 +0100 Subject: [PATCH] Function for grouping followers by domain --- posts.py | 19 +++++++++++++++++++ tests.py | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/posts.py b/posts.py index 190b72f4..e19c87e8 100644 --- a/posts.py +++ b/posts.py @@ -596,6 +596,25 @@ def sendPost(session,baseDir: str,nickname: str, domain: str, port: int, \ thr.start() return 0 +def groupFollowersByDomain(baseDir :str,nickname :str,domain :str) -> {}: + """Returns a dictionary with followers grouped by domain + """ + handle=nickname+'@'+domain + followersFilename=baseDir+'/accounts/'+handle+'/followers.txt' + if not os.path.isfile(followersFilename): + return None + grouped={} + with open(followersFilename, "r") as f: + for followerHandle in f: + if '@' in followerHandle: + fHandle=followerHandle.strip().replace('\n','') + followerDomain=fHandle.split('@')[1] + if not grouped.get(followerDomain): + grouped[followerDomain]=[fHandle] + else: + grouped[followerDomain].append(fHandle) + return grouped + def sendSignedJson(postJsonObject: {},session,baseDir: str, \ nickname: str, domain: str, port: int, \ toNickname: str, toDomain: str, toPort: int, cc: str, \ diff --git a/tests.py b/tests.py index 545b4153..dad93c83 100644 --- a/tests.py +++ b/tests.py @@ -24,6 +24,7 @@ from posts import createPublicPost from posts import sendPost from posts import archivePosts from posts import noOfFollowersOnDomain +from posts import groupFollowersByDomain from follow import clearFollows from follow import clearFollowers from utils import followPerson @@ -510,6 +511,44 @@ def testNoOfFollowersOnDomain(): os.chdir(currDir) shutil.rmtree(baseDir) +def testGroupFollowers(): + print('testGroupFollowers') + + currDir=os.getcwd() + nickname='test735' + domain='mydomain.com' + password='somepass' + port=80 + httpPrefix='https' + federationList=[] + baseDir=currDir+'/.tests_testgroupfollowers' + if os.path.isdir(baseDir): + shutil.rmtree(baseDir) + os.mkdir(baseDir) + os.chdir(baseDir) + createPerson(baseDir,nickname,domain,port,httpPrefix,True,password) + + clearFollowers(baseDir,nickname,domain) + followerOfPerson(baseDir,nickname,domain,'badger','wild.domain',federationList,False) + followerOfPerson(baseDir,nickname,domain,'squirrel','wild.domain',federationList,False) + followerOfPerson(baseDir,nickname,domain,'rodent','wild.domain',federationList,False) + followerOfPerson(baseDir,nickname,domain,'utterly','clutterly.domain',federationList,False) + followerOfPerson(baseDir,nickname,domain,'zonked','zzz.domain',federationList,False) + followerOfPerson(baseDir,nickname,domain,'nap','zzz.domain',federationList,False) + + grouped=groupFollowersByDomain(baseDir,nickname,domain) + assert len(grouped.items())==3 + assert grouped.get('zzz.domain') + assert grouped.get('clutterly.domain') + assert grouped.get('wild.domain') + assert len(grouped['zzz.domain'])==2 + assert len(grouped['wild.domain'])==3 + assert len(grouped['clutterly.domain'])==1 + + os.chdir(currDir) + shutil.rmtree(baseDir) + + def testFollows(): print('testFollows') currDir=os.getcwd() @@ -641,5 +680,6 @@ def runAllTests(): testAuthentication() testFollowersOfPerson() testNoOfFollowersOnDomain() - testFollows() + testFollows() + testGroupFollowers() print('Tests succeeded\n')