diff --git a/jaklis.py b/jaklis.py index aa8def8..eab4a8b 100755 --- a/jaklis.py +++ b/jaklis.py @@ -1,14 +1,13 @@ #!/usr/bin/env python3 -import argparse, sys, os, random, string, getpass, json +import argparse, sys, os, getpass, string, random from os.path import join, dirname from shutil import copyfile from dotenv import load_dotenv from duniterpy.key import SigningKey from lib.cesium import CesiumPlus -from lib.likes import ReadLikes, SendLikes, UnLikes -VERSION = "0.0.1" +__version__ = "0.0.2" MY_PATH = os.path.realpath(os.path.dirname(sys.argv[0])) + '/' @@ -73,7 +72,7 @@ if not cmd: sys.exit(1) if args.version: - print(VERSION) + print(__version__) sys.exit(0) def createTmpDunikey(): @@ -142,26 +141,21 @@ elif cmd == "delete": cesium.delete(args.id[0], args.outbox) # Profiles -elif cmd in ('set','get','erase'): - cesium = Profiles(dunikey, pod) - if cmd == "set": - cesium.set(args.name, args.description, args.ville, args.adresse, args.position, args.site, args.avatar) - elif cmd == "get": - cesium.get(args.profile, args.avatar) - elif cmd == "erase": - cesium.erase() +elif cmd == "set": + cesium.set(args.name, args.description, args.ville, args.adresse, args.position, args.site, args.avatar) +elif cmd == "get": + cesium.get(args.profile, args.avatar) +elif cmd == "erase": + cesium.erase() # Likes elif cmd == "like": if args.stars or args.stars == 0: - gchange = SendLikes(dunikey, pod) - gchange.like(args.stars, args.profile) + cesium.like(args.stars, args.profile) else: - gchange = ReadLikes(dunikey, pod) - gchange.readLikes(args.profile) + cesium.readLikes(args.profile) elif cmd == "unlike": - gchange = UnLikes(dunikey, pod) - gchange.unLike(args.profile) + cesium.unLike(args.profile) if keyPath: diff --git a/lib/cesium.py b/lib/cesium.py index 56b778e..642ac1d 100644 --- a/lib/cesium.py +++ b/lib/cesium.py @@ -1,8 +1,13 @@ -import string, random, base64 -from lib.cesiumCommon import CesiumCommon +import re, string, random, base64 +from lib.cesiumCommon import CesiumCommon, PUBKEY_REGEX from lib.messaging import ReadFromCesium, SendToCesium, DeleteFromCesium +from lib.profiles import Profiles +from lib.likes import ReadLikes, SendLikes, UnLikes class CesiumPlus(CesiumCommon): + + #################### Messaging #################### + def read(self, nbrMsg, outbox, isJSON): readCesium = ReadFromCesium(self.dunikey, self.pod) jsonMsg = readCesium.sendDocument(nbrMsg, outbox) @@ -31,3 +36,58 @@ class CesiumPlus(CesiumCommon): for idMsg in idsMsgList: finalDoc = deleteCesium.configDoc(idMsg, outbox) deleteCesium.sendDocument(finalDoc, idMsg) + + #################### Profiles #################### + + def set(self, name=None, description=None, ville=None, adresse=None, position=None, site=None, avatar=None): + setProfile = Profiles(self.dunikey, self.pod) + document = setProfile.configDocSet(name, description, ville, adresse, position, site, avatar) + result = setProfile.sendDocument(document,'set') + + print(result) + return result + + def get(self, profile=None, avatar=None): + getProfile = Profiles(self.dunikey, self.pod) + if not profile: + profile = self.pubkey + if not re.match(PUBKEY_REGEX, profile) or len(profile) > 45: + scope = 'title' + else: + scope = '_id' + + document = getProfile.configDocGet(profile, scope, avatar) + resultJSON = getProfile.sendDocument(document, 'get') + result = getProfile.parseJSON(resultJSON) + + print(result) + + def erase(self): + eraseProfile = Profiles(self.dunikey, self.pod) + document = eraseProfile.configDocErase() + result = eraseProfile.sendDocument(document,'erase') + + print(result) + + #################### Likes #################### + + def readLikes(self, profile=False): + likes = ReadLikes(self.dunikey, self.pod) + document = likes.configDoc(profile) + result = likes.sendDocument(document) + result = likes.parseResult(result) + + print(result) + + def like(self, stars, profile=False): + likes = SendLikes(self.dunikey, self.pod) + document = likes.configDoc(profile, stars) + if document: + likes.sendDocument(document, profile) + + def unLike(self, pubkey, silent=False): + likes = UnLikes(self.dunikey, self.pod) + idLike = likes.checkLike(pubkey) + if idLike: + document = likes.configDoc(idLike) + likes.sendDocument(document, silent) diff --git a/lib/likes.py b/lib/likes.py index ce4b1f6..3217cb9 100644 --- a/lib/likes.py +++ b/lib/likes.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 - import os, sys, ast, requests, json, base58, base64, time, string, random, re from lib.natools import fmt, sign, get_privkey, box_decrypt, box_encrypt from time import sleep @@ -124,26 +122,14 @@ class ReadLikes: data = json.dumps(data) result = requests.post('{0}/user/profile/_search'.format(self.pod), headers=headers, data=data) - result = json.loads(result.text)['hits']['hits'][0]['_source'] - - return result - - def readLikes(self, profile=False): - document = self.configDoc(profile) - result = self.sendDocument(document) - result = self.parseResult(result) - - print(result) - return result - - + result = json.loads(result.text)['hits']['hits'] + for i in result: + return i['_source'] #################### Like class #################### - - class SendLikes: def __init__(self, dunikey, pod): # Get my pubkey from my private key @@ -216,7 +202,10 @@ class SendLikes: resultJson = json.loads(result.text) if 'DuplicatedDocumentException' in resultJson['error']: rmLike = UnLikes(self.dunikey, self.pod) - rmLike.unLike(pubkey, True) + idLike = rmLike.checkLike(pubkey) + if idLike: + document = rmLike.configDoc(idLike) + rmLike.sendDocument(document, True) sleep(0.5) self.sendDocument(document, pubkey) return resultJson['error'] @@ -227,21 +216,9 @@ class SendLikes: sys.stderr.write("Echec de l'envoi du document de lecture des messages...\n" + resultJson['error'] + '\n') - - - def like(self, stars, profile=False): - document = self.configDoc(profile, stars) - if document: - self.sendDocument(document, profile) - - - - #################### Unlike class #################### - - class UnLikes: def __init__(self, dunikey, pod): # Get my pubkey from my private key @@ -321,11 +298,3 @@ class UnLikes: return result.text else: sys.stderr.write("Echec de l'envoi du document de lecture des messages...\n" + result.text + '\n') - - - def unLike(self, pubkey, silent=False): - idLike = self.checkLike(pubkey) - if idLike: - document = self.configDoc(idLike) - self.sendDocument(document, silent) - diff --git a/lib/messaging.py b/lib/messaging.py index 15bcafe..2165182 100644 --- a/lib/messaging.py +++ b/lib/messaging.py @@ -5,6 +5,10 @@ from termcolor import colored from lib.natools import fmt, get_privkey, box_decrypt, box_encrypt from lib.cesiumCommon import CesiumCommon, pp_json, PUBKEY_REGEX + +#################### Reading class #################### + + class ReadFromCesium(CesiumCommon): # Configure JSON document to send def configDoc(self, nbrMsg, outbox): @@ -137,14 +141,9 @@ class ReadFromCesium(CesiumCommon): return data - - - #################### Sending class #################### - - class SendToCesium(CesiumCommon): def encryptMsg(self, msg): return fmt["64"](box_encrypt(msg.encode(), get_privkey(self.dunikey, "pubsec"), self.recipient, self.nonce)).decode() @@ -192,14 +191,9 @@ class SendToCesium(CesiumCommon): print(str(pp_json(result.text)) + '\n') - - - #################### Deleting class #################### - - class DeleteFromCesium(CesiumCommon): def configDoc(self, idMsg, outbox): # Get current timestamp diff --git a/lib/profiles.py b/lib/profiles.py index e2b2531..5386c89 100644 --- a/lib/profiles.py +++ b/lib/profiles.py @@ -123,33 +123,3 @@ class Profiles(CesiumCommon): return json.dumps(final, indent=2) else: return 'Profile vide' - - - def set(self, name=None, description=None, ville=None, adresse=None, position=None, site=None, avatar=None): - document = self.configDocSet(name, description, ville, adresse, position, site, avatar) - result = self.sendDocument(document,'set') - - print(result) - return result - - def get(self, profile=None, avatar=None): - if not profile: - profile = self.pubkey - if not re.match(PUBKEY_REGEX, profile) or len(profile) > 45: - scope = 'title' - else: - scope = '_id' - - document = self.configDocGet(profile, scope, avatar) - resultJSON = self.sendDocument(document, 'get') - result = self.parseJSON(resultJSON) - - print(result) - return result - - def erase(self): - document = self.configDocErase() - result = self.sendDocument(document,'erase') - - print(result) - return result