the public repo for the e-paper bulletin board. It's a bulletin board that syncs over scuttlebutt :)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
3.1 KiB

#!/usr/bin/python
# -*- coding:utf-8 -*-
# call this to sync any new bitmaps in SSB from our friends into our posts.json EBB DB
import ConfigParser
import json
import re
import subprocess
import datetime
import traceback
import iterparse
import uuid
import addtoDB
from tinydb import TinyDB, Query
def main():
nonspace = re.compile(r'\S')
def fresh():
#get our ssb pub key and db location from config
configParser = ConfigParser.RawConfigParser()
configFilePath = r'config.txt'
configParser.read(configFilePath)
ssbme = configParser.get('ebb-config', 'ssbme')
dbPath = configParser.get('ebb-config', 'dbPath')
imgpath = configParser.get('ebb-config', 'imagesPath')
db = TinyDB(dbPath)
#get time of last db-to-ssb sync from db
root_entry = db.get(doc_id=1)
last_run = root_entry["date"]
newssb="nada"
newssbcmd = 'ssb-server createLogStream --reverse 1 --gt ' + str(last_run)
#get the latest posts from ssb
try:
proc=subprocess.Popen(newssbcmd, 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=proc.stdout.read()
# iterate through new ssb posts
for decoded in iterparse.parsy(newssb):
# get the timestamp
timestamp = decoded['timestamp']
# get the value part of the ssb post
value = decoded['value']
#get the content part within value...
content = value['content']
#check that type is post
if(content['type'] == 'post'):
#check that we are not the author
if (value['author']!=ssbme):
#gotta pull out the non blob shiz
bigblobstring = content['text']
halfblob = bigblobstring.split("(")
theblob = halfblob[1].split(")")
#so now theblob[0] should be THE blob
dablob = theblob[0]
print dablob
#check to make sure we haven't already added this one
Fruit = Query()
isitthere=db.search(Fruit.ssb == dablob)
if (bool(isitthere)==False):
#make sure we actually have the blob
newssbcmd = 'ssb-server blobs.has "' + theblob[0] + '"'
try:
proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
except:
print 'traceback.format_exc():\n%s' % traceback.format_exc()
exit()
#tru/false:
newssb=proc.stdout.read()
print newssb
if (str(newssb)=='true\n'):
print "yep, we have the blob"
#generate unique file_name
unique = uuid.uuid4()
unique = str(unique)
bmpath = imgpath + unique + '.bmp'
#get the blob out of .ssb/blobs and put into imgages folder
newssbcmd = 'touch ' + bmpath + ' && sbot blobs.get "' + theblob[0] + '" >> ' + bmpath
try:
proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
except:
print 'traceback.format_exc():\n%s' % traceback.format_exc()
exit()
#add to db
addtoDB.addFile(bmpath,dbPath,dablob)
#update last_run (so that we don't have to search through ALL ssb posts everytime, this keeps track of oldest
Fruit = Query()
db.update({'date': timestamp}, Fruit.ssb == 'default')
if __name__ == '__main__':
main()