like option

master
Bob Mottram 2019-07-17 19:33:41 +01:00
parent 58c213256c
commit edf4951853
2 changed files with 93 additions and 0 deletions

View File

@ -169,6 +169,8 @@ parser.add_argument("--allowdeletion", type=str2bool, nargs='?', \
help="Do not allow deletions") help="Do not allow deletions")
parser.add_argument('--repeat','--announce', dest='announce', type=str,default=None, \ parser.add_argument('--repeat','--announce', dest='announce', type=str,default=None, \
help='Announce/repeat a url') help='Announce/repeat a url')
parser.add_argument('--favorite','--like', dest='like', type=str,default=None, \
help='Like a url')
parser.add_argument('--sendto', nargs='+',dest='sendto', \ parser.add_argument('--sendto', nargs='+',dest='sendto', \
help='List of post recipients') help='List of post recipients')
parser.add_argument('--attach', dest='attach', type=str,default=None, \ parser.add_argument('--attach', dest='attach', type=str,default=None, \
@ -385,6 +387,30 @@ if args.announce:
time.sleep(1) time.sleep(1)
sys.exit() sys.exit()
if args.like:
if not nickname:
print('Specify a nickname with the --nickname option')
sys.exit()
if not args.password:
print('Specify a password with the --password option')
sys.exit()
session = createSession(domain,port,useTor)
personCache={}
cachedWebfingers={}
print('Sending like of '+args.like)
sendLikeViaServer(session,nickname,args.password,
domain,port, \
httpPrefix,args.like, \
cachedWebfingers,personCache, \
True)
for i in range(10):
# TODO detect send success/fail
time.sleep(1)
sys.exit()
if args.delete: if args.delete:
if not nickname: if not nickname:
print('Specify a nickname with the --nickname option') print('Specify a nickname with the --nickname option')

67
like.py
View File

@ -268,3 +268,70 @@ def undoLikePost(session,baseDir: str,federationList: [], \
return undoLike(session,baseDir,federationList,nickname,domain,port, \ return undoLike(session,baseDir,federationList,nickname,domain,port, \
ccList,httpPrefix,objectUrl,clientToServer, \ ccList,httpPrefix,objectUrl,clientToServer, \
sendThreads,postLog,personCache,cachedWebfingers,debug) sendThreads,postLog,personCache,cachedWebfingers,debug)
def sendLikeViaServer(session,fromNickname: str,password: str,
fromDomain: str,fromPort: int, \
httpPrefix: str,likeUrl: str, \
cachedWebfingers: {},personCache: {}, \
debug: bool) -> {}:
"""Creates a like via c2s
"""
if not session:
print('WARN: No session for sendLikeViaServer')
return 6
fromDomainFull=fromDomain
if fromPort!=80 and fromPort!=443:
fromDomainFull=fromDomain+':'+str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl = httpPrefix + '://'+fromDomainFull+'/users/'+fromNickname+'/followers'
newLikeJson = {
'type': 'Like',
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
'object': likeUrl,
'to': [toUrl],
'cc': [ccUrl]
}
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname
# lookup the inbox for the To handle
wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers)
if not wfRequest:
if debug:
print('DEBUG: announce webfinger failed for '+handle)
return 1
postToBox='outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \
getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl:
if debug:
print('DEBUG: No '+postToBox+' was found for '+handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: No actor was found for '+handle)
return 4
authHeader=createBasicAuthHeader(fromNickname,password)
headers = {'host': fromDomain, \
'Content-type': 'application/json', \
'Authorization': authHeader}
postResult = \
postJson(session,newLikeJson,[],inboxUrl,headers,"inbox:write")
#if not postResult:
# if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
# return 5
if debug:
print('DEBUG: c2s POST like success')
return newLikeJson