custodisco-kiosk/addtoDB-backup.py
2024-08-07 14:28:48 -04:00

115 lines
3.5 KiB
Python

#!/usr/bin/python
# -*- coding:utf-8 -*-
#this script takes a file as an option and adds that file scuttlebutt
# originally from ebb, modified for custodisco
import optparse
import traceback
import os, sys
import json
import subprocess
import logging
def main():
#get options and arguments
p = optparse.OptionParser()
p.add_option('--file', '-f', action='store', dest='file', help='this needs to be a file path')
options, arguments = p.parse_args()
if options.file:
pathToImage=options.file
else:
print("you need to provide a file path")
exit(1)
def addToSSB(pathToImage,description,mintOrGive):
# mint
if mintOrGive == 1:
try:
result = subprocess.Popen('./ssb-post.sh mint "' + description + '" ' + pathToImage, shell=True, stdout=subprocess.PIPE, )
except:
print('traceback.format_exc():\n%s' % traceback.format_exc())
exit()
# else give
elif mintOrGive == 2:
try:
result = subprocess.Popen('./ssb-post.sh give "' + pathToImage + '" ' + description, shell=True, stdout=subprocess.PIPE, )
except:
print('traceback.format_exc():\n%s' % traceback.format_exc())
exit()
# get the ssb json from the bash command we just ran
newssb = result.stdout.read().decode() # decode bytes to string
print(newssb)
# CHECK that newssb is _anything_ if ssb-server isn't running that may show as garbage that will crash the program
if len(newssb) == 0:
print("String is empty")
#convert string to object
# Make sure it's valid JSON before loading
try:
json_object = json.loads(newssb)
except json.JSONDecodeError:
print("Invalid JSON")
return "offline" # Return the blouch
# get the key for the post we just made
key = json_object["key"]
print (key)
return key
def get_message_content(message_id):
try:
print(f"Executing command: ./ssb-post.sh get_message_content {message_id}")
result = subprocess.run(['./ssb-post.sh', 'get_message_content', message_id],
capture_output=True, text=True, check=True)
print(f"Command output:\n{result.stdout}")
# Split the output into lines
lines = result.stdout.split('\n')
# Extract the image path
image_path = None
for line in lines:
if line.startswith("IMAGE_PATH:"):
image_path = line.split(":", 1)[1].strip()
break
# Find the start of the JSON content
json_start = next(i for i, line in enumerate(lines) if line.strip().startswith("{"))
# Join the JSON lines and parse
json_content = "\n".join(lines[json_start:])
message_content = json.loads(json_content)
print(f"Parsed message content: {message_content}")
print(f"Image path: {image_path}")
return message_content, image_path
except subprocess.CalledProcessError as e:
print(f"Error: subprocess.CalledProcessError - {e}")
print(f"Debug: stdout = {e.stdout}")
print(f"Debug: stderr = {e.stderr}")
return None, None
except json.JSONDecodeError as e:
print(f"Error: json.JSONDecodeError - {e}")
print(f"Debug: stdout = {result.stdout}")
return None, None
except Exception as e:
print(f"Error: Unexpected exception - {e}")
return None, None
if __name__ == '__main__':
main()