Finish to split classes

This commit is contained in:
poka 2020-12-07 16:42:22 +01:00
parent a426771761
commit cc68918d59
5 changed files with 85 additions and 98 deletions

View File

@ -1,14 +1,13 @@
#!/usr/bin/env python3 #!/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 os.path import join, dirname
from shutil import copyfile from shutil import copyfile
from dotenv import load_dotenv from dotenv import load_dotenv
from duniterpy.key import SigningKey from duniterpy.key import SigningKey
from lib.cesium import CesiumPlus 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])) + '/' MY_PATH = os.path.realpath(os.path.dirname(sys.argv[0])) + '/'
@ -73,7 +72,7 @@ if not cmd:
sys.exit(1) sys.exit(1)
if args.version: if args.version:
print(VERSION) print(__version__)
sys.exit(0) sys.exit(0)
def createTmpDunikey(): def createTmpDunikey():
@ -142,26 +141,21 @@ elif cmd == "delete":
cesium.delete(args.id[0], args.outbox) cesium.delete(args.id[0], args.outbox)
# Profiles # Profiles
elif cmd in ('set','get','erase'): elif cmd == "set":
cesium = Profiles(dunikey, pod) cesium.set(args.name, args.description, args.ville, args.adresse, args.position, args.site, args.avatar)
if cmd == "set": elif cmd == "get":
cesium.set(args.name, args.description, args.ville, args.adresse, args.position, args.site, args.avatar) cesium.get(args.profile, args.avatar)
elif cmd == "get": elif cmd == "erase":
cesium.get(args.profile, args.avatar) cesium.erase()
elif cmd == "erase":
cesium.erase()
# Likes # Likes
elif cmd == "like": elif cmd == "like":
if args.stars or args.stars == 0: if args.stars or args.stars == 0:
gchange = SendLikes(dunikey, pod) cesium.like(args.stars, args.profile)
gchange.like(args.stars, args.profile)
else: else:
gchange = ReadLikes(dunikey, pod) cesium.readLikes(args.profile)
gchange.readLikes(args.profile)
elif cmd == "unlike": elif cmd == "unlike":
gchange = UnLikes(dunikey, pod) cesium.unLike(args.profile)
gchange.unLike(args.profile)
if keyPath: if keyPath:

View File

@ -1,8 +1,13 @@
import string, random, base64 import re, string, random, base64
from lib.cesiumCommon import CesiumCommon from lib.cesiumCommon import CesiumCommon, PUBKEY_REGEX
from lib.messaging import ReadFromCesium, SendToCesium, DeleteFromCesium from lib.messaging import ReadFromCesium, SendToCesium, DeleteFromCesium
from lib.profiles import Profiles
from lib.likes import ReadLikes, SendLikes, UnLikes
class CesiumPlus(CesiumCommon): class CesiumPlus(CesiumCommon):
#################### Messaging ####################
def read(self, nbrMsg, outbox, isJSON): def read(self, nbrMsg, outbox, isJSON):
readCesium = ReadFromCesium(self.dunikey, self.pod) readCesium = ReadFromCesium(self.dunikey, self.pod)
jsonMsg = readCesium.sendDocument(nbrMsg, outbox) jsonMsg = readCesium.sendDocument(nbrMsg, outbox)
@ -31,3 +36,58 @@ class CesiumPlus(CesiumCommon):
for idMsg in idsMsgList: for idMsg in idsMsgList:
finalDoc = deleteCesium.configDoc(idMsg, outbox) finalDoc = deleteCesium.configDoc(idMsg, outbox)
deleteCesium.sendDocument(finalDoc, idMsg) 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)

View File

@ -1,5 +1,3 @@
#!/usr/bin/env python3
import os, sys, ast, requests, json, base58, base64, time, string, random, re 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 lib.natools import fmt, sign, get_privkey, box_decrypt, box_encrypt
from time import sleep from time import sleep
@ -124,26 +122,14 @@ class ReadLikes:
data = json.dumps(data) data = json.dumps(data)
result = requests.post('{0}/user/profile/_search'.format(self.pod), headers=headers, data=data) result = requests.post('{0}/user/profile/_search'.format(self.pod), headers=headers, data=data)
result = json.loads(result.text)['hits']['hits'][0]['_source'] result = json.loads(result.text)['hits']['hits']
for i in result:
return result return i['_source']
def readLikes(self, profile=False):
document = self.configDoc(profile)
result = self.sendDocument(document)
result = self.parseResult(result)
print(result)
return result
#################### Like class #################### #################### Like class ####################
class SendLikes: class SendLikes:
def __init__(self, dunikey, pod): def __init__(self, dunikey, pod):
# Get my pubkey from my private key # Get my pubkey from my private key
@ -216,7 +202,10 @@ class SendLikes:
resultJson = json.loads(result.text) resultJson = json.loads(result.text)
if 'DuplicatedDocumentException' in resultJson['error']: if 'DuplicatedDocumentException' in resultJson['error']:
rmLike = UnLikes(self.dunikey, self.pod) 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) sleep(0.5)
self.sendDocument(document, pubkey) self.sendDocument(document, pubkey)
return resultJson['error'] 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') 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 #################### #################### Unlike class ####################
class UnLikes: class UnLikes:
def __init__(self, dunikey, pod): def __init__(self, dunikey, pod):
# Get my pubkey from my private key # Get my pubkey from my private key
@ -321,11 +298,3 @@ class UnLikes:
return result.text return result.text
else: else:
sys.stderr.write("Echec de l'envoi du document de lecture des messages...\n" + result.text + '\n') 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)

View File

@ -5,6 +5,10 @@ from termcolor import colored
from lib.natools import fmt, get_privkey, box_decrypt, box_encrypt from lib.natools import fmt, get_privkey, box_decrypt, box_encrypt
from lib.cesiumCommon import CesiumCommon, pp_json, PUBKEY_REGEX from lib.cesiumCommon import CesiumCommon, pp_json, PUBKEY_REGEX
#################### Reading class ####################
class ReadFromCesium(CesiumCommon): class ReadFromCesium(CesiumCommon):
# Configure JSON document to send # Configure JSON document to send
def configDoc(self, nbrMsg, outbox): def configDoc(self, nbrMsg, outbox):
@ -137,14 +141,9 @@ class ReadFromCesium(CesiumCommon):
return data return data
#################### Sending class #################### #################### Sending class ####################
class SendToCesium(CesiumCommon): class SendToCesium(CesiumCommon):
def encryptMsg(self, msg): def encryptMsg(self, msg):
return fmt["64"](box_encrypt(msg.encode(), get_privkey(self.dunikey, "pubsec"), self.recipient, self.nonce)).decode() 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') print(str(pp_json(result.text)) + '\n')
#################### Deleting class #################### #################### Deleting class ####################
class DeleteFromCesium(CesiumCommon): class DeleteFromCesium(CesiumCommon):
def configDoc(self, idMsg, outbox): def configDoc(self, idMsg, outbox):
# Get current timestamp # Get current timestamp

View File

@ -123,33 +123,3 @@ class Profiles(CesiumCommon):
return json.dumps(final, indent=2) return json.dumps(final, indent=2)
else: else:
return 'Profile vide' 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